Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

multithreading

Thread view: 
harsha - 23 Apr 2007 07:34 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".
Gordon Beaton - 23 Apr 2007 07:31 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".

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

--
Andrew Thompson - 23 Apr 2007 08:05 GMT
>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.

Signature

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

Z. - 23 Apr 2007 16:51 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.

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.
...
Bill - 24 Apr 2007 01:27 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 - 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.