0

I am working on a project in which I have multiple interface and two Implementations classes which needs to implement these two interfaces.

Suppose my first Interface is -

public Interface interfaceA {
    public void abc() throws Exception;
}

And its implementation is -

public class TestA implements interfaceA {

    // abc method
}

I am calling it like this -

TestA testA = new TestA();
testA.abc();

Now my second interface is -

public Interface interfaceB {
    public void xyz() throws Exception;
}

And its implementation is -

public class TestB implements interfaceB {

    // xyz method   
}

I am calling it like this -

TestB testB = new TestB();
testB.xyz();

Problem Statement:-

Now my question is - Is there any way, I can execute these two implementation classes in parallel? I don't want to run it in sequential.

Meaning, I want to run TestA and TestB implementation in parallel? Is this possible to do? Initially I thought of using Callable but Callable requires return type but my interface methods are void so not sure how can I run these two in parallel.

AKIWEB
  • 19,008
  • 67
  • 180
  • 294
  • Yes, it's possible, but why do you want to? It's a lot more complicated than running them sequentially. – chrylis -cautiouslyoptimistic- Apr 02 '14 at 02:26
  • I see.. In my case, both of the methods abc and xyz are writing into different databases. So I need to write that in parallel instead of sequential. – AKIWEB Apr 02 '14 at 02:28
  • Duplicate of [How to run two classes in parallel using multithreading?](http://stackoverflow.com/questions/22755151/how-to-run-two-classes-in-parallel-using-multithreading/22755430#22755430) that you have already asked. Haven't you got any answer in your last question? – Braj Apr 02 '14 at 04:21
  • Actually its little different, In that question - I have a return type as string and here I dont have return type as string. I have void.. Please read this question then you will get the idea. – AKIWEB Apr 02 '14 at 04:22

1 Answers1

0
Thread thread1 = new Thread(new Runnable() {
    public void run() {
        TestB testB = new TestB();
        testB.xyz();
    }
});
Thread thread2 = new Thread(new Runnable() {
    public void run() {
        TestA testA = new TestA();
        testA.abc();
    }
});

thread1.start();
thread2.start();

Another way - if you have a lot of runnable

ExecutorService service = Executors.newFixedThreadPool(10);
for (int i = 0; i < 10; i++) {
    service.submit(new Runnable() {
        public void run() {

        }
    });
}
zella
  • 4,645
  • 6
  • 35
  • 60
  • Thanks, this helps a lot. In my example I have around 7-8 implementations as opposed to current example in which I only have 2. In that case, do I need to keep in making new `thread` for that? Doesn't it look pretty long code if I have more than `8-9`. Or is there any other way of doing it? – AKIWEB Apr 02 '14 at 02:37
  • Thanks. Inside run method, I will TestA and TestB call right? And if I have more than two, I can do the same thing for them as well?> – AKIWEB Apr 02 '14 at 02:50
  • Yes. In your case you need create runnable for every implementation and submit every runnable. – zella Apr 02 '14 at 02:56