File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread interrupt confusion

 
Lidija Sketa
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is from Devaka's ExamLab practice 3 mock exam (thank you Devaka for this great simulator!).

I modified the code a little bit and added printouts but I still don't get it - why doesn't call to a.interrupt() cause InterruptedException?



The output is:
Thread main started sleeping
Thread Thread-1 started sleeping
Thread main stopped sleeping
Calling interrupt
After call to interrupt
Thread Thread-1 stopped sleeping
Ex-B

And another thing:


How can we be sure that the first thing after call to t.start() will be doDelay(100)? Why isn't the first thing that is being executed the code in thread's run method?
 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The call to interrupt doesn't causes interrupted exception as you are interrupting a instead of t (the actual thread). In the output, you can see there is one exception (Ex-B). That is because you call wait on an object whose lock the thread doesn't have...
 
Lidija Sketa
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ooh, it's calling interrupt on a! I completely overlooked that fact, although for a second it looked suspicious

Thanks for clearing the confusion.

About the next statement that happens right after the thread.start() call: can we be sure that run() method will not execute right AFTER this call but rather will the next statement in the method?
In other words: in the previous example how can we be sure that doDelay(100); will execute before t.doDelay(1000)?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic