aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on Threads" Watch "Question on Threads" New topic
Author

Question on Threads

Shubha Kirani
Greenhorn

Joined: Jun 13, 2007
Posts: 12
For the below code, given answer as:
"other.join attempts to join two Threads" ... Please explain.

public class ThreadTest {

class InnerRun implements Runnable {
public void run() {
// do slow stuff
System.out.println ("Ren");
}
}
class InnerRunTwo implements Runnable {
Thread other;
InnerRunTwo(Thread t) {other = t;}
public void run() {
other.join();
System.out.println ("Stimpy");
}
}
void start() {
InnerRun ir = new InnerRun();
Thread t = new Thread(ir);
InnerRunTwo irr = new InnerRunTwo(t);
Thread h = new Thread(irr);
t.start();
h.start();
}
public static void main(String[] args) {
ThreadTest tt = new ThreadTest();
tt.start();

}

}
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Subha,

Welcome to JavaRanch!



class InnerRunTwo implements Runnable {
Thread other;
InnerRunTwo(Thread t) {other = t;}
public void run() {
other.join(); //LINE 1
System.out.println ("Stimpy");
}
}


You should read Line #1 in the way: the current thread joins other thread.
It means current thread can't complete until the thread it joins to
completes.

[EDIT]
Note:
join() throws InterruptedException, so handle that exception.


Thanks,
[ June 13, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Pravin Jain
Ranch Hand

Joined: May 20, 2007
Posts: 60

Note: join() throws IllegalStateException, so handle that exception.


it actually throws InterruptedException and not IllegalStateException
and you need to handle in the run method.


The Zen of Java Programming.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Ooops!!!

It is InterruptedException!

"Previous post edited"

Thanks,
Shubha Kirani
Greenhorn

Joined: Jun 13, 2007
Posts: 12
Thank you Chandra and Pravin.
Regarding the question, this is from Kathy & Bert, Master Exam. If I enclose 'other.join' in try-catch block (handle InturreptedException), code would compile fine. As explained in the answer key, I did not understand the meaning of 'attempts to join two threads' as there are only two threads. Other two similar program solution say, one thread join after other. SO wanted to know if I'm missing something here.

I wonder the # of posts answered everyday in this website. I'm very new to Java and Ranch. Like to start my career in Java, starting with SCJP certification. Hope one day I get to answer others quiries.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on Threads
 
Similar Threads
Thread functioning help
Master exam Thread question
Mock Exam question, Interrupted Exception
Threads trying to get InterruptedException
When both Thread and Runnable have public void run