1、join
模拟现在有三个worker,采用join的方法控制:
package org.pbccrc.org.pbccrc.thread;
public class Test {
public static void main(String[] args) throws InterruptedException {
Worker worker1=new Worker("第一个任务", (int)(Math.random()*1000+2000));
Worker worker2=new Worker("第二个任务", (int)(Math.random()*1000+2000));
Worker worker3=new Worker("第三个任务", (int)(Math.random()*1000+2000));
Thread thread1 =new Thread(worker1);
Thread thread2 =new Thread(worker2);
Thread thread3 =new Thread(worker3);
thread1.start();
thread2.start();
thread1.join();
thread2.join();
System.out.println("wait thread1 and thread2 complete :");
thread3.start();
thread3.join();
System.out.println("three threads have complete!");
}
}
package org.pbccrc.org.pbccrc.thread;
public class Worker implements Runnable {
private String name;
private int time;
public Worker(String name, int time) {
super();
this.name = name;
this.time = time;
}
@Override
public void run() {
System.out.println("task :"+name+":is running!");
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("task :"+name+":complete!");
}
}
task :第一个任务:is running!
task :第二个任务:is running!
task :第二个任务:complete!
task :第一个任务:complete!
wait thread1 and thread2 complete :
task :第三个任务:is running!
task :第三个任务:complete!
three threads have complete!
2、Countdownlatch
采用countDownlatch控制:
package org.pbccrc.org.pbccrc.thread;
import java.util.concurrent.CountDownLatch;
public class Test {
public static void main(String[] args) throws InterruptedException {
CountDownLatch latch=new CountDownLatch(2);
Worker worker1=new Worker("第一个任务", (int)(Math.random()*1000+2000),latch);
Worker worker2=new Worker("第二个任务", (int)(Math.random()*1000+2000),latch);
Worker worker3=new Worker("第三个任务", (int)(Math.random()*1000+2000),latch);
Thread thread1 =new Thread(worker1);
Thread thread2 =new Thread(worker2);
Thread thread3 =new Thread(worker3);
thread1.start();
thread2.start();
latch.await();
System.out.println("wait thread1 and thread2 complete :");
thread3.start();
thread3.join();
System.out.println("three threads have complete!");
}
}
package org.pbccrc.org.pbccrc.thread;
import java.util.concurrent.CountDownLatch;
public class Worker implements Runnable {
private String name;
private int time;
private CountDownLatch latch;
public Worker(String name, int time,CountDownLatch latch) {
super();
this.name = name;
this.time = time;
this.latch = latch;
}
@Override
public void run() {
System.out.println("task :"+name+":is running!");
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("task :"+name+":complete!");
latch.countDown();
}
}
task :第二个任务:is running!
task :第一个任务:is running!
task :第二个任务:complete!
task :第一个任务:complete!
wait thread1 and thread2 complete :
task :第三个任务:is running!
task :第三个任务:complete!
three threads have complete!
我们发现好像join与countdownlatch没有区别:看下面的例子吧:
3、分阶段控制使用countdownlatch
package org.pbccrc.org.pbccrc.thread;
import java.util.concurrent.CountDownLatch;
public class Worker implements Runnable {
private String name;
private int time;
private CountDownLatch latch;
public Worker(String name, int time,CountDownLatch latch) {
super();
this.name = name;
this.time = time;
this.latch = latch;
}
@Override
public void run() {
System.out.println("task :"+name+":is running for first process!");
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
latch.countDown();
try {
System.out.println("task :"+name+":is begin for second process!");
Thread.sleep(time);
System.out.println("task :"+name+":is complete for second process!");
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("task :"+name+":complete!");
}
}
task :第一个任务:is running for first process!
task :第二个任务:is running for first process!
task :第一个任务:is begin for second process!
task :第二个任务:is begin for second process!
wait thread1 and thread2 complete :
task :第三个任务:is running for first process!
task :第一个任务:is complete for second process!
task :第一个任务:complete!
task :第二个任务:is complete for second process!
task :第二个任务:complete!
task :第三个任务:is begin for second process!
task :第三个任务:is complete for second process!
task :第三个任务:complete!
three threads have complete!
区别在:线程执行过程中我们可以使用countdownlatch的方法countDown;countdownlatch是减数器,当数量为0时会唤醒其他的线程执行;
4、CyclicBarrier
public class Test {
public static void main(String[] args) throws InterruptedException, BrokenBarrierException {
CyclicBarrier barrier=new CyclicBarrier(3);
Worker worker1=new Worker("第一个任务", (int)(Math.random()*1000+2000),barrier);
Worker worker2=new Worker("第二个任务", (int)(Math.random()*1000+2000),barrier);
Worker worker3=new Worker("第三个任务", (int)(Math.random()*1000+2000),barrier);
Thread thread1 =new Thread(worker1);
Thread thread2 =new Thread(worker2);
Thread thread3 =new Thread(worker3);
thread1.start();
thread2.start();
thread3.start();
}
}
public class Worker implements Runnable {
private String name;
private int time;
private CyclicBarrier barrier;
public Worker(String name, int time,CyclicBarrier barrier) {
super();
this.name = name;
this.time = time;
this.barrier = barrier;
}
@Override
public void run() {
try {
Thread.sleep(time);
System.out.println("task :"+name+":has already!");
barrier.await();
System.out.println("task :"+name+":run !");
Thread.sleep(time);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("task :"+name+":complete!");
}
}
task :第二个任务:has already!
task :第一个任务:has already!
task :第三个任务:has already!
task :第二个任务:run !
task :第三个任务:run !
task :第一个任务:run !
task :第二个任务:complete!
task :第一个任务:complete!
task :第三个任务:complete!
5、区别:CyclicBarrier和CountDownLatch