• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a tip on threads: (follow up)

 
Alfred Kemety
Ranch Hand
Posts: 279
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jay Ashar
Ranch Hand
Posts: 208
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouldnt it give the compilation error because wait() method is used and its not inside synchronize block.
 
Jay Ashar
Ranch Hand
Posts: 208
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops sorry not compilation but runtime exception.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 279
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 208
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmmm... is the answer 1...?
 
Shishio San
Ranch Hand
Posts: 223
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only when bTest calls method2(). When method2 is called by the thread aTest, the monitor was already acquired.
 
Alfred Kemety
Ranch Hand
Posts: 279
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic