java中的線程協(xié)調(diào)與線程間的數(shù)據(jù)交換
來源:程序員人生 發(fā)布時(shí)間:2015-04-10 08:37:01 閱讀次數(shù):2700次
Java中提供多種方式調(diào)和線程的工作。
CountDownLatch:當(dāng)多個(gè)也就是具體的數(shù)量等于CountDownLatch初始化參數(shù)的時(shí)候。線程到達(dá)了預(yù)期狀態(tài)或完成了預(yù)期工作時(shí)觸發(fā)事件,其他線程可以等待這個(gè)事件來觸發(fā)自己的后續(xù)工作。等待的線程是多個(gè)。到達(dá)了預(yù)期狀態(tài)的線程會(huì)調(diào)用CountDownLatch的countDown方法。等待的線程會(huì)調(diào)用CountDownLatch的await方法。
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class CountDownLatchDemo {
static ThreadPoolExecutor tp = new ThreadPoolExecutor(1, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(5) );
public static void main(String[] args) throws InterruptedException {
int count = 10;
final CountDownLatch latch = new CountDownLatch(count);
int[] datas = new int[10240];
int step = datas.length / count;
for (int i=0; i<count; i++) {
int start = i * step;
int end = (i+1) * step;
if (i == count ⑴)
end = datas.length;
tp.execute(new MyRunable(latch, datas, start, end));
}
latch.await();
}
}
import java.util.concurrent.CountDownLatch;
public class MyRunable implements Runnable {
CountDownLatch latch;
int[] datas;
int start;
int end;
public CountDownLatch getLatch() {
return latch;
}
public void setLatch(CountDownLatch latch) {
this.latch = latch;
}
public int[] getDatas() {
return datas;
}
public void setDatas(int[] datas) {
this.datas = datas;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getEnd() {
return end;
}
public void setEnd(int end) {
this.end = end;
}
public MyRunable(CountDownLatch latch, int[] datas, int start, int end) {
this.latch = latch;
this.datas = datas;
this.start = start;
this.end = end;
}
@Override
public void run() {
latch.countDown();
}
}
CyclicBarrier是指循環(huán)屏障。它可以協(xié)同多個(gè)線程,讓多個(gè)線程在這個(gè)屏障前等待。直到所有的線程都到達(dá)了這個(gè)屏障,再1起繼續(xù)履行后面的工作。
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class CyclicBarrierDemo {
static ThreadPoolExecutor tp = new ThreadPoolExecutor(1, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(5) );
public static void main(String[] args) throws InterruptedException {
int count = 10;
final CyclicBarrier latch = new CyclicBarrier(count);
int[] datas = new int[10240];
int step = datas.length / count;
for (int i=0; i<count; i++) {
int start = i * step;
int end = (i+1) * step;
if (i == count ⑴)
end = datas.length;
tp.execute(new MyRunable1(latch, datas, start, end));
}
try {
latch.await();
} catch (BrokenBarrierException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
CyclicBarrier與CountDownLatch都用于線程之間的調(diào)和。二者之間的差別在于,CountDownLatch是在多個(gè)線程都進(jìn)行了latch.countDown方法后,才會(huì)觸發(fā)事件,喚醒a(bǔ)wait在latch上的線程。而履行countDown的線程,行完countDown后會(huì)繼續(xù)自己的線程工作。CyclicBarrier用于同步所有調(diào)用await方法的線程,并且等待所有線程都到await方法時(shí),這些線程才繼續(xù)各自的工作。
Exchanger
不同線程之間的數(shù)據(jù)交換用Exchanger。當(dāng)1個(gè)線程的方法履行調(diào)用exchanger.exchange的時(shí)候,會(huì)等待其他線程也履行到調(diào)用這個(gè)方法。然后交換彼此之間的數(shù)據(jù)
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Exchanger;
public class ExchangerDemo {
public static void main(String[] args) {
final Exchanger<List<Integer>> exchanger = new Exchanger<List<Integer>>();
new Thread(){
public void run(){
List<Integer> list = new ArrayList<Integer>(2);
list.add(1);
list.add(2);
try {
list = exchanger.exchange(list);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("thread1:" + list);
}
}.start();
new Thread(){
public void run(){
List<Integer> list = new ArrayList<Integer>(3);
list.add(3);
list.add(4);
list.add(5);
try {
list = exchanger.exchange(list);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("thread2:" + list);
}
}.start();
}
}
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)