noMAD
noMAD

Reputation: 7844

How to assign different methods to different threads in java

I know a bit about threads in 'C' but am new to them in Java. If I want to create three threads, one for addition, second for subtraction and third for multiplication I can simply do

pthread_t mathThread[3];

and while creating each thread I can assign them different functions in their arguments.

void *add(void *arg);
void *sub(void *arg);
void *mul(void *arg);

In Java, if I implement the Runnable interface there is only one run() method that I can use. How do I implement the above?

Upvotes: 1

Views: 4393

Answers (4)

Brill Pappin
Brill Pappin

Reputation: 4800

You really don't need to do this in Java... I mean you don't need to have basic math in a separate thread unless your calculating Pi or something else only a little less lengthy. If your just handling math, do it in line and the the compiler optimize as needed.

There are a number of ways to do it, but I suggest that you will get in the least amount of trouble if you review the Executor class in the java.util.concurrent package. The javadocs have a lot of info on how to use them (all the info you'll need to get it running).

You can find those docs here: http://docs.oracle.com/javase/6/docs/api/index.html?java/util/concurrent/package-summary.html

Assuming you still want to do this the hard way:

However in the intrest of just giving you the darn answer already, you can do as noMADD suggested or you can also use an anonymous inner class.

class MathStuff {
  boolean running = false;
  int result = 0;

  boolean isRunning(){
      return running;
  }

  int getResult(){
      return result;
  }

  int add(final int arg0, final int arg1){
     Thread t = new Thread(){
         public void run(){
           running = true;
           result = arg0 + arg1;
           running = false;
         }
     };
     t.start();
  }
}

Note that I added the running member variable because the result is not valid while it returns true. Usually I'd build this kind of thing with an event listener that would receive the callback when the operation was completed, however I used a simple isRunning call so as to not obfuscate the thread part too much.

Upvotes: 1

Hemant Metalia
Hemant Metalia

Reputation: 30648

see the example here i have created two threads t1 and t2 where t1 does the addition and t2 does the subtraction.

class ThreadClass {    
    public static void main(String a[]) {

        Thread addthread = new Thread() {
                public void run() {
                //addition logic here
            }
        };
        Thread subtractThread = new Thread() {
              public void run() {

              //subtraction logic here

            }
        };
        addthread .start();
        subtractThread .start();
    }
}

this is from my own question java access an object in different threads

follow this tutorial Java - Multithreading

Upvotes: 0

Mike Samuel
Mike Samuel

Reputation: 120516

Create three different runnables:

Runnable[] runnables = new Runnable[] {
  new Runnable() {
    public void run() { /* add code here */ }
  },
  new Runnable() {
    public void run() { /* sub code here */ }
  },
  new Runnable() {
    public void run() { /* mul code here */ }
  },
};

Upvotes: 3

gprathour
gprathour

Reputation: 15333

Simplest way : You can make three different classes implementing Runnable interface, each for doing one operation and then starting all three threads from main thread.

Upvotes: 3

Related Questions