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

嗨寒意,我已經審查過,並檢查代碼,但無法找到任何太多的差異。將再次檢查並回復給你。 –

相關問題