1.阻塞队列
package tutorial; /** * <pre> * 这是java.util.concurrent队列应用的例子 * 这里以阻塞队列为实例说明 内容和代码取自jdk文档并做出了相应的修改 * * 接口 BlockingQueue<E> 实现类 * ArrayBlockingQueue, DelayQueue, * LinkedBlockingDeque, LinkedBlockingQueue, * PriorityBlockingQueue, SynchronousQueue * * BlockingQueue 方法以四种形式出现,对于不能立即满足但可能在将来某一时刻可以满足的操作, * 这四种形式的处理方式不同:第一种是抛出一个异常,第二种是返回一个特殊值(null 或 false, * 具体取决于操作),第三种是在操作可以成功前,无限期地阻塞当前线程,第四种是在放弃前只在 * 给定的最大时间限制内阻塞。下表中总结了这些方法: * 抛出异常 特殊值 阻塞 超时 * 插入 add(e) offer(e) put(e) offer(e, time, unit) * 移除 remove() poll() take() poll(time, unit) * 检查 element() peek() 不可用 不可用 * * 以下是基于典型的生产者-使用者场景的一个用例。注意,BlockingQueue 可以安全地与多个生产者和多个使用者一起使用。 * </pre> */ import java.util.Date; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; @SuppressWarnings("unchecked") class Producer implements Runnable { private final BlockingQueue queue; Producer(BlockingQueue q) { queue = q; } public void run() { try { while (true) { queue.put(produce()); Thread.sleep(3000); } } catch (InterruptedException ex) { ex.printStackTrace(); } } Object produce() { return new Date(); } } @SuppressWarnings("unchecked") class Consumer implements Runnable { int id; private final BlockingQueue queue; Consumer(BlockingQueue q, int id) { queue = q; this.id = id; } public void run() { try { while (true) { consume(queue.take()); } } catch (InterruptedException ex) { ex.printStackTrace(); } } void consume(Object x) { System.out.println(id + " " + (Date) x); } } @SuppressWarnings("unchecked") class Setup { void main() { BlockingQueue q = new LinkedBlockingQueue(); Producer p = new Producer(q); Consumer c1 = new Consumer(q, 1); Consumer c2 = new Consumer(q, 2); new Thread(p).start(); new Thread(c1).start(); new Thread(c2).start(); } } public class BlockingQueueTest { public static void main(String args[]) { new Setup().main(); } }
2.执行者
package tutorial; /** * Executor使用例子 代码取自jdk文档并稍作了修改 * * 执行已提交的 Runnable 任务的对象。此接口提供一种将任务提交与每个任务将如何运行的 * 机制(包括线程使用的细节、调度等)分离开来的方法。通常使用 Executor 而不是显式 * 地创建线程。例如,可能会使用以下方法,而不是为一组任务中的每个任务调用 * new Thread(new(RunnableTask())).start(): */ import java.util.Queue; import java.util.concurrent.Executor; import java.util.concurrent.LinkedBlockingQueue; class DirectExecutor implements Executor { public void execute(Runnable r) { r.run(); } } class SerialExecutor implements Executor { final Queue<Runnable> tasks = new LinkedBlockingQueue<Runnable>(); final Executor executor; Runnable active; SerialExecutor(Executor executor) { this.executor = executor; } public synchronized void execute(final Runnable r) { tasks.offer(new Runnable() { public void run() { try { r.run(); } finally { scheduleNext(); } } }); if (active == null) { scheduleNext(); } } protected synchronized void scheduleNext() { if ((active = tasks.poll()) != null) { executor.execute(active); } } } public class SerialExecutorTest { public static void main(String args[]) { SerialExecutor exe = new SerialExecutor(new DirectExecutor()); exe.execute(new Runnable(){ public void run() { System.out.println("执行任务1"); } }); exe.execute(new Runnable(){ public void run() { System.out.println("执行任务2"); } }); } }
3.线程池
package tutorial; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class ThreadPoolExecutorTest { /** * @param args */ @SuppressWarnings("unchecked") public static void main(String[] args) { ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 6, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue()); executor.execute(new Runnable() { public void run() { System.out.println("执行线程任务1"); } }); executor.execute(new Runnable() { public void run() { try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("执行线程任务2"); } }); executor.execute(new Runnable() { public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("执行线程任务3"); } }); executor.execute(new Runnable() { public void run() { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("执行线程任务4"); } }); executor.execute(new Runnable() { public void run() { System.out.println("执行线程任务5"); } }); } }