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
92b11b9
commit 00dc8e8
Showing
4 changed files
with
80 additions
and
7 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
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
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
56 changes: 56 additions & 0 deletions
56
Week_04/src/main/java/homework/myHomework/SemaphoreMethod.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,56 @@ | ||
package homework.myHomework; | ||
|
||
import java.util.concurrent.Semaphore; | ||
|
||
public class SemaphoreMethod { | ||
|
||
private int sum; | ||
final Semaphore semaphore = new Semaphore(1); | ||
|
||
private void setSum() { | ||
try { | ||
semaphore.acquire(); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
this.sum = fibo(36); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
}finally { | ||
semaphore.release(); | ||
} | ||
|
||
} | ||
|
||
private void printResult(long stratTime) { | ||
|
||
try { | ||
semaphore.acquire(); | ||
// 确保 拿到result 并输出 | ||
int result = sum; | ||
System.out.println("异步计算结果为:" + result); | ||
System.out.println("使用时间:"+ (System.currentTimeMillis()-stratTime) + " ms"); | ||
|
||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
semaphore.release(); | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
|
||
long start=System.currentTimeMillis(); | ||
// 在这里创建一个线程或线程池, | ||
// 异步执行 下面方法 | ||
SemaphoreMethod semaphoreMethod = new SemaphoreMethod(); | ||
|
||
new Thread(semaphoreMethod::setSum).start(); | ||
new Thread(() -> semaphoreMethod.printResult(start)).start(); | ||
|
||
// 然后退出main线程 | ||
} | ||
|
||
public static int fibo(int a) { | ||
if ( a < 2) | ||
return 1; | ||
return fibo(a-1) + fibo(a-2); | ||
} | ||
} |
1 comment
on commit 00dc8e8
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.
作业写法很多,可以参考其他同学的作业看看。继续加油小兄弟。
可以再简洁些