File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes join method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "join method " Watch "join method " New topic
Author

join method

sweety sinha
Ranch Hand

Joined: Jul 07, 2008
Posts: 76
this is from: SCJP 6 Mock exam for Threads � Niko�s java blog.mht


if line 1 is commented then answer is 0, otherwise it is 999999.
what join() is doing here? i know that join method of thread lets one thread to join onto the end of another thread. Here when start is called on Job instance, then run method is invoked and after completion of that method thread.join() should be called. how this code is working?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

The join method works in this fashion

in a thread(let's name it t1) you create a thread instance(let's name it t2). then you start that thread t2. then in t1, if you want something to be executed only when t2, then you call t2.join. The call will only return when t2 completes execution. So the execution of t1 will stop or pause till t2 completes.


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

What is happening is,
Thread.join() method joins the current thread(Main in this case) with the previously started thread.

So what happens is, Main method waits for the previously started thread to complete. After the thread completes, main method continues to execute.

When you comment Thread.join(), what happens is, two parallel thread starts running(Main thread and the thread printing numbers in this case) and they execute without waiting for each other.


Enjoy, Chander
SCJP 5, Oracle Certified PL/SQL Developer
Mohammad Khan
Ranch Hand

Joined: Sep 23, 2008
Posts: 37
Following example might help to understand the issue you are having.



class Job implements Runnable {
private int number = 0;

public void run() {
for (int i = 0; i < 1000000; i++) {
synchronized (this) {
this.number++;
}
}
}

public int getNumber() {
return number;
}
}

public class Test {
public static void main(String[] args) throws Exception {
Job job = new Job();

Thread t1 = new Thread(job, "t1");
Thread t2 = new Thread(job, "t2");
Thread t3 = new Thread(job, "t3");

t1.start();// at this point we have main thread and t1
t2.start();// so far we have main thread, t1 and t2
t3.start();// now we have total 4 threads which are main, t1, t2 and t3

// join/append Thread t1 at the end of main thread
// this would leave us total 3 thread main, t2 and t3
//t1.join();

// join/append Thread t2 at the end of main thread
// and this would leave main thread and t3
//t2.join();

// join/append Thread t3 at the end of main thread
// this would leave only main thread
//t3.join();


// making sure to print the number after finished three threads t1, t2, t3
while (true) {
if ((t1.getState() == Thread.State.TERMINATED) && (t2.getState() == Thread.State.TERMINATED) && (t3.getState() == Thread.State.TERMINATED)) {
// If this.number incrementation is not synchronized in run method,
// it is not guaranteed to satisfy number == 3000000
System.out.println(job.getNumber());
break;
}
}
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: join method
 
Similar Threads
Thread question
Niko's mock exam question
Thread - join method
Problem in Threading - 2
another question from nikos