1

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?

noMAD
  • 7,744
  • 19
  • 56
  • 94
  • You have not specified how you want to run those three methods. Are you passing two numbers as a struct to the void* parameter? Are you adding two global variables? – Tudor Jan 25 '12 at 09:42

4 Answers4

3

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.

gprathour
  • 14,813
  • 5
  • 66
  • 90
  • Since the methods appear to be basic math operations, consider using an Enum (that implements Runnable) instead of a class. – user949300 Jan 25 '12 at 03:57
  • 1
    Although I get your line of thought, I'm not sure that I'd co-opt Enum and add operations to it. It simply would not be expected. Enums are really supposed to be light enumerated values. – Brill Pappin Jan 25 '12 at 04:22
3

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 */ }
  },
};
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
  • Will it be possible to elaborate a bit more on the above? I am a beginner hence not able to understand it completely. – noMAD Jan 25 '12 at 04:16
  • A runnable can be passed to a Thread and executed. He's saying that you can simply create the threads you need... in this case you can create three runnables to use for your three operations. – Brill Pappin Jan 25 '12 at 04:25
  • @noMAD, the `Runnable[]` is analagous to your C array of threads. You can create a `Thread` via `new Thread(runnables[i])` for each and start it running. – Mike Samuel Jan 25 '12 at 05:05
1

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.

Brill Pappin
  • 4,692
  • 1
  • 36
  • 36
0

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

Community
  • 1
  • 1
Hemant Metalia
  • 29,730
  • 18
  • 72
  • 91