如何使用多线程并行运行两个类?

AKI*_*WEB 4 java parallel-processing multithreading callable executorservice

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

假设我的第一个接口是 -

public Interface interfaceA {
    public String abc() throws Exception;
}
Run Code Online (Sandbox Code Playgroud)

它的实施是 -

public class TestA implements interfaceA {

    // abc method
}
Run Code Online (Sandbox Code Playgroud)

我这样称呼它 -

TestA testA = new TestA();
testA.abc();
Run Code Online (Sandbox Code Playgroud)

现在我的第二个界面是 -

public Interface interfaceB {
    public String xyz() throws Exception;
}
Run Code Online (Sandbox Code Playgroud)

它的实施是 -

public class TestB implements interfaceB {

    // xyz method   
}
Run Code Online (Sandbox Code Playgroud)

我这样称呼它 -

TestB testB = new TestB();
testB.xyz();
Run Code Online (Sandbox Code Playgroud)

问题陈述:-

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

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

ent*_*ios 8

当然有可能.你有很多选择.首选的是使用可调用和执行器.

    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);
Run Code Online (Sandbox Code Playgroud)

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

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

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

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

那你就需要了

Lists.newArrayList(new TestB(), new TestA());
Run Code Online (Sandbox Code Playgroud)

代替

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();
                }
            }
    );
Run Code Online (Sandbox Code Playgroud)

更重要的是,执行程序赋予您维护和重用Thread对象的能力,从性能和可维护性的角度来看,这是很好的.