• 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

Sleepy Threads

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found the following interesting question on a mock exam:
When application A below is run, two threads are created and started. Each thread prints a message just before terminating. Which thread prints its message first?
class A {
private Thread t1, t2;
public static void main(String[] args) {
new A();
}
A() {
t1 = new T1();
t2 = new T2();
t1.start();
t2.start();
}
class T1 extends Thread {
public void run() {
try {
sleep(5000); // 5 secs
}
catch (InterruptedException e) { }
System.out.println("t1 done");
}
}
class T2 extends Thread {
public void run() {
try {
t1.sleep(10000); // 10 secs
}
catch (InterruptedException e) { }
System.out.println("t2 done");
}
}
}
The answer is T1 since even though it looks like T2 will put T1 to sleep for an even longer amount of time, sleep() is a static method and executes on the currently running thread. Cool.
My question is, is why is ANYTHING printed out at all? I thought that an InterruptedException was only thrown when a sleeping or waiting thread received an interrupt() call from an executing thread. No such call is coded here. I didn't think that such an exception was automatically thrown when a sleeping or waiting thread entered the ready state. Am I wrong in so thinking?
Dan
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No InterruptedException is actually thrown in this code. Each sleep() method completes normally, after which it has reached the end of the try{} block, so it skips over the catch{} block to the first code after the try/catch, which is a print statement. Each print statement will be executed whether an exception is thrown or not.
 
Dan Temple
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
God I am such an idiot ... for some reason, my brain saw that output as being inside the catch block.
Sorry about that ...
Dan
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
S'OK.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which brings up the touchy issue about coding style.
if the catch had been coded:

It would have been MUCH easier for you to see that the println was AFTER it.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It also brings up the importance of using proper indentation, and [ code ] tags to preserve this indentation at JavaRanch. Actually I see that Dan's original source was indented properly - it's just the [ code ] tags that are missing. Personally I think it's perfectly well readable with the proper indentation, without putting the braces on separate lines. But it's all a matter of what style you're used to, I think...
 
reply
    Bookmark Topic Watch Topic
  • New Topic