File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Question" Watch "Thread Question" New topic
Author

Thread Question

Krishna Attravanam
Greenhorn

Joined: Jan 11, 2006
Posts: 9
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
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
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.


"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
Krishna Attravanam
Greenhorn

Joined: Jan 11, 2006
Posts: 9
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
Changchun Wang
Ranch Hand

Joined: Feb 15, 2006
Posts: 83
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Question