开发者

Invoking different methods within one runnable thread in java

As far as my understanding is so far; a class which implements 开发者_如何学Gorunnable seems to only be able to perform one set task within its run method. How is it possible to create a new thread and then run different methods from this one additional thread, without needing to create some new runnable class for every set task.


  1. Make your own subclass of Thread (MyThread extends Thread)
  2. Add private members to control the behavior.
  3. Add bean-pattern get/set methods to control the private members, or use a fluent API.
  4. Read this properties in the run() method.
 MyThread t = new MyThread();  
 t.setTypeOfSparrow("African");  
 t.setFavoriteColor("Yellow");  
 t.start();


Your Runnable class can call any logic it likes. The logic you want to run must be in some class, could be different methods of the Runnable class or could be in lots of other classes.

How did you plan to tell the runnable what to do?

You could do something like:

 MyRunnable implements Runnable {
     private String m_whatToDo;

     public MyRunnable(String whatToDo) {
           m_whatToDo = whatToDo;
     }


     public void Runnable run() {
          if ("x".equals(m_whatToDo) {
                 // code to do X
          } else if ( "y".equals(m_whatToDo) {
                 // code to do Y
          } else {
                 // some error handling
          }
     }
 }

Or as Srikanth says you could communicate the intent by other means such as thread names.

However I don't see much overhead in creating a runnable class. Just adding a public void run() to a class is surely not that big a deal?


A class should perform one task and perform it well, and if you are adding multiple operations in a single Runnable then you are violating this principle. You should create a new implementation of Runnable for each runnable task.

To simplify your api you might like to create a MyRunnableFactory method which constructs a runnable class depending on one or more construction criteria. This would shield the user from having to remember which class to create for each task.


Your question isn't quite clear. My guess is that you want to run different methods in some other thread, but you don't want to waste time restarting a new thread for each method. In that case you need an ExecutorService with one thread. You can submit sequentially some Runnables to a thread that is kept alive between calls.

Or more simply if you already know the order in which your methods are called

 (new Thread() {
   @Override public void run() {
      method1();
      method2();
      ...
   }
 }).start();


In the run() method check for the thread name and call the appropriate method.

 public class SampleThread  implements Runnable{
/**
 * @param args
 */
Thread t=null;

public SampleThread(String threadName)
{
    t=new Thread(this,threadName);
    t.start();

}
@Override
public void run() {
         if(t.getName().equals("one"))
         {
             One();
         }
         else if(t.getName().equals("two"))
         {
             Two();
         }
}
public void One()
{
    System.out.println(" ---- One ---- ");
}
public void Two()
{
    System.out.println(" ---- Two ---- ");
}

public static void main(String[] args) {
     SampleThread t1=new SampleThread("one");
     SampleThread t2=new SampleThread("two");

}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜