Solved

thread join method

Posted on 2014-02-07
8
458 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 35

Expert Comment

by:mccarl
ID: 39844001
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
ID: 39850824
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
ID: 39880857
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
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 
LVL 7

Author Comment

by:gudii9
ID: 39890211
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
 
LVL 35

Expert Comment

by:mccarl
ID: 39890577
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
ID: 39890592
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
ID: 39930513
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
ID: 39930519
join() method acting here almost same as wait() . then what is the difference between these methods.
please advise
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
web application structure 18 135
CSV file parsing thru Java 13 54
junit example issue 2 56
How to retrieve the HTTPServletResponse header field in script tag 1 42
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

738 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