使用Concurrent vs Linear Program在Result中没有区别

时间:2016-05-31 14:00:41

标签: java multithreading performance concurrency executorservice

我是并发Api的新手,我只是制作程序并使用Linear和并发方式测试它。当我看到结果时,两个结果是相同的,并且执行时间没有差异。 代码如下: -

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class TestFuture {

    private static final ExecutorService threadpool = Executors.newFixedThreadPool(3);
    public static void main(String[] a) throws InterruptedException, ExecutionException{
        TestFuture testFuture=new TestFuture();
        testFuture.testFutureTask();
        Date start=new Date();
        System.out.println("Start time :: "+start);
        TestLinear testLinear=new TestLinear();
    for (int i = 0; i < 3; i++) {
        if(i==0){
            testLinear.getFirstOutput();
            System.out.println("time to done by every thread :: one "+new Date());
        }else if(i==1){
            testLinear.getSecondOutput();
            System.out.println("time to done by every thread :: second "+new Date());
        }else if(i==2){
            testLinear.getThirdOutput();
            System.out.println("time to done by every thread :: third "+new Date());
        }
    }
        Date end=new Date();
        System.out.println("End time :: "+end);
        System.out.println("Difference :: "+((end.getTime()-start.getTime())/1000));
    }

    public void testFutureTask() throws InterruptedException, ExecutionException{
        Date start=new Date();
        System.out.println("Future Start time :: "+start);
        List<Future<String>> futureList=new ArrayList<Future<String>>();
        CallableTest callableTest=null;
        ResultClassTest resultClassTest=new ResultClassTest();
        for (int i = 0; i < 3; i++) {
            callableTest=new CallableTest();
            callableTest.setThreadWork(""+i);
            callableTest.setResultClassTest(resultClassTest);
            Future<String> future=threadpool.submit(callableTest);
            futureList.add(future);
        }
        threadpool.shutdown();

        for (Future<String> future : futureList) {
            System.out.println("time to done by every thread :: "+future.get()+" "+new Date());
        }
        System.out.println("result in Test future Result1 :: "+callableTest.getResultClassTest().getResult1());
        System.out.println("result in Test future Result2 :: "+callableTest.getResultClassTest().getResult2());
        System.out.println("result in Test future Result3 :: "+callableTest.getResultClassTest().getResult3());
        Date end=new Date();
        System.out.println("Future End time :: "+end);
        System.out.println("Difference Future :: "+((end.getTime()-start.getTime())/1000));
    }
}



import java.util.concurrent.Callable;

public class CallableTest implements Callable<String> {
    private String threadWork;
    private String result;
    private ResultClassTest resultClassTest;

    public String getThreadWork() {
        return threadWork;
    }

    public void setThreadWork(String threadWork) {
        this.threadWork = threadWork;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public ResultClassTest getResultClassTest() {
        return resultClassTest;
    }

    public void setResultClassTest(ResultClassTest resultClassTest) {
        this.resultClassTest = resultClassTest;
    }

    @Override
    public String call() throws Exception {
        if(threadWork!=null){
            if(threadWork.equals("0")){
                this.getResultClassTest().setResult1(this.getFirstOutput());                
                this.setResult("one");
            }else if(threadWork.equals("1")){
                this.getResultClassTest().setResult2(this.getSecondOutput());
                this.setResult("two");
            }else if(threadWork.equals("2")){
                this.getResultClassTest().setResult3(this.getThirdOutput());
                this.setResult("three");
            }
        }
        return result;
    }

    private Long getThirdOutput() {
        System.out.println("In Third");
        Long a=0l;
        for (long i = 400001; i < 600000000l; i++) {
            a+=i;
        }
        for (long i = 200001; i < 400000000l; i++) {
            a+=i;
        }
        for (long i = 200001; i < 400000000l; i++) {
            a+=i;
        }
        System.out.println("three "+a);
        return a;
    }

    private Long getSecondOutput() {
        System.out.println("In Second");
        Long a=0l;
        for (long i = 200001; i < 400000000l; i++) {
            a+=i;
        }
        for (long i = 200001; i < 400000000l; i++) {
            a+=i;
        }
        for (long i = 200001; i < 400000000l; i++) {
            a+=i;
        }
        System.out.println("two "+a);
        return a;
    }

    private Long getFirstOutput() {
        System.out.println("In First");
        Long a=0l;
        for (long i = 0; i < 200000000l; i++) {
            a+=i;
        }
        for (long i = 200001; i < 400000000l; i++) {
            a+=i;
        }
        for (long i = 200001; i < 400000000l; i++) {
            a+=i;
        }
        System.out.println("one "+a);
        return a;
    }

}



public class ResultClassTest {

    private Long result1;
    private Long result2;
    private Long result3;

    public Long getResult1() {
        return result1;
    }

    public void setResult1(Long result1) {
        this.result1 = result1;
    }

    public Long getResult2() {
        return result2;
    }

    public void setResult2(Long result2) {
        this.result2 = result2;
    }

    public Long getResult3() {
        return result3;
    }

    public void setResult3(Long result3) {
        this.result3 = result3;
    }

}



public class TestLinear {

    public Long getThirdOutput() {
        System.out.println("In Third");
        Long a=0l;
        for (long i = 400001; i < 600000000l; i++) {
            a+=i;
        }
        for (long i = 200001; i < 400000000l; i++) {
            a+=i;
        }
        for (long i = 200001; i < 400000000l; i++) {
            a+=i;
        }
        System.out.println("three "+a);
        return a;
    }

    public Long getSecondOutput() {
        System.out.println("In Second");
        Long a=0l;
        for (long i = 200001; i < 400000000l; i++) {
            a+=i;
        }
        for (long i = 200001; i < 400000000l; i++) {
            a+=i;
        }
        for (long i = 200001; i < 400000000l; i++) {
            a+=i;
        }
        System.out.println("two "+a);
        return a;
    }

    public Long getFirstOutput() {
        System.out.println("In First");
        Long a=0l;
        for (long i = 0; i < 200000000l; i++) {
            a+=i;
        }
        for (long i = 200001; i < 400000000l; i++) {
            a+=i;
        }
        for (long i = 200001; i < 400000000l; i++) {
            a+=i;
        }
        System.out.println("one "+a);
        return a;
    }

}

此测试包括四个类: TestFuture (主类), CallableTest (它实现Callable接口), ResultClassTest (结果类为并发执行)和 TestLinear (对于线性执行)。如果我做错了什么,请告诉我。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我运行了你的代码并获得了不同的结果。我使用System.currentTimeMillis();。

将开始/结束时间更改为长值
Future Start time :: 1464705132097
In First
In Second
In Third
one 179999959499800000
time to done by every thread :: one Tue May 31 10:32:17 EDT 2016
two 239999939399700000
time to done by every thread :: two Tue May 31 10:32:18 EDT 2016
three 339999879299600000
time to done by every thread :: three Tue May 31 10:32:18 EDT 2016
result in Test future Result1 :: 179999959499800000
result in Test future Result2 :: 239999939399700000
result in Test future Result3 :: 339999879299600000
Future End time :: 1464705138665
Difference Future :: 6568
Linear Start time :: 1464705138665
In First
one 179999959499800000
time to done by every thread :: one Tue May 31 10:32:21 EDT 2016
In Second
two 239999939399700000
time to done by every thread :: second Tue May 31 10:32:23 EDT 2016
In Third
three 339999879299600000
time to done by every thread :: third Tue May 31 10:32:27 EDT 2016
Linear End time :: 1464705147105
Difference :: 8440

如果您的计算机速度足够快,也许您只是出现了四舍五入错误?使用ms输出再次尝试。