0

我是新来的并发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(它实现可赎回接口),ResultClassTest(对并发执行结果类)和TestLinear(用于线性执行)。如果我做错了什么,请告诉我。

在此先感谢。

回答

0

我跑你的代码,并得到不同的结果。我使用System.currentTimeMillis();将开始/结束时间改为long值。

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输出再试一次。

+0

感谢您的回复,我明白了您的观点,并在毫秒内也做到了这一点。以毫秒为单位的差异,但我希望差不多3至4秒的差异,但它不是同一种类型的,这就是为什么我要求它。 –

+0

你可能想看看这个线程:http://stackoverflow.com/questions/1647990/executorservices-surprising-performance-break-even-point-rules-of-thumb。基本上,需要考虑线程池的开销成本。 – Chill

+0

嗨寒意,我已经审查过,并检查代码,但无法找到任何太多的差异。将再次检查并回复给你。 –

相关问题