1.CountDownLatch-闭锁
package cn
.tedu
.concurrentLock
;
import java
.util
.concurrent
.CountDownLatch
;
public class CountDownLatchDemo {
public static void main(String
[] args
) throws Exception
{
CountDownLatch cdl
= new CountDownLatch(3);
new Thread(new P1(cdl
)).start();
new Thread(new P2(cdl
)).start();
new Thread(new P3(cdl
)).start();
cdl
.await();
new Thread(new P4()).start();
}
}
class P1 implements Runnable{
private CountDownLatch cdl
;
public P1(CountDownLatch cdl
){
this.cdl
= cdl
;
}
@Override
public void run() {
System
.out
.println("师傅准备好了");
cdl
.countDown();
}
}
class P2 implements Runnable{
private CountDownLatch cdl
;
public P2(CountDownLatch cdl
){
this.cdl
= cdl
;
}
@Override
public void run() {
try {
Thread
.sleep(5000);
} catch (InterruptedException e
) {
e
.printStackTrace();
}
System
.out
.println("打怪5秒,悟空准备好了");
cdl
.countDown();
}
}
class P3 implements Runnable{
private CountDownLatch cdl
;
public P3(CountDownLatch cdl
){
this.cdl
= cdl
;
}
@Override
public void run() {
try {
Thread
.sleep(8000);
} catch (InterruptedException e
) {
e
.printStackTrace();
}
System
.out
.println("吃饭8秒,八戒准备好了");
cdl
.countDown();
}
}
class P4 implements Runnable{
@Override
public void run() {
System
.out
.println("沙僧开始发牌");
}
}
2.栅栏CyclicBarrier
package cn
.tedu
.concurrentLock
;
import java
.util
.concurrent
.BrokenBarrierException
;
import java
.util
.concurrent
.CyclicBarrier
;
public class CyclicBarrierDemo {
public static void main(String
[] args
) {
CyclicBarrier cb
= new CyclicBarrier(2);
new Thread(new Horse1(cb
)).start();
new Thread(new Horse2(cb
)).start();
}
}
class Horse1 implements Runnable{
private CyclicBarrier cb
;
public Horse1(CyclicBarrier cb
){
this.cb
= cb
;
}
@Override
public void run() {
try {
Thread
.sleep(5000);
System
.out
.println("1号马准备好了");
cb
.await();
System
.out
.println("开始跑");
} catch (InterruptedException e
) {
e
.printStackTrace();
} catch (BrokenBarrierException e
) {
e
.printStackTrace();
}
}
}
class Horse2 implements Runnable{
private CyclicBarrier cb
;
public Horse2(CyclicBarrier cb
){
this.cb
= cb
;
}
@Override
public void run() {
try {
System
.out
.println("2号马准备好了");
cb
.await();
System
.out
.println("开始跑");
} catch (InterruptedException e
) {
e
.printStackTrace();
} catch (BrokenBarrierException e
) {
e
.printStackTrace();
}
}
}
3.Exchanger交换机
package cn
.tedu
.concurrentLock
;
import java
.util
.concurrent
.Exchanger
;
public class ExchangerDemo {
public static void main(String
[] args
) {
Exchanger
<String> ex
= new Exchanger<String>();
new Thread(new Spy1(ex
)).start();
new Thread(new Spy2(ex
)).start();
}
}
class Spy1 implements Runnable{
private Exchanger ex
;
public Spy1(Exchanger ex
){
this.ex
= ex
;
}
@Override
public void run() {
try {
String info
= "回眸一笑";
String result
= (String
)ex
.exchange(info
);
System
.out
.println("spy1收到spy2的暗号:"+result
);
} catch (InterruptedException e
) {
e
.printStackTrace();
}
}
}
class Spy2 implements Runnable{
private Exchanger ex
;
public Spy2(Exchanger ex
){
this.ex
= ex
;
}
@Override
public void run() {
try {
String info
= "寸草不生";
String result
= (String
)ex
.exchange(info
);
System
.out
.println("spy2收到spy1的暗号:"+result
);
} catch (InterruptedException e
) {
e
.printStackTrace();
}
}
}
转载请注明原文地址: https://mac.8miu.com/read-495756.html