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?

1
  • 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? Commented Jan 25, 2012 at 9:42

4 Answers 4

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 */ }
  },
};
Sign up to request clarification or add additional context in comments.

3 Comments

Will it be possible to elaborate a bit more on the above? I am a beginner hence not able to understand it completely.
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.
@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.
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.

2 Comments

Since the methods appear to be basic math operations, consider using an Enum (that implements Runnable) instead of a class.
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.
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.

Comments

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

4 Comments

So this way you would call mine.add(int) or mine.subtract(int)?
sorry no need to create myclass object this thread will be invoked on start method.
if you want to access an object in different threads then you can create an class an call method of that class there are some alternatives also
Actually, in that case you run the risk of a race condition here because both threads will be accessing the same instance without synchronization. Gotta vote this one down for that reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.