Tito
Tito

Reputation: 852

Invoking different methods within one runnable thread in java

As far as my understanding is so far; a class which implements runnable 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.

Upvotes: 0

Views: 5562

Answers (5)

toto2
toto2

Reputation: 5326

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();

Upvotes: 0

bmargulies
bmargulies

Reputation: 100013

  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();

Upvotes: 3

Srikanth Venkatesh
Srikanth Venkatesh

Reputation: 2812

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");

}

}

Upvotes: -1

djna
djna

Reputation: 55897

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?

Upvotes: 2

Anarchofascist
Anarchofascist

Reputation: 474

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.

Upvotes: 2

Related Questions