学习笔记 - java.util.concurrent 多线程框架 [转]
java concurrent 多线程
package concurrent;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class TestThreadPool {public static void main(String args[]) throws InterruptedException {// only two threadsExecutorService exec = Executors.newFixedThreadPool(2);for(int index = 0; index < 100; index++) {Runnable run = new Runnable() {public void run() {long time = (long) (Math.random() * 1000);System.out.println(“Sleeping ” + time + “ms”);try {Thread.sleep(time);} catch (InterruptedException e) {}}};exec.execute(run);}// must shutdownexec.shutdown();}}package concurrent;import static java.util.concurrent.TimeUnit.SECONDS;import java.util.Date;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.ScheduledFuture;public class TestScheduledThread {public static void main(String[] args) {final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);final Runnable beeper = new Runnable() {int count = 0;public void run() {System.out.println(new Date() + ” beep ” + (++count));}};final ScheduledFuture beeperHandle = scheduler.scheduleAtFixedRate(beeper, 1, 2, SECONDS);
final ScheduledFuture beeperHandle2 = scheduler.scheduleWithFixedDelay(beeper, 2, 5, SECONDS);
scheduler.schedule(new Runnable() {public void run() {beeperHandle.cancel(true);beeperHandle2.cancel(true);scheduler.shutdown();}}, 30, SECONDS);}}package concurrent;import java.text.SimpleDateFormat;import java.util.Date;import java.util.concurrent.BrokenBarrierException;import java.util.concurrent.CyclicBarrier;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class TestCyclicBarrier {// 徒步需要的时间: Shenzhen, Guangzhou, Shaoguan, Changsha, Wuhanprivate static int[] timeWalk = { 5, 8, 15, 15, 10 };// 自驾游private static int[] timeSelf = { 1, 3, 4, 4, 5 };// 旅游大巴private static int[] timeBus = { 2, 4, 6, 6, 7 };static String now() {SimpleDateFormat sdf = new SimpleDateFormat(“HH:mm:ss”);return sdf.format(new Date()) + “: “;}static class Tour implements Runnable {private int[] times;private CyclicBarrier barrier;private String tourName;public Tour(CyclicBarrier barrier, String tourName, int[] times) {this.times = times;this.tourName = tourName;this.barrier = barrier;}public void run() {try {Thread.sleep(times[0] * 1000);System.out.println(now() + tourName + ” Reached Shenzhen”);barrier.await();Thread.sleep(times[1] * 1000);System.out.println(now() + tourName + ” Reached Guangzhou”);barrier.await();Thread.sleep(times[2] * 1000);System.out.println(now() + tourName + ” Reached Shaoguan”);barrier.await();Thread.sleep(times[3] * 1000);System.out.println(now() + tourName + ” Reached Changsha”);barrier.await();Thread.sleep(times[4] * 1000);System.out.println(now() + tourName + ” Reached Wuhan”);barrier.await();} catch (InterruptedException e) {} catch (BrokenBarrierException e) {}}}public static void main(String[] args) {// 三个旅行团CyclicBarrier barrier = new CyclicBarrier(3);ExecutorService exec = Executors.newFixedThreadPool(3);exec.submit(new Tour(barrier, “WalkTour”, timeWalk));exec.submit(new Tour(barrier, “SelfTour”, timeSelf));exec.submit(new Tour(barrier, “BusTour”, timeBus));exec.shutdown();}}