Solved

Thread FAQ

Posted on 2004-08-10
6
206 Views
Last Modified: 2012-05-05
There is two ways to create threads:
1, implements Runnable
2, class MyThread extends Thread

i can call MyThread.join(); in the example 2 but how can i do that in example 1?

thanks
0
Comment
Question by:hawkly
  • 2
  • 2
  • 2
6 Comments
 
LVL 7

Assisted Solution

by:tomboshell
tomboshell earned 20 total points
ID: 11769944
The same way.  A runnable will not stand alone.  It will be given to a thread constructor.  Then you call the methods on the thread.

ex.
MyRunnable myRun = new MyRunnable();

Thread theThread = new Thread(myRun);
theThread.start();

Then it is all the same as you use in #2
0
 
LVL 35

Assisted Solution

by:girionis
girionis earned 20 total points
ID: 11770017
Just call join onthe thread's instcane:

myThread.join();
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 20 total points
ID: 11770440
If the class which implements Runnable has a Thread member which you're instantiating and over which you're calling the start () method, then expose it through a getter method:

public class Xyz extends Runnable
{
  private Thread t ;
 
  public Something ()
  {
    t = new Thread ( this ) ;
    t.start () ;

  } // end of constructor ()

  public void run ()
  {
    // the run () logic goes here

  } // end of run ()

  public Thread getHandle ()
  {
    return t ;

  } // end of getHandle ()

} // class definition over

Xyz obj = new Xyz () ;
obj.getHandle ().join () ;
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 35

Expert Comment

by:girionis
ID: 11838518
:)
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 11838690
thanks!
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11841027
;-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
create a gui in perl 3 72
eclipse compiler vs Installed JREs option 3 77
stackato and cloud 4 74
servlet example 17 10
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now