> 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".
> > i did't understand the purpose of "JOIN()" method.please help me.
> > now, i have been learning java.i follow the book called "THE
[quoted text clipped - 9 lines]
> to terminate.
> ...
Could you give a simple example why you would want to do this?
Thanks.
bill
Z. - 24 Apr 2007 02:07 GMT
>>> 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.
[quoted text clipped - 3 lines]
>> to terminate.
>> ...
> 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.
Michael - 24 Apr 2007 14:04 GMT
> > > i did't understand the purpose of "JOIN()" method.please help me.
> > > now, i have been learning java.i follow the book called "THE
[quoted text clipped - 14 lines]
>
> 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[i] = new Thread(new
MyFileReader(files[i])).start(); }
for (int i = 0; i < 5; i++) { threads[i].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.