1.序列計算的模擬執行時間(time=532)
2.CompleteFuture平行計算(time=231)
3.Callable平行計算 (time=208)
Java8 多執行緒及平行計算demo
* https://www.cnblogs.com/oktokeep/p/16639417.html
需求背景:比如一個大的物件(userInfo),包含3個部分的集合等資料的計算,查詢等。可以啟動3個多執行緒來平行計算。最後計算完畢之後,組裝物件,平行計算完畢。
1.序列計算的模擬執行時間
package com.example.core.mydemo.bean; import com.example.core.mydemo.json2.GsonUtils; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * userInfo={"username":"劉德華","sex":"男","age":30},time=532 */ public class CompleteFutureBeforeTest { public static void main(String[] args) throws Exception{ long now = System.currentTimeMillis(); final UserInfo userInfo = new UserInfo(); getRemoteUserAndFill(1, userInfo); getRemoteBonusAndFill(1, userInfo); getRemoteGrowthAndFill(1, userInfo); long end = System.currentTimeMillis(); System.out.println("userInfo=" + GsonUtils.toJson(userInfo) + ",time=" + (end-now)); } private static void getRemoteUserAndFill(int i, UserInfo userInfo) throws Exception{ userInfo.setUsername("劉德華"); Thread.sleep(200); } private static void getRemoteBonusAndFill(int i, UserInfo userInfo) throws Exception{ userInfo.setSex("男"); Thread.sleep(150); } private static void getRemoteGrowthAndFill(int i, UserInfo userInfo) throws Exception{ userInfo.setAge(30); Thread.sleep(180); } }
2.CompleteFuture平行計算
package com.example.core.mydemo.bean; import com.example.core.mydemo.json2.GsonUtils; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * userInfo={"username":"劉德華","sex":"男","age":30},time=231 * * Java8 多執行緒及平行計算demo * https://www.cnblogs.com/oktokeep/p/16639417.html */ public class CompleteFutureTest { public static void main(String[] args) throws Exception{ long now = System.currentTimeMillis(); ExecutorService executorService = Executors.newFixedThreadPool(10); final UserInfo userInfo = new UserInfo(); CompletableFuture userFuture = CompletableFuture.supplyAsync(() -> { try { getRemoteUserAndFill(1, userInfo); } catch (Exception e) { e.printStackTrace(); } return Boolean.TRUE; }, executorService); CompletableFuture bonusFuture = CompletableFuture.supplyAsync(() -> { try { getRemoteBonusAndFill(1, userInfo); } catch (Exception e) { e.printStackTrace(); } return Boolean.TRUE; }, executorService); CompletableFuture growthFuture = CompletableFuture.supplyAsync(() -> { try { getRemoteGrowthAndFill(1, userInfo); } catch (Exception e) { e.printStackTrace(); } return Boolean.TRUE; }, executorService); CompletableFuture.allOf(userFuture, bonusFuture, growthFuture).join(); userFuture.get(); bonusFuture.get(); growthFuture.get(); long end = System.currentTimeMillis(); System.out.println("userInfo=" + GsonUtils.toJson(userInfo) + ",time=" + (end-now)); } private static void getRemoteUserAndFill(int i, UserInfo userInfo) throws Exception{ userInfo.setUsername("劉德華"); Thread.sleep(200); } private static void getRemoteBonusAndFill(int i, UserInfo userInfo) throws Exception{ userInfo.setSex("男"); Thread.sleep(150); } private static void getRemoteGrowthAndFill(int i, UserInfo userInfo) throws Exception{ userInfo.setAge(30); Thread.sleep(180); } }
3.Callable平行計算
package com.example.core.mydemo.bean.callable; import com.example.core.mydemo.bean.UserInfo; import java.util.concurrent.Callable; public class UserAndFill implements Callable<UserInfo> { UserInfo userInfo; public UserAndFill(UserInfo userInfo) { this.userInfo = userInfo; } @Override public UserInfo call() throws Exception { userInfo.setUsername("劉德華"); Thread.sleep(200); return userInfo; } }
package com.example.core.mydemo.bean.callable; import com.example.core.mydemo.bean.UserInfo; import java.util.concurrent.Callable; public class BonusAndFill implements Callable<UserInfo> { UserInfo userInfo; public BonusAndFill(UserInfo userInfo) { this.userInfo = userInfo; } @Override public UserInfo call() throws Exception { userInfo.setSex("男"); Thread.sleep(150); return userInfo; } }
package com.example.core.mydemo.bean.callable; import com.example.core.mydemo.bean.UserInfo; import java.util.concurrent.Callable; public class GrowthAndFill implements Callable<UserInfo> { UserInfo userInfo; public GrowthAndFill(UserInfo userInfo) { this.userInfo = userInfo; } @Override public UserInfo call() throws Exception { userInfo.setAge(30); Thread.sleep(180); return userInfo; } }
package com.example.core.mydemo.bean; import com.example.core.mydemo.bean.callable.BonusAndFill; import com.example.core.mydemo.bean.callable.GrowthAndFill; import com.example.core.mydemo.bean.callable.UserAndFill; import com.example.core.mydemo.json2.GsonUtils; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class CallableTest { public static void main(String[] args) throws Exception{ long now = System.currentTimeMillis(); UserInfo userInfo = new UserInfo(); ExecutorService executorService = Executors.newFixedThreadPool(10); /** * userInfo={"username":"劉德華","sex":"男","age":30},time=539 * 類似序列了 */ /* UserAndFill userAndFill = new UserAndFill(userInfo); Future<UserInfo> future1 = executorService.submit(userAndFill); userInfo = future1.get(); BonusAndFill bonusAndFill = new BonusAndFill(userInfo); Future<UserInfo> future2 = executorService.submit(bonusAndFill); userInfo = future2.get(); GrowthAndFill growthAndFil = new GrowthAndFill(userInfo); Future<UserInfo> future3 = executorService.submit(growthAndFil); userInfo = future3.get();*/ /** * userInfo={"username":"劉德華","sex":"男","age":30},time=208 */ UserAndFill userAndFill = new UserAndFill(userInfo); BonusAndFill bonusAndFill = new BonusAndFill(userInfo); GrowthAndFill growthAndFil = new GrowthAndFill(userInfo); Future<UserInfo> future1 = executorService.submit(userAndFill); Future<UserInfo> future2 = executorService.submit(bonusAndFill); Future<UserInfo> future3 = executorService.submit(growthAndFil); userInfo = future1.get(); userInfo = future2.get(); userInfo = future3.get(); executorService.shutdownNow(); long end = System.currentTimeMillis(); System.out.println("userInfo=" + GsonUtils.toJson(userInfo) + ",time=" + (end-now)); } }