This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Sleepy Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sleepy Threads" Watch "Sleepy Threads" New topic
Author

Sleepy Threads

Dan Temple
Ranch Hand

Joined: Jul 10, 2001
Posts: 93
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
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Dan Temple
Ranch Hand

Joined: Jul 10, 2001
Posts: 93
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
Sheriff

Joined: Jan 30, 2000
Posts: 18671
S'OK.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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.

"JavaRanch, where the deer and the Certified play" - David O'Meara
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sleepy Threads
 
Similar Threads
RHE Mock exam
thread dies, implicit notifyAll?
"synchronizing" a static member
Concurrency Question with Thread.sleep(5000) ?
wait() and notifyAll()