문제

I noticed interesting behaviour:

I can write

class My {
     public My(){
        synchronized(this){
            // code
        }

    }  
}

but I cannot write

class My {
         public synchronized My(){
                // code
            }

        }  
    }

I thought that both constructions works identically terms java API.

Please clarify this misunderstanding.

도움이 되었습니까?

해결책

In the first case, the object is almost created, only the body of the Constructor need to executed.

But, in the second case, there won't any object to synchronize.

다른 팁

It is impossible situation that two Threads will create same object !

Thus synchronized modificator hasn't sense for constructor.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top