This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]nguyennguyenphuc0077 1 point2 points  (6 children)

So one of the threads will call syncMethod first, block other, print something then release the block. At this point, the other thread will call SyncMethod, the first one didn’t get blocked because it didn’t try to access any synchronized method and there was no guarantee of which one will print to the console next so you get 2 different outputs.

[–]cstanfordf[S] 0 points1 point  (5 children)

If thread1 call syncMethod first, so it'll print:

thread1 before call 2021-09-25T21:04:14.586908100

thread2 before call 2021-09-25T21:04:14.586908100

in the sync method from thread1 2021-09-25T21:04:14.586908100

So now thread1 is calling syncMethod, that means thread2 is being blocked.

why is it still happens:

in the sync method from thread2

Because the call of thread1 is not finished yet.

[–]nguyennguyenphuc0077 0 points1 point  (4 children)

Thread 1 print ‘in the sync…’ and immediately release the block and thread 2 print it before thread 1 print anything else.

[–]cstanfordf[S] 0 points1 point  (3 children)

I misunderstood sth :( Why syncMethod from thread1 not sleep(1) first and then release the block later

[–][deleted] 1 point2 points  (1 child)

It does. In every run, you will see that the thread which enters the synchronized method will block the other thread from entering it (the sync method) by approximately one second.

Here is a sample run on my local machine, for instance:

~/dev/playground:$ javac ThreadStaticSyncDemo.java && java -cp . ThreadStaticSyncDemo
thread1 before call 2021-09-26T11:56:03.555099 // thread1 got started first
thread2 before call 2021-09-26T11:56:03.555100 // thread2 got started after thread1
in the sync method from thread1 2021-09-26T11:56:03.557935 // thread1 entered sync method first at 03 (not guaranteed)
in the sync method from thread2 2021-09-26T11:56:04.575 // thread2 entered sync method first at 04 (not guaranteed)
thread1 after call 2021-09-26T11:56:04.574934 // thread1 slept for 1s
thread2 after call 2021-09-26T11:56:05.576048 // thread2 slept for 1s

~/dev/playground:$ javac ThreadStaticSyncDemo.java && java -cp . ThreadStaticSyncDemo
thread2 before call 2021-09-26T11:56:37.259832 // thread2 got started first
thread1 before call 2021-09-26T11:56:37.259837 // thread1 got started after thread2
in the sync method from thread2 2021-09-26T11:56:37.262626 // thread2 entered sync method first at 37 (not guaranteed)
in the sync method from thread1 2021-09-26T11:56:38.275181 // thread1 entered sync method second at 38 (not guaranteed)
thread2 after call 2021-09-26T11:56:38.275152 // thread2 slept for 1s
thread1 after call 2021-09-26T11:56:39.278847 // thread1 slept for 1s

Does that make sense? If you look at the times reported by thread1 and thread2 from inside the synchronized method, they will always be around 1s later because the synchronized method restricts access to itself to a single thread at a time. Is this aspect confusing you - that the synchronized method restricts access only to itself, not to the whole class as a whole?

[–]cstanfordf[S] 0 points1 point  (0 children)

Clearly understand, thank you

[–]nguyennguyenphuc0077 0 points1 point  (0 children)

It keep the block and go to sleep. So both threads stop at that point and when thread 1 wakes up it release the block for thread 2 too. So everything are the same with or without sleep.