如何使用多线程并行运行两个类? [英] How to run two classes in parallel using multithreading?

查看:318
本文介绍了如何使用多线程并行运行两个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目中,我有多个接口和两个Implementations类,需要实现这两个接口.

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;
}

其实现方式是-

public class TestA implements interfaceA {

    // abc method
}

我这样称呼它-

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

现在我的第二个界面是-

Now my second interface is -

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

其实现方式是-

public class TestB implements interfaceB {

    // xyz method   
}

我这样称呼它-

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

问题陈述:-

现在我的问题是-有什么办法可以并行执行这两个实现类?我不想按顺序运行它.

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.

意思是,我想并行运行TestATestB实现吗?这可能吗?

Meaning, I want to run TestA and TestB implementation in parallel? Is this possible to do?

推荐答案

当然可以.您实际上有很多选择.首选的方法是使用可调用和执行程序.

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);

此方法使您有机会从执行任务中获得结果. InvokeAll返回Future对象的列表.

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);
    }

如果使类实现Callable,则可以使代码更易于使用,然后将减少准备任务列表所需的代码量.让我们以TestB类为例:

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();
    }
}

那么您只需要

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

代替

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();
                }
            }
    );

此外,执行程序使您能够维护和重用Thread对象,这从性能和可维护性的角度来看都是很好的.

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

这篇关于如何使用多线程并行运行两个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆