Solved

Thread FAQ

Posted on 2004-08-10
6
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

733 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