forked from JAVA-000/JAVA-000
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
265 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.*; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class HomeWorkCountDownLatch { | ||
public static void main(String[] args) throws ExecutionException, InterruptedException { | ||
long start = System.currentTimeMillis(); | ||
final int size = 5; | ||
final CountDownLatch countDownLatch = new CountDownLatch(size); | ||
ExecutorService executorService = Executors.newCachedThreadPool(); | ||
List<Integer> futureList = new ArrayList<>(); | ||
for (int i = 0; i < size; i++) { | ||
Future<Integer> future = executorService.submit(new CountDownLatchFuture(countDownLatch)); | ||
futureList.add(future.get()); | ||
} | ||
|
||
try { | ||
countDownLatch.await(); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
executorService.shutdown(); | ||
System.out.println("使用时间:" + (System.currentTimeMillis() - start) + " ms"); | ||
System.out.println("执行后的结果: " + futureList.get(0)); | ||
} | ||
|
||
static class CountDownLatchFuture implements Callable { | ||
private AtomicInteger atomicInteger = new AtomicInteger(); | ||
private CountDownLatch countDownLatch; | ||
|
||
public CountDownLatchFuture(CountDownLatch countDownLatch) { | ||
this.countDownLatch = countDownLatch; | ||
} | ||
|
||
public AtomicInteger getAtomicInteger() { | ||
return atomicInteger; | ||
} | ||
|
||
@Override | ||
public Object call() throws Exception { | ||
try { | ||
atomicInteger.set(sum()); | ||
countDownLatch.countDown(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return atomicInteger; | ||
} | ||
} | ||
|
||
private static int sum() { | ||
return fibo(36); | ||
} | ||
|
||
private static int fibo(int a) { | ||
if (a < 2) | ||
return 1; | ||
return fibo(a - 1) + fibo(a - 2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.*; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class HomeWorkCyclicBarrier { | ||
|
||
public static void main(String[] args) throws ExecutionException, InterruptedException { | ||
final int size = 2; | ||
long start = System.currentTimeMillis(); | ||
final CyclicBarrier cyclicBarrier = new CyclicBarrier(size); | ||
List<Future> futureList = new ArrayList<>(); | ||
ExecutorService executorService = Executors.newFixedThreadPool(size); | ||
for (int i = 0; i < size; i++) { | ||
Future<Integer> future = executorService.submit(new CyclicBarrierCallable(cyclicBarrier)); | ||
futureList.add(future); | ||
} | ||
for (Future future : futureList) { | ||
System.out.println("jdk8CompletableFeature使用时间:" + (System.currentTimeMillis() - start) + " ms"); | ||
System.out.println("执行后的结果: " + future.get()); | ||
} | ||
executorService.shutdown(); | ||
} | ||
} | ||
|
||
class CyclicBarrierCallable implements Callable { | ||
private AtomicInteger atomicInteger = new AtomicInteger(); | ||
private CyclicBarrier cyclicBarrie; | ||
|
||
public CyclicBarrierCallable(CyclicBarrier cyclicBarrie) { | ||
this.cyclicBarrie = cyclicBarrie; | ||
} | ||
|
||
public AtomicInteger getAtomicInteger() { | ||
return atomicInteger; | ||
} | ||
|
||
@Override | ||
public Object call() throws Exception { | ||
try { | ||
atomicInteger.set(sum()); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} finally { | ||
cyclicBarrie.await(); | ||
} | ||
return atomicInteger; | ||
} | ||
|
||
private static int sum() { | ||
return fibo(36); | ||
} | ||
|
||
private static int fibo(int a) { | ||
if (a < 2) | ||
return 1; | ||
return fibo(a - 1) + fibo(a - 2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import java.util.concurrent.*; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class HomeWorkFeature { | ||
|
||
public static void main(String[] args) throws Exception { | ||
//创建一个线程池 | ||
ExecutorService executorService = Executors.newFixedThreadPool(1); | ||
//jdk8 | ||
futureByCompletableFeature(); | ||
//jdk5 | ||
futureByCallable(executorService); | ||
futureByRunnable(executorService); | ||
futureByFutureTask(); | ||
executorService.shutdown(); | ||
} | ||
|
||
private static void futureByCompletableFeature() throws InterruptedException, ExecutionException { | ||
long start = System.currentTimeMillis(); | ||
CompletableFuture completableFuture = CompletableFuture.supplyAsync(() -> { | ||
return sum(); | ||
}); | ||
System.out.println("jdk8CompletableFeature使用时间:" + (System.currentTimeMillis() - start) + " ms"); | ||
System.out.println("jdk8CompletableFeature异步计算结果为:" + completableFuture.get()); | ||
} | ||
|
||
private static void futureByCallable(ExecutorService executorService) throws InterruptedException, ExecutionException { | ||
long start = System.currentTimeMillis(); | ||
Future<Integer> future = executorService.submit(new Callable<Integer>() { | ||
@Override | ||
public Integer call() throws Exception { | ||
return sum(); | ||
} | ||
}); | ||
System.out.println("jdk5Callable使用时间:" + (System.currentTimeMillis() - start) + " ms"); | ||
System.out.println("jdk5Callable异步计算结果为:" + future.get()); | ||
} | ||
|
||
/** | ||
* @param executorService | ||
*/ | ||
private static void futureByRunnable(ExecutorService executorService) throws ExecutionException, InterruptedException { | ||
long start = System.currentTimeMillis(); | ||
AtomicInteger atomicInteger = new AtomicInteger(); | ||
Future<AtomicInteger> future = executorService.submit(new Runnable() { | ||
@Override | ||
public void run() { | ||
atomicInteger.set(sum()); | ||
} | ||
}, atomicInteger); | ||
System.out.println("jdk5Runnable使用时间:" + (System.currentTimeMillis() - start) + " ms"); | ||
System.out.println("jdk5Runnable异步计算结果为: " + future.get()); | ||
} | ||
|
||
private static void futureByFutureTask() throws InterruptedException, ExecutionException { | ||
HomeWorkTask homeWorkTask = new HomeWorkTask(); | ||
long start = System.currentTimeMillis(); | ||
FutureTask<Integer> futureTask = new FutureTask<>(homeWorkTask); | ||
System.out.println("jdk5futureTask使用时间:" + (System.currentTimeMillis() - start) + " ms"); | ||
System.out.println("jdk5futureTask异步计算结果为:" + futureTask.get()); | ||
} | ||
|
||
static class HomeWorkTask implements Callable<Integer> { | ||
@Override | ||
public Integer call() throws Exception { | ||
return sum(); | ||
} | ||
} | ||
|
||
private static int sum() { | ||
return fibo(36); | ||
} | ||
|
||
private static int fibo(int a) { | ||
if (a < 2) | ||
return 1; | ||
return fibo(a - 1) + fibo(a - 2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import java.util.Vector; | ||
|
||
public class HomeWorkJoin { | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
long start=System.currentTimeMillis(); | ||
Vector<Integer> vector = new Vector<>(); | ||
Thread thread = new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
vector.add(sum()); | ||
} | ||
}); | ||
thread.start(); | ||
thread.join(); | ||
System.out.println("使用时间:"+ (System.currentTimeMillis()-start) + " ms"); | ||
System.out.println("异步计算结果为:"+vector.get(0)); | ||
} | ||
|
||
|
||
private static int sum() { | ||
return fibo(36); | ||
} | ||
|
||
private static int fibo(int a) { | ||
if (a < 2) | ||
return 1; | ||
return fibo(a - 1) + fibo(a - 2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Semaphore; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class HomeWorkSemaphore { | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
long start = System.currentTimeMillis(); | ||
Semaphore semaphore = new Semaphore(1); | ||
AtomicInteger atomicInteger = new AtomicInteger(); | ||
Thread thread = new Thread(() -> { | ||
try { | ||
semaphore.acquire(); | ||
atomicInteger.set(sum()); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
semaphore.release(); | ||
} | ||
}); | ||
thread.start(); | ||
thread.join(); | ||
System.out.println("异步计算结果为:"+atomicInteger.get()); | ||
System.out.println("使用时间:" + (System.currentTimeMillis() - start) + " ms"); | ||
} | ||
|
||
private static int sum() { | ||
return fibo(36); | ||
} | ||
|
||
private static int fibo(int a) { | ||
if (a < 2) | ||
return 1; | ||
return fibo(a - 1) + fibo(a - 2); | ||
} | ||
} |
37e0089
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1.脑图画的很棒,总结的很好,点赞
2. 思考总结对比每种实现方式的优缺点