• 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

Thread Question

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of the mock exam questions -

What will be the output of this program? Can someone explain the behaviour?
Also what will happen if I replace synchronized(this) in startMe() method will synchronized(ThreadO.class) ?

public class ThreadO {


public static void main(String[] args) {

TestThread t1 = new TestThread();

t1.start();

t1.restart();

}


}



class TestThread extends Thread{

public void restart(){

startMe();

}



public void startMe(){

synchronized(this){

notify();

System.out.println("passing notify");

}

}



public void run(){

try{

synchronized(this){
wait();

System.out.println("Waited long enough -- Quitting");

}

}

catch(InterruptedException e){}

}

}

Thanks
Krishna
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Krishna,


What will be the output of this program? Can someone explain the behaviour?



The output is unpredictable because the start() call may or may not take place immediately after invoked.

Supposing start() method takes place immediatelly after called, the output would be as expected : passing notify and then Waited long enough -- Quitting.

But if start() takes place only after startme() call, the output would be passing notify and then your program would infinitely wait and never print Waited long enough -- Quitting.

This last behavior would occur due to startme() releases the lock on object t1 before the other thread running call the wait() method.


Also what will happen if I replace synchronized(this) in startMe() method will synchronized(ThreadO.class) ?



An IllegalMonitorStateException would happen. This is because when calling notify() from within startMe(), the notify() method called is from t1 object, thus you have to have the lock on this object in order to call notify().

ThreadO.class has no effect here.
 
Krishna Attravanam
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Edisandro

Thanks for the response. I could figure out the first part until I ran the code on a compiler. The thread was always waiting and notify() was getting invoked first. That confused me

Krishna
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
frist off ,in your program ,there exist two thread ,one is t1,the other is main thread(we called) t1 need to own the lock which is the instance of TestThread
and then invoke wait() method.the main thread is need to own the lock which is the samne as t1 and then invoke notify() method
I think you expect the jvm will invoke the t1 frist ,but we can not assure.
if you only want to invoke the thread t1 frist ,you can do the following

and then o/p

generally speaking the jvm will execute parts code of main thread(personal opinion :the jvm assure all the thread which are created start in time) and then
execute the thread t1,so will appear the thread t1 always waiting


Also what will happen if I replace synchronized(this) in startMe() method will synchronized(ThreadO.class) ?


also the static lock is apply to static field or static method
[ April 08, 2006: Message edited by: Changchun Wang ]
reply
    Bookmark Topic Watch Topic
  • New Topic