java多线程(2):并发编程实践(三)

2014-11-24 02:42:53 · 作者: · 浏览: 3
程数达到了CyclicBarrier初始时规定的数目时,所有进入等待状态的线程被唤醒并继续。
* CyclicBarrier就象它名字的意思一样,可看成是个障碍,
* 所有的线程必须到齐后才能一起通过这个障碍。
* CyclicBarrier初始时还可带一个Runnable的参数,
* 此Runnable任务在CyclicBarrier的数目达到后,所有其它线程被唤醒前被执行。

第1节:实例

package com.mcc.core.test.thread;

import com.mcc.core.concurrent.ExecutorServiceUtils;

import java.util.concurrent.*;

/**
* CyclicBarrierTest线程计数器使用实例
*
* @author menergy
* DateTime: 13-12-30 上午10:39
*/
public class CyclicBarrierTest {
public static void main(String args[]){
// 实例化线程计数器
int size = 3;
final CyclicBarrier barrier = new CyclicBarrier(size,new Runnable(){

@Override
public void run() {
System.out.println("barrier thread running !");
}
});
Executor executor = ExecutorServiceUtils.getExecutor("test", size);
//这里测试7个线程
for(int i = 0; i < size * 2 + 1; i++){
final int threadNum = i;
executor.execute(new Runnable(){
@Override
public void run() {
try {
System.out.println("thead wait:" + threadNum);
//线程等待,barrier初始化为3,所以要等齐三个线程抵达栅栏时才能一起通过栅栏,前6个线程需分两批3个通过,第7个通不过,除非到达超时
//barrier.await();
try {
barrier.await(5000, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
System.out.println("thread timeout:"+ threadNum);
}
//线程通过栅栏
System.out.println("thread running:" + threadNum);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
});
}
// 主线程等待子线程,一直到程序计数器为0
System.out.println("main trhread finished !");
}
}


第5章:延迟队列DelayQueue

第0节:札记

* DelayQueue是一个无界的BlockingQueue,用于放置实现了Delayed接口的对象,其中的对象只能在其到期时才能从队列中取走。
* 这种队列是有序的,即队头对象的延迟到期时间最长。注意:不能将null元素放置到这种队列中。
* Delayed接口的实现必须定义一个 compareTo 方法,该方法提供与此接口的 getDelay 方法一致的排序。

第1节:实例

package com.mcc.core.test.thread;

import com.mcc.core.concurrent.ExecutorServiceUtils;

import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
* DelayQueue实例测试
*
* @author menergy
* DateTime: 13-12-31 下午3:20
*/
public class DelayQueueTest {
static class DelayTask implements Runnable,Delayed{
//任务名
private String name;
//时间
private long time = 0;
public DelayTask(String name,long delay) {
this.name = name;
time = System.currentTimeMillis() + TimeUnit.MILLISECONDS.convert(delay, TimeUnit.MILLISECONDS);
}

@Override
public long getDelay(TimeUnit unit) {
return unit.convert(time - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
}

@Override
public int compareTo(Delayed o) {
long result = ((DelayTask) o).getTime() - this.getTime();
if (result < 0) {
return 1;
}
if (result > 0) {
return -1;
}
return 0;
}

@Override
public void run() {
System.out.println("产品被消费:" + this.toString());

}

public String toString() {
return "{name:" + this.getName() + ",延时:" + this.getDelay(TimeUnit.MILLISECONDS) + "}";
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public long getTime() {
return time;
}

public void setTime(long time) {
this.time = time;
}
}

public static void main(String args[]){

//原子计数器
final AtomicInteger productNum = new AtomicInteger(0);
final DelayQueue delayQueue = new DelayQueue ();

ExecutorService executorService = ExecutorServiceUtils.getExecutor("test", 2);
//生产
executorS