-
线程交互
- import java.util.concurrent.CountDownLatch;
-
- public class LatchDriverDemo {
- private static final int N = 5;
- public static void main(String[] args) throws InterruptedException {
-
- CountDownLatch startSignal = new CountDownLatch(1);
-
- CountDownLatch doneSignal = new CountDownLatch(N);
- for (int i = 0; i < N; i++)
-
- new Thread(new LatchWorker(startSignal,doneSignal),"t"+i).start();
-
- long start = System.currentTimeMillis();
-
- startSignal.countDown();
-
- startSignal.await();
- long end = System.currentTimeMillis();
-
-
- System.out.println(end+" "+start);
- }
- }
-
- import java.util.concurrent.CountDownLatch;
-
- public class LatchWorker implements Runnable{
- private final CountDownLatch startSignal;
- private final CountDownLatch doneSignal;
- public LatchWorker(CountDownLatch startSignal,CountDownLatch doneSignal){
- super();
- this.startSignal = startSignal;
- this.doneSignal = doneSignal;
- }
- @Override
- public void run() {
- try {
- startSignal.await();
- dowork();
- doneSignal.countDown();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
-
- }
- private void dowork() {
-
- int a = 0 ;
- for (int i = 0; i < 10000; i++) {
- a += 1;
- }
- System.out.println(Thread.currentThread().getName()+"="+a);
- }
-
- }
-
相关阅读:
浅析HTT
html5移动端页面分辨率设置及相应字体大小设置的靠谱使用方式
CSS--posiion学习日记
面向对象程序设计课程感想
OO第二单元(5-8周)作业总结
OO第一作业周期(前四周)总结
OO第三阶段作业总结
OO第二单元作业总结
Java学习第一阶段作业总结
面向对象OO课程学习总结
-
原文地址:https://www.cnblogs.com/hold/p/3097083.html
Copyright © 2020-2023
润新知