• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Threads- que.2

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
janki tangeda
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Sergei Iakhnin.now it's clear to me.
reply
    Bookmark Topic Watch Topic
  • New Topic