【问题标题】:No difference in Result using Concurrent vs Linear Program使用并发程序与线性程序的结果没有差异
【发布时间】:2016-09-29 14:15:45
【问题描述】:

我是并发 Api 的新手,我只是制作程序并使用线性和并发方式对其进行测试。当我看到结果时,两个结果是相同的,执行时间没有区别。 代码如下:-

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(Result Class for并发执行)和TestLinear(用于线性执行)。如果我做错了什么,请告诉我。

提前致谢。

【问题讨论】:

    标签: java multithreading performance concurrency executorservice


    【解决方案1】:

    我运行了您的代码并得到了不同的结果。我使用 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 输出再试一次。

    【讨论】:

    • 感谢您的回复,我明白了您的意思,并在毫秒内完成了同样的操作。以毫秒为单位有一些差异,但我希望几乎有 3 到 4 秒的差异,但它不是同一种,这就是我要求它的原因。
    • 你可能想看看这个帖子:stackoverflow.com/questions/1647990/…。基本上,需要考虑线程池的开销成本。
    • 您好,Chill,我已经查看了它并检查了代码,但没有发现任何太大的区别。将再次检查并回复您。
    猜你喜欢
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 2019-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    相关资源
    最近更新 更多