package com.itheima.mycountDown;
import java.util.concurrent.CountDownLatch;
public class MotherThead extends Thread{
private CountDownLatch countDownLatch;
public MotherThead(CountDownLatch countDownLatch) {
this.countDownLatch=countDownLatch;
}
@Override
public void run() {
try {
//
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("妈妈在收拾碗筷");
}
}
package com.itheima.mycountDown;
import java.util.concurrent.CountDownLatch;
public class ChileThread1 extends Thread{
private CountDownLatch countDownLatch;
public ChileThread1(CountDownLatch countDownLatch) {
this.countDownLatch=countDownLatch;
}
@Override
public void run() {
for (int i = 0; i <= 10; i++) {
System.out.println(getName()+"在吃第"+i+"个饺子");
}
countDownLatch.countDown();
}
}
package com.itheima.mycountDown;
import java.util.concurrent.CountDownLatch;
public class ChileThread2 extends Thread{
private CountDownLatch countDownLatch;
public ChileThread2(CountDownLatch countDownLatch) {
this.countDownLatch=countDownLatch;
}
@Override
public void run() {
for (int i = 0; i <= 15; i++) {
System.out.println(getName()+"在吃第"+i+"个饺子");
}
countDownLatch.countDown();
}
}
package com.itheima.mycountDown;
import java.util.concurrent.CountDownLatch;
public class ChileThread3 extends Thread{
private CountDownLatch countDownLatch;
public ChileThread3(CountDownLatch countDownLatch) {
this.countDownLatch=countDownLatch;
}
@Override
public void run() {
for (int i = 0; i <= 20; i++) {
System.out.println(getName()+"在吃第"+i+"个饺子");
}
countDownLatch.countDown();
}
}
package com.itheima.mycountDown;
import java.util.concurrent.CountDownLatch;
public class MyCountDownLatchDemo {
public static void main(String[] args) {
CountDownLatch countDownLatch=new CountDownLatch(3);
MotherThead motherThead=new MotherThead(countDownLatch);
motherThead.start();
ChileThread1 t1=new ChileThread1(countDownLatch);
t1.setName("小明");
ChileThread2 t2=new ChileThread2(countDownLatch);
t2.setName("小红");
ChileThread3 t3=new ChileThread3(countDownLatch);
t3.setName("小刚");
t1.start();
t2.start();
t3.start();
}
}