Solved

Thread FAQ

Posted on 2004-08-10
6
214 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Jpanels and Jframe 8 33
xampp tool 12 50
fibonacci ten numbers 4 39
use lov values 2 50
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

777 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