forked from JAVA-000/JAVA-000
-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
2a8a725
commit 482b66c
Showing
12 changed files
with
561 additions
and
0 deletions.
There are no files selected for viewing
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,24 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.marcus</groupId> | ||
<artifactId>Week_04</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>8</source> | ||
<target>8</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
</project> |
40 changes: 40 additions & 0 deletions
40
Week_04/src/main/java/homework/myHomework/CompletableFutureMethod.java
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,40 @@ | ||
package homework.myHomework; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
/** | ||
* 本周作业:(必做)思考有多少种方式,在main函数启动一个新线程或线程池, | ||
* 异步运行一个方法,拿到这个方法的返回值后,退出主线程? | ||
* 写出你的方法,越多越好,提交到github。 | ||
* | ||
* 一个简单的代码参考: | ||
*/ | ||
public class CompletableFutureMethod { | ||
public static void main(String[] args) { | ||
|
||
long start=System.currentTimeMillis(); | ||
// 在这里创建一个线程或线程池, | ||
// 异步执行 下面方法 | ||
|
||
int result = CompletableFuture.supplyAsync(() -> sum()).join(); //这是得到的返回值 | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
// 确保 拿到result 并输出 | ||
System.out.println("异步计算结果为:"+result); | ||
|
||
System.out.println("使用时间:"+ (System.currentTimeMillis()-start) + " ms"); | ||
|
||
// 然后退出main线程 | ||
} | ||
|
||
private static int sum() { | ||
return fibo(36); | ||
} | ||
|
||
public static int fibo(int a) { | ||
if ( a < 2) | ||
return 1; | ||
return fibo(a-1) + fibo(a-2); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
Week_04/src/main/java/homework/myHomework/CountDownLatchMethod.java
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,48 @@ | ||
package homework.myHomework; | ||
|
||
import homework.template.Homework03; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
/** | ||
* 本周作业:(必做)思考有多少种方式,在main函数启动一个新线程或线程池, | ||
* 异步运行一个方法,拿到这个方法的返回值后,退出主线程? | ||
* 写出你的方法,越多越好,提交到github。 | ||
* | ||
* 一个简单的代码参考: | ||
*/ | ||
public class CountDownLatchMethod { | ||
private int sum; | ||
|
||
public static void main(String[] args) throws ExecutionException, InterruptedException { | ||
|
||
long start=System.currentTimeMillis(); | ||
// 在这里创建一个线程或线程池, | ||
// 异步执行 下面方法 | ||
CountDownLatchMethod countDownLatchMethod = new CountDownLatchMethod(); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
CountDownLatch countDownLatch = new CountDownLatch(1); | ||
|
||
Thread thread = new Thread(() -> { | ||
countDownLatchMethod.setSum(Homework03.fibo(36)); | ||
countDownLatch.countDown(); | ||
}); | ||
thread.start(); | ||
countDownLatch.await(); | ||
int result = countDownLatchMethod.getSum();//这是得到的返回值 | ||
|
||
// 确保 拿到result 并输出 | ||
System.out.println("异步计算结果为:"+result); | ||
|
||
System.out.println("使用时间:"+ (System.currentTimeMillis()-start) + " ms"); | ||
|
||
// 然后退出main线程 | ||
} | ||
public int getSum() { | ||
return sum; | ||
} | ||
|
||
private void setSum(int sum) { | ||
this.sum = sum; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
Week_04/src/main/java/homework/myHomework/CyclicBarrierMethod.java
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,57 @@ | ||
package homework.myHomework; | ||
|
||
import homework.template.Homework03; | ||
|
||
import java.util.concurrent.BrokenBarrierException; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.CyclicBarrier; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
/** | ||
* 本周作业:(必做)思考有多少种方式,在main函数启动一个新线程或线程池, | ||
* 异步运行一个方法,拿到这个方法的返回值后,退出主线程? | ||
* 写出你的方法,越多越好,提交到github。 | ||
* | ||
* 一个简单的代码参考: | ||
*/ | ||
public class CyclicBarrierMethod { | ||
private int sum; | ||
|
||
public static void main(String[] args) throws ExecutionException, InterruptedException { | ||
|
||
long start=System.currentTimeMillis(); | ||
// 在这里创建一个线程或线程池, | ||
// 异步执行 下面方法 | ||
CyclicBarrierMethod cyclicBarrierMethod = new CyclicBarrierMethod(); | ||
CyclicBarrier cyclicBarrier = new CyclicBarrier(1,() ->{ | ||
This comment has been minimized.
Sorry, something went wrong. |
||
int result = cyclicBarrierMethod.getSum();//这是得到的返回值 | ||
|
||
// 确保 拿到result 并输出 | ||
System.out.println("异步计算结果为:"+result); | ||
|
||
System.out.println("使用时间:"+ (System.currentTimeMillis()-start) + " ms"); | ||
}); | ||
|
||
Thread thread = new Thread(() -> { | ||
try { | ||
cyclicBarrierMethod.setSum(Homework03.fibo(36)); | ||
cyclicBarrier.await(); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} catch (BrokenBarrierException e) { | ||
e.printStackTrace(); | ||
} | ||
}); | ||
thread.start(); | ||
|
||
// 然后退出main线程 | ||
} | ||
public int getSum() { | ||
return sum; | ||
} | ||
|
||
private void setSum(int sum) { | ||
this.sum = sum; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
Week_04/src/main/java/homework/myHomework/FutureMethod.java
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,46 @@ | ||
package homework.myHomework; | ||
|
||
import homework.template.Homework03; | ||
|
||
import java.util.concurrent.*; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
/** | ||
* 本周作业:(必做)思考有多少种方式,在main函数启动一个新线程或线程池, | ||
* 异步运行一个方法,拿到这个方法的返回值后,退出主线程? | ||
* 写出你的方法,越多越好,提交到github。 | ||
* <p> | ||
* 一个简单的代码参考: | ||
*/ | ||
public class FutureMethod { | ||
|
||
public static void main(String[] args) throws ExecutionException, InterruptedException { | ||
|
||
long start = System.currentTimeMillis(); | ||
// 在这里创建一个线程或线程池, | ||
// 异步执行 下面方法 | ||
|
||
ExecutorService executorService = Executors.newSingleThreadExecutor(); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
Future<Integer> fiboFuture = executorService.submit(() -> Homework03.fibo(36)); | ||
executorService.shutdown(); | ||
int result = fiboFuture.get();//这是得到的返回值 | ||
|
||
// 确保 拿到result 并输出 | ||
System.out.println("异步计算结果为:" + result); | ||
|
||
System.out.println("使用时间:" + (System.currentTimeMillis() - start) + " ms"); | ||
|
||
// 然后退出main线程 | ||
} | ||
|
||
|
||
private int sum() { | ||
return fibo(36); | ||
} | ||
|
||
private int fibo(int a) { | ||
if (a < 2) | ||
return 1; | ||
return fibo(a - 1) + fibo(a - 2); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
Week_04/src/main/java/homework/myHomework/FutureTaskMethod.java
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,44 @@ | ||
package homework.myHomework; | ||
|
||
import homework.template.Homework03; | ||
|
||
import java.util.concurrent.*; | ||
|
||
/** | ||
* 本周作业:(必做)思考有多少种方式,在main函数启动一个新线程或线程池, | ||
* 异步运行一个方法,拿到这个方法的返回值后,退出主线程? | ||
* 写出你的方法,越多越好,提交到github。 | ||
* <p> | ||
* 一个简单的代码参考: | ||
*/ | ||
public class FutureTaskMethod { | ||
|
||
public static void main(String[] args) throws ExecutionException, InterruptedException { | ||
|
||
long start = System.currentTimeMillis(); | ||
// 在这里创建一个线程或线程池, | ||
// 异步执行 下面方法 | ||
FutureTask<Integer> task = new FutureTask(() -> Homework03.fibo(36)); | ||
Thread thread = new Thread(task); | ||
thread.start(); | ||
int result = task.get();//这是得到的返回值 | ||
|
||
// 确保 拿到result 并输出 | ||
System.out.println("异步计算结果为:" + result); | ||
|
||
System.out.println("使用时间:" + (System.currentTimeMillis() - start) + " ms"); | ||
|
||
// 然后退出main线程 | ||
} | ||
|
||
|
||
private int sum() { | ||
return fibo(36); | ||
} | ||
|
||
private int fibo(int a) { | ||
if (a < 2) | ||
return 1; | ||
return fibo(a - 1) + fibo(a - 2); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
Week_04/src/main/java/homework/myHomework/LockCondition.java
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,87 @@ | ||
package homework.myHomework; | ||
|
||
import java.util.concurrent.CyclicBarrier; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.concurrent.locks.Condition; | ||
import java.util.concurrent.locks.Lock; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
/** | ||
* 本周作业:(必做)思考有多少种方式,在main函数启动一个新线程或线程池, | ||
* 异步运行一个方法,拿到这个方法的返回值后,退出主线程? | ||
* 写出你的方法,越多越好,提交到github。 | ||
* <p> | ||
* 一个简单的代码参考: | ||
*/ | ||
public class LockCondition { | ||
private Lock lock ; | ||
private Condition condition; | ||
private int result = 0; | ||
|
||
public LockCondition() { | ||
this.lock = new ReentrantLock(); | ||
this.condition = lock.newCondition(); | ||
} | ||
|
||
public Condition getCondition() { | ||
return condition; | ||
} | ||
|
||
public Lock getLock() { | ||
return lock; | ||
} | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
LockCondition lockCondition = new LockCondition(); | ||
|
||
long start=System.currentTimeMillis(); | ||
// 在这里创建一个线程或线程池, | ||
// 异步执行 下面方法 | ||
|
||
Lock lock = lockCondition.getLock(); | ||
|
||
new Thread(() -> { | ||
lock.lock(); | ||
try { | ||
lockCondition.getCondition().await(); | ||
int result = lockCondition.result; | ||
// 确保 拿到result 并输出 | ||
System.out.println("异步计算结果为:" + result); | ||
|
||
System.out.println("使用时间:" + (System.currentTimeMillis() - start) + " ms"); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
lock.unlock(); | ||
} | ||
|
||
}).start(); | ||
TimeUnit.SECONDS.sleep(1); | ||
|
||
lockCondition.getResult(); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
// 然后退出main线程 | ||
} | ||
|
||
private void getResult() { | ||
lock.lock(); | ||
try { | ||
result = sum(); | ||
condition.signalAll(); | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
|
||
private int sum() { | ||
return fibo(36); | ||
} | ||
|
||
public int fibo(int a) { | ||
if ( a < 2) | ||
return 1; | ||
return fibo(a-1) + fibo(a-2); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
Week_04/src/main/java/homework/myHomework/ManualSpinMethod.java
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,43 @@ | ||
package homework.myHomework; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
public class ManualSpinMethod { | ||
|
||
private int sum; | ||
|
||
private void setSum() { | ||
this.sum = fibo(36); | ||
} | ||
|
||
private int getSum() throws InterruptedException { | ||
return sum; | ||
} | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
|
||
long start=System.currentTimeMillis(); | ||
// 在这里创建一个线程或线程池, | ||
// 异步执行 下面方法 | ||
ManualSpinMethod manualSpinMethod = new ManualSpinMethod(); | ||
|
||
new Thread(manualSpinMethod::setSum).start(); | ||
|
||
int result; //这是得到的返回值 | ||
while ((result = manualSpinMethod.getSum()) == 0) { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
TimeUnit.MILLISECONDS.sleep(10); | ||
} | ||
// 确保 拿到result 并输出 | ||
System.out.println("异步计算结果为:"+result); | ||
|
||
System.out.println("使用时间:"+ (System.currentTimeMillis()-start) + " ms"); | ||
|
||
// 然后退出main线程 | ||
} | ||
|
||
public static int fibo(int a) { | ||
if ( a < 2) | ||
return 1; | ||
return fibo(a-1) + fibo(a-2); | ||
} | ||
} |
Oops, something went wrong.
1 comment
on commit 482b66c
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.
很不错,基本方法都有覆盖了。继续加油,写的很认真
CompletableFuture good