multithreading

G

Gordon Beaton

i did't understand the purpose of "JOIN()" method.please help me.
now, i have been learning java.i follow the book called "THE
COMPLETE REFERENCE".

If you have started a thread to do some task:

Thread t = new Thread(new MyRunnable());
t.start();

Then use

t.join();

to wait at that point until the other thread has finished.

/gordon

--
 
H

harsha

i did't understand the purpose of "JOIN()" method.please help me.
now, i have been learning java.i follow the book called "THE
COMPLETE REFERENCE".
 
A

Andrew Thompson

harsha said:
i did't understand the purpose of "JOIN()" method.

Did you mean the 'join()' method? Or some other method
that is not in the J2SE?*
now, i have been learning java.i follow the book called "THE
COMPLETE REFERENCE".

- There is no need to SHOUT at us.
- The word 'I' should always be upper case.
- Please put a single upper case letter at the start of
each sentence.

* For the J2SE classes, I find Sun's JavaDocs to
be valuable. They can be browsed on-line, e.g. ..
<http://java.sun.com/javase/6/docs/api/java/lang/Thread.html#join()>
..and also downloaded for local use.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200704/1
 
Z

Z.

harsha said:
i did't understand the purpose of "JOIN()" method.please help me.
now, i have been learning java.i follow the book called "THE
COMPLETE REFERENCE".

When thread A join()s thread B, thread A will wait for thread B to
terminate before resuming execution.

Add the Java API URL to your bookmarks:
http://java.sun.com/javase/6/docs/api/

....
A thread that has called Thread.join() is waiting for a specified thread
to terminate.
....
 
B

Bill

When thread A join()s thread B, thread A will wait for thread B to
terminate before resuming execution.

Add the Java API URL to your bookmarks:http://java.sun.com/javase/6/docs/api/

...
A thread that has called Thread.join() is waiting for a specified thread
to terminate.
...

Could you give a simple example why you would want to do this?
Thanks.

bill
 
Z

Z.

Could you give a simple example why you would want to do this?
Thanks.

Let's say you have two threads, A and B.

At some point, thread A cannot proceed until thread B has completed, so
thread A will join() thread B.

Thread A will then wait until thread B terminates, at which time
execution in thread A will automagically resume.
 
M

Michael

Could you give a simple example why you would want to do this?
Thanks.

bill

For a slightly more concrete example. Imagine that you want to do 5
things in parallel (say download 5 files off the internet), but you
want to tell the user when all 5 things are done. A trivial example
would be a web browser.. You want to stop spinning the wheel when
your'e done with all your tasks.. So you fire up 5 threads, then wait
for them to exit.. The Thread.join() waits until the 'run()' method
exits. So
File[] files = ...
Thread[] threads = new Thread[5];
for (int i = 0; i < 5; i++) { threads = new Thread(new
MyFileReader(files)).start(); }
for (int i = 0; i < 5; i++) { threads.join(); }
System.out.println("Done!");

The other reason to join threads is during shut-down, to make sure
that the thread is fully finished before taking down it's resources.
Imagine that you have a thread reading a TCP/IP socket. If the socket
is managed by a differnet thread, how do you coordinate the closing of
the worker threads before disconnecting the socket? Sure you can call
a custom myThreadWorker.shutdown() method, but how can that method
know when the thread is fully finished? Normally it would be
something like:

Object shutdownLock = new Object();
boolean running = true;
boolean injob = false;

void shutdown() {
synchronized (shutdownLock) {
running = false;
if (!injob) { this.interrupt(); }
}
this.join();
}

void run() {
... initialize
while (running) {
try {
s = socket.accept();
synchronized(shutdownLock) { if (!running) { return; } else
{ injob = true; } }
.... Do something useful
synchronized (shutdownLock) { injob = false; }
} catch (InterruptedException e) { if (!running)
{ LOG.info("Interrupted and exiting"); } else { LOG.warn("Interrupted
but not exiting!"); } }
}
}

There are other, possibly more elegant ways of handling this
synchronization.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,230
Latest member
LifeBoostCBD

Latest Threads

Top