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 a tip on threads: (follow up) 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 "a tip on threads: (follow up)" Watch "a tip on threads: (follow up)" New topic
Author

a tip on threads: (follow up)

Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
Consider the following code:

1- Output is:
start main thread
End main thread
aTest
bTest
2- Output is:
start main thread
aTest
bTest
End main thread
3- One can not be sure what the output will be.
4- Compilation fails.
5- Run-time exception thrown on execution.
[ October 29, 2002: Message edited by: Alfred Kemety ]

Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Jay Ashar
Ranch Hand

Joined: Oct 13, 2002
Posts: 208
Shouldnt it give the compilation error because wait() method is used and its not inside synchronize block.


SCJP 1.4<br />SCWCD 1.3
Jay Ashar
Ranch Hand

Joined: Oct 13, 2002
Posts: 208
oops sorry not compilation but runtime exception.
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Originally posted by Hetal Mehta:
oops sorry not compilation but runtime exception.

I agree -- specifically an IllegalMonitorStateException because calls to .wait() and .notify() need to be in a synchronized block.
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
That's correct, one would jump though to (compile error) since in the back of the mind we have "It must be in a synchronized block"
And for your info, it will throw IllegalMonitorStateException 2 times. one for each thread aTest and bTest.
How about the following code???

Well, we'll not ask what will the output be, we all know one can't be sure what will be printed, but the question will be:
How many IllegalMonitorStateException will be thrown?? and WHY? And if any is thrown, which thread will throw it? Will the other threads continue to execute?
Try to THINK and asses the code, NOT copy it and run it...
[ October 29, 2002: Message edited by: Alfred Kemety ]
Jay Ashar
Ranch Hand

Joined: Oct 13, 2002
Posts: 208
hmmm... is the answer 1...?
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
Only when bTest calls method2(). When method2 is called by the thread aTest, the monitor was already acquired.


Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
Right on Shishio, to add:
1- The thread "main" is the thread that throws the Exception at run-time.
2- Even though the main thread is aborted, the other threads aTest and bTest continue execution and print what they should print. A program exits after the last user thread finish executing the run method or abruptly ends for another reason.
3- The output is unpredictable and is dependent on the JVM scheduler.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a tip on threads: (follow up)
 
Similar Threads
Java thread question...
output of this code
A quick question on threads
Simple thread question
Join() method in Java