Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Concurrency Question with Thread.sleep(5000) ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Concurrency Question with Thread.sleep(5000) ?" Watch "Concurrency Question with Thread.sleep(5000) ?" New topic

Concurrency Question with Thread.sleep(5000) ?

Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
In the following code snippet, there are 3 threads ("main", "t1", "t2"). In other words, there are 3 Thread objects. printName() is synchronized and printValue() is synchronized. run() is not synchronized. The "main" Thread starts first, then the "t1" thread is started. The "t1" thread acquires the lock on the "t1" Object when it enters the printName() method. "printName" is printed, and then "t1" enters a 5 second sleep(). While "t1" is sleeping, "main" Thread continues executing. "t2" Thread acquires the lock on "t2" Object when it enters synchronized printValue() method. "printValue" is printed, and "t2" Thread completes. "main" thread completes. "t1" thread finishes the 5 second wait, and "t2" Thread completes.

Does this sound correct to you?

Options are:
A.print : printName , then wait for 5 seconds then print : printValue
B.print : printName then print : printValue
C.print : printName then wait for 5 minutes then print : printValue
D.Compilation succeed but Runtime Exception

Answer :
A is the correct answer. I think the answer is B.

There is only one lock per object, if one thread has picked up the lock, no other thread
can pick up the lock until the first thread releases the lock. printName() method
acquire the lock for 5 seconds, So other threads can not access the object. If one
synchronized method of an instance is executing then other synchronized method of
the same instance should wait.
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206

How many objects have been created by this code snippet ?
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206

The original code snippet results in "printName" + 5 second wait + "printValue"

This code snippet results in "printName" + "printValue" + 5 second wait
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
A is the correct answer. I think the answer is B.

Option B is wrong.
Both synchronized methods are in class B.
Lock is acquired on object 'b', not on object 't1' nor 't2'.

Thread 't1' enters b.printName, acquires lock on 'b', then sleeps for 5 seconds.
Thread 't2' enters b.printValue, trys to acquire lock on b - but b is already locked by t1, so t2 must waits util lock is released.
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
I understand now.

An instance of class B acquires the lock, when thread "t1" enters the synchronized printName() method. Because both printName() and printValue() are both synchronized methods of class B, thread "t2" cannot enter the printValue() method until the printName() method releases the lock on the only instance of class B.

The reason the second code snippet executes differently is because there are two instances of class B. "t1" acquires the lock on the first instance of class B, and "t2 acquires the lock on the second instance of class B.
Henry Wong

Joined: Sep 28, 2004
Posts: 20057

Jose Armando,

Your post was not really related to this topic; and was moved to a new topic.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Concurrency Question with Thread.sleep(5000) ?
It's not a secret anymore!