aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes join() start() run() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "join() start() run()" Watch "join() start() run()" New topic
Author

join() start() run()

Moya Green
Ranch Hand

Joined: Jan 24, 2002
Posts: 49
http://www.jiris.com/ Mock 2 Q57:
Which of the following outputs are predictable in all platform? (Select two correct answers)
Assume t1, t2 are all newly created valid Thread objects.
---------------------
A:
t1.start();
t1.join();
t2.start();
t2.join();
B:
t1.start();
t2.start();
t1.join();
t2.join();
C:
t1.run();
t2.run();
t1.join();
t2.join();
D:
t1.join();
t2.join();
t1.start();
t2.start();
--------------------------------
The answers are A C. Can anyone help me expalin this? Thanks a lot!
Moya
Gautam Jha
Greenhorn

Joined: Sep 23, 2002
Posts: 10
A:
t1.start();
t1.join();
t2.start();
t2.join();

join() basically means wait till this thread is done. So in the first case t1 starts and waits till that thread is done. Once t1 is done, t2 starts. So the result is predictable on all platform. t2 does not start till t1 is done.

C:
t1.run();
t2.run();
t1.join();
t2.join();

Calling threads run() directly instead of start() causes it to run on the same thread (demon?)instead of creating a new one. Hence when t1.run() is called, it only exits when t1 is done. So t2 runs only when t1 is done. Thus the result is predictable on all platerforms.
As for option B, since both the threads are started using start(), how they are scheduled depends upon the platform and the scheduler. Hence result is unpredictable even on the same platform. Join statement only means waiting till both is done.
In option D, the two join() do not make any sense.
Threads have not started. So ignore those and you
have the same situation as option B.
Hope that helps. Good Question.
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
Hi,
The funtion join waits for the thread to die first before the normal execution of the program continues. Thus in Q.A t2 will only start executing after t1 dies.
A:
t1.start();
t1.join();
t2.start();
t2.join();
As for question C, since you didn't call the method start, the program will execute sequentially i.e t1 and t2 afterwards. There won't be any concurrent threads.
C:
t1.run();
t2.run();
t1.join();
t2.join();
Hope this helps.

Q. Why we should call Thread.start() to start a Thread? I called the run() method, no error or exception at all, why?
A:
When you call Thread start() method, JVM will create a new Thread, then call run() method of the new Thread. The new Thread will run concurrently with the oringinal calling Thread.
If you directly call the run() method, the Thread will act as a normal Java object. No new Thread will be created. The code will run sequentially.
Try the following code to see the difference, and get a feeling of concurrency.
// MyRun.java
//*******************************
public class MyRun implements Runnable {
public static void main(String argv[]) {
MyRun r = new MyRun();
Thread t = new Thread(r);

// t is still running after Main finished
t.start();

// No new thread created, Main finishes after run() returns
// t.run();

try {
// See concurrency if you call t.start()
// See sequential if you call t.run()
for (int i = 0; i < 5; i++) {
Thread.sleep(10);
System.out.println("in Main: " + i);
}
}
catch (InterruptedException e) {
System.out.println(e);
}
System.out.println("Main thread finished...");
}
public void run(){
try {
for (int i = 0; i < 5; i++) {
Thread.sleep(20);
System.out.println("in Run: " + i);
}
}
catch (InterruptedException e) {
System.out.println(e);
}
System.out.println("Run method finished...");
}
}
//*******************************


Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
How would t1.join() react if t1.start was never executed?
Moya Green
Ranch Hand

Joined: Jan 24, 2002
Posts: 49
G Jha and Shishio,
Thanks for the detailed explaination. I get it. I appreciate your help very much!
Moya
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
np i'm glad to help
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
Originally posted by Barkat Mardhani:
How would t1.join() react if t1.start was never executed?

If you take a look at the implementation of the join() funtion you'll see that it only calls join(0)

Now if you go to join(long millis), you'll notice that the function tests first whether the current thread is alive of not. Now to anwser you question, if a thread was never started the isAlive funtion in the join funtion will return false and thus the thread never enter the waiting state.
 
I agree. Here's the link: jrebel
 
subject: join() start() run()
 
Similar Threads
Tharead start() /join()
join() method in Thread doesn't work as expected
How can i start multiple threads ??
regding Thread.join
Threads