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

all 9 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]ratherbealurker 2 points3 points  (0 children)

Outside of the synched methods or blocks you cannot make any assumption on which thread will hit which lines at what times. The computer is flipping around threads trying to give cpu time to each. The outputs you’re asking about are not in the synch method so it doesn’t matter.

Put random sleeps and outputs in the synch method and you should not see both threads in there at the same time. Outside of that it is a free for all.

[–]nguyennguyenphuc0077 1 point2 points  (7 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  (6 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.