Skip to content

Commit

Permalink
第四周作业(必做题)
Browse files Browse the repository at this point in the history
  • Loading branch information
hhj0719 committed Nov 11, 2020
1 parent a2e4f42 commit 37e0089
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 0 deletions.
Binary file added Week_04/java并发编程.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 61 additions & 0 deletions Week_04/src/HomeWorkCountDownLatch.java
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);
}
}
59 changes: 59 additions & 0 deletions Week_04/src/HomeWorkCyclicBarrier.java
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);
}
}
79 changes: 79 additions & 0 deletions Week_04/src/HomeWorkFeature.java
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);
}
}
30 changes: 30 additions & 0 deletions Week_04/src/HomeWorkJoin.java
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);
}
}
36 changes: 36 additions & 0 deletions Week_04/src/HomeWorkSemaphore.java
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);
}
}

1 comment on commit 37e0089

@menghaoranss
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1.脑图画的很棒,总结的很好,点赞
2. 思考总结对比每种实现方式的优缺点

Please sign in to comment.