Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Thread FAQ

Posted on 2004-08-10
6
Medium Priority
?
233 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 80 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 80 total points
ID: 11770017
Just call join onthe thread's instcane:

myThread.join();
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 80 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
Technology Partners: 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

Technology Partners: 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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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.
Suggested Courses

618 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