• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 478
  • Last Modified:

thread join method

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
gudii9
Asked:
gudii9
  • 5
  • 3
1 Solution
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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
 
gudii9Author Commented:
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
 
gudii9Author Commented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
gudii9Author Commented:
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
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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
 
gudii9Author Commented:
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
 
gudii9Author Commented:
join() method acting here almost same as wait() . then what is the difference between these methods.
please advise
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now