Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run two classes in parallel using multithreading?

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 String 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 String 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?

like image 515
AKIWEB Avatar asked Dec 26 '22 11:12

AKIWEB


1 Answers

Sure it is possible. You have actually many options. Preferred one is using callable and executors.

    final ExecutorService executorService = Executors.newFixedThreadPool(2);
    final ArrayList<Callable<String>> tasks = Lists.newArrayList(
            new Callable<String>()
            {
                @Override
                public String call() throws Exception
                {
                    return testA.abc();
                }
            },
            new Callable<String>()
            {
                @Override
                public String call() throws Exception
                {
                    return testB.xyz();
                }
            }
    );

    executorService.invokeAll(tasks);

This method gives you opportunity to get a result from executions of your tasks. InvokeAll returns a list of Future objects.

    final List<Future<String>> futures = executorService.invokeAll(tasks);
    for (Future<String> future : futures)
    {
        final String resultOfTask = future.get();
        System.out.println(resultOfTask);
    }

You can make your code easier to use if you make your classes implements Callable, then you will reduce amount of code needed to prepare list of tasks. Let's use TestB class as an example:

public interface interfaceB {
    String xyz() throws Exception;
}

public class TestB implements interfaceB, Callable<String>{

    @Override
    public String xyz() throws Exception
    {
        //do something
        return "xyz"; 
    }

    @Override
    public String call() throws Exception
    {
        return xyz();
    }
}

Then you will need just

Lists.newArrayList(new TestB(), new TestA());

instead of

final ArrayList<Callable<String>> tasks = Lists.newArrayList(
            new Callable<String>()
            {
                @Override
                public String call() throws Exception
                {
                    return testA.abc();
                }
            },
            new Callable<String>()
            {
                @Override
                public String call() throws Exception
                {
                    return testB.xyz();
                }
            }
    );

Whats more, executors gives you power to maintain and reuse Thread objects which is good from performance and maintainability perspective.

like image 58
enterbios Avatar answered Dec 28 '22 07:12

enterbios