Multithreading and thread-priority synchronization

Suppose I have the following code snippet

public synchronized void method()
{
    if(something == null)
    {
        something = new SomeThing();
    }
    //do something 
}

Now suppose that in a multi-threaded environment, one thread [Thread 1] enters a method and was unloaded right after its execution new Something();, but before it could assign it something. Then another thread [Thread 2] also tries to call the method. What exactly is happening now? What happens to the lock Thread 1 acquired? Will Rollback Thread 1 Steps?

+3
source share
2 answers

Thread1 , . Thread2 , , BLOCKED. , Thread1, . Thread2.

+7

2 , 1 , .

Thread 1, Thread 1 Something() . Thread 2 Something().

, Thread 1 , . Thread 1 , (), Thread 2 .

+3

Source: https://habr.com/ru/post/1791638/


All Articles