Solved

thread join method

Posted on 2014-02-07
8
442 Views
Last Modified: 2014-03-24
Hi,

I am working on below example

http://www.avajava.com/tutorials/lessons/how-do-i-use-threads-join-method.html


If join() is called on a Thread instance, the currently running thread will block until the Thread instance has finished executing.

when author say 'until Thread instance has finished executing.' doe he meant current thread.
Note that join() can also take times in milliseconds and nanoseconds as parameters. These values specify the maximum amount of time to wait before returning the blocked thread's state to RUNNABLE.
Also which thread comes to RUNNABLE from blocked state, current or other joining thread.



Output is not clear to me

RunnableJob is being run by T1 at Tue May 19 01:56:11 PDT 2009
RunnableJob is being run by T2 at Tue May 19 01:56:12 PDT 2009
RunnableJob is being run by T3 at Tue May 19 01:56:13 PDT 2009
RunnableJob is being run by T4 at Tue May 19 01:56:14 PDT 2009
RunnableJob is being run by T5 at Tue May 19 01:56:15 PDT 2009
RunnableJob is being run by T7 at Tue May 19 01:56:15 PDT 2009
RunnableJob is being run by T6 at Tue May 19 01:56:15 PDT 2009
RunnableJob is being run by T8 at Tue May 19 01:56:15 PDT 2009

please advise
Any links resources ideas highly appreciated. Thanks in advance
0
Comment
Question by:gudii9
  • 5
  • 3
8 Comments
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
This should hopefully make it clearer...

Say you have two threads, A and B. If the code in thread A calls the join method on B's Thread object then thread A will stop running (block) waiting until thread B finishes executing. When thread B does finish, thread A will resume execution (at the next line after the join() call).

The output shows that the first 4 threads run in order, one after the other, because the main thread calls join on each thread after it starts it. Therefore, the main thread waits for each of those 4 threads before the next starts.

The second group of 4 threads run in some random order and all 4 run at the same time because the main thread simply starts all 4 at the same time, ie. no join calls in between the start calls.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
If the code in thread A calls the join method on B's Thread object then thread A will stop running (block) waiting until thread B finishes executing. When thread B does finish, thread A will resume execution

In the below code
thread1.join();

I wonder why they are not mentioning about thread2 in above line as we are joining thread1 with thread2 right.

i also wonder in the example output

RunnableJob is being run by T1 at Tue May 19 01:56:11 PDT 2009
RunnableJob is being run by T2 at Tue May 19 01:56:12 PDT 2009
RunnableJob is being run by T3 at Tue May 19 01:56:13 PDT 2009
RunnableJob is being run by T4 at Tue May 19 01:56:14 PDT 2009
RunnableJob is being run by T5 at Tue May 19 01:56:15 PDT 2009
RunnableJob is being run by T7 at Tue May 19 01:56:15 PDT 2009
RunnableJob is being run by T6 at Tue May 19 01:56:15 PDT 2009
RunnableJob is being run by T8 at Tue May 19 01:56:15 PDT 2009


Why  T1 did not come after T2 but instead T3 came


i expected to see first three lines something like below
RunnableJob is being run by T1 at Tue May 19 01:56:11 PDT 2009
RunnableJob is being run by T2 at Tue May 19 01:56:12 PDT 2009
RunnableJob is being run by T1 at Tue May 19 01:56:13 PDT 2009


not like below
RunnableJob is being run by T1 at Tue May 19 01:56:11 PDT 2009
RunnableJob is being run by T2 at Tue May 19 01:56:12 PDT 2009
RunnableJob is being run by T3 at Tue May 19 01:56:13 PDT 2009
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
It calls start() and then join() on each thread, in order. This blocks the execution of the current (main) thread from proceeding until the thread has completed. This means that the main thread will block for 1 second at each join(), since the RunnableJob sleeps for 1 second. Following this, ThreadExample creates 4 more threads, "T5", "T6", "T7", and "T8". This time, no joins are called on these threads, so the main thread will not block.

where  1second is specified. Why not 5 seconds.


thread1.join()

above join statement joins thread1 with which thread?


thread5.start();
            thread6.start();
            thread7.start();
            thread8.start();

After start if no join called similar to
thread1.start();
            thread1.join();

How is it different. Out put seems same to me with or without join except 1 second gap.

Please advise
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
Say you have two threads, A and B. If the code in thread A calls the join method on B's Thread object

How to know if A calls join method on B's Thread Object or  C's Thread Object (since we are only calling like threadA.join() not like threadA.join(B) or something like that)
Please advise
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 35

Expert Comment

by:mccarl
Comment Utility
It just depends on what thread is running at the time, ie. what thread actually calls the .join() method of another thread.

So if I remember the example correctly (because I too can't see the avajava pages now), it is the main thread which calls the .join() methods on the other threads. So for example, if in the main thread you call thread1.join() then it is the main thread that will go to sleep and wait for thread1 to finish running (and then the main thread continues on to the next statement)
0
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
Comment Utility
And if you are still wondering about the thread A and thread B situation that I talked about above, then think about it like this...

While a thread is running it is executing some lines of code, yeah? So let's say that thread A is executing some code that you have written and then it comes to a line like this...
threadB.join();

Open in new window

Yes, that line only mentions thread B but as I said first, it is thread A that is running that line of code, so that is how the two threads are determined. The code running at the time (thread A) when it gets to a line like threadB.join() means that thread A will stop and wait for thread B to finish running before thread A will continue again.

Any clearer?
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
avajava site is up again.

so
package com.cakes;

public class ThreadExample {

	public static void main(String[] args) throws InterruptedException {

		RunnableJob runnableJob = new RunnableJob();

		Thread thread1 = new Thread(runnableJob, "T1");
		Thread thread2 = new Thread(runnableJob, "T2");
		Thread thread3 = new Thread(runnableJob, "T3");
		Thread thread4 = new Thread(runnableJob, "T4");

		thread1.start();
		thread1.join();
		thread2.start();
		thread2.join();
		thread3.start();
		thread3.join();
		thread4.start();
		thread4.join();

		Thread thread5 = new Thread(runnableJob, "T5");
		Thread thread6 = new Thread(runnableJob, "T6");
		Thread thread7 = new Thread(runnableJob, "T7");
		Thread thread8 = new Thread(runnableJob, "T8");

		thread5.start();
		thread6.start();
		thread7.start();
		thread8.start();

	}

}

Open in new window


In above example main executes
>> thread1.join();
so main wait till thread1 completes its task (here it just sleeps for 1000 milli seconds) and then proceeds further right.

so output shows 1000 milli second difference for Thread1, 2, 3, 4,5
whereas Thread6,7,8 executed same time as the main thread with Thread6.join() etc not there.

Am i correct in my understanding
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
join() method acting here almost same as wait() . then what is the difference between these methods.
please advise
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now