wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads- que.2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads- que.2" Watch "Threads- que.2" New topic
Author

Threads- que.2

janki tangeda
Ranch Hand

Joined: Jun 07, 2005
Posts: 54
class A extends Thread {
String[] sa;
public A(String[] sa) {this.sa = sa;}
public void run() {
synchronized (sa) {
while (!sa[0].equals("Done")) {
try {sa.wait();} catch (InterruptedException ie) {}
}}
System.out.print(sa[1] + sa[2] + sa[3]);
}}
class B {
private static String[] sa = new String[]{"Not Done","X","Y","Z"};
public static void main (String[] args) {
Thread t1 = new A(sa); t1.start();
synchronized (sa) {
sa[0] = "Done"; //1
sa[1] = "A"; sa[2] = "B"; sa[3] = "C";
sa.notify();
}}}

The given output for this program is ABC.The explanation is that thread t1 enters wait state and by the time notify gets called from main method all values are changed.
But by the time t1 gets to exeute run() method the main thread may execute line 1.Thus with sa[0]= "Done" t1 may not enter into wait state at all.Is there no such possibility?If no, why?
Sergei Iakhnin
Ranch Hand

Joined: Nov 09, 2004
Posts: 53
There are three possible scenarios here.

Scenario 1 - The whole main method completes before the run method of t1 ever starts. Then trivially the program will print ABC.

Scenario 2 - Thread t1 gets CPU time for the run method before s[0] gets set to "Done" (and before the lock on sa is acquired in main). The run method will acquire the lock on sa, check whether sa[0] equals to "Done" and if not (which will be the case) the thread will block and release the lock on sa. At that point the main method will eventually get switched into the CPU again acquire the lock on sa, change the values to "Done" and "A", "B", and "C" and notify a thread waiting on sa. Then t1 will wake up and print ABC.

Scenario 3 - Lastly, if main method starts, creates t1, acquires lock on sa, changes sa[0] to "Done" and then gets switched out of the CPU and the run method of t1 gets switched in for execution it will never be able to execute the line System.out.print(sa[1] + sa[2] + sa[3]); because that line occurs inside a block synchronized on sa and the lock for sa would be held by main at that point. The only time it would give up the lock would be after it exits its own synchronized block.

Thus the line System.out.print(sa[1] + sa[2] + sa[3]); will always print ABC


SCJP 1.4, SCWCD 1.4
janki tangeda
Ranch Hand

Joined: Jun 07, 2005
Posts: 54
Scenario 3 - Lastly, if main method starts, creates t1, acquires lock on sa, changes sa[0] to "Done" and then gets switched out of the CPU and the run method of t1 gets switched in for execution it will never be able to execute the line System.out.print(sa[1] + sa[2] + sa[3]); because that line occurs inside a block synchronized on sa and the lock for sa would be held by main at that point. The only time it would give up the lock would be after it exits its own synchronized block.


In the above scenario what will happen when main menthod comes to line
sa.notify();
t1 can starting running only after main method finishes the execution of synchronized block but how will the main method execute notify() when t1 is not in waiting state.
Sergei Iakhnin
Ranch Hand

Joined: Nov 09, 2004
Posts: 53
The sequence of events is

main calls notify on sa
t1 wakes up and requests sa lock (is blocked waiting for the lock)
main releases sa lock
t1 acquires sa lock and is at that point allowed to execute synchronized code
janki tangeda
Ranch Hand

Joined: Jun 07, 2005
Posts: 54
thanks Sergei Iakhnin.now it's clear to me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads- que.2
 
Similar Threads
Synchronized Objects
Threads Exam 2 D.Chisholm
Dan's: Thread Q 39
Dan Chisholm Thread Mock test Q
Thread - Wait Doubt