码迷,mamicode.com
首页 > 编程语言 > 详细

Java同步器示例

时间:2015-12-30 13:51:24      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:java

CountDownLatch

    public static void testCountDownLatch(String[] args) {
        final CountDownLatch start = new CountDownLatch(1);
        final CountDownLatch end = new CountDownLatch(4);

        final Runnable runnable = new Runnable() {
            @Override
            public void run() {

                try {
                    int id = new Random().nextInt(10);
                    start.await();
                    System.out.println(id + " Start");
                    Thread.sleep(id * 1000);
                    System.out.println(id + " end");
                    end.countDown();
                }
                catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        };

        new Thread(runnable).start();
        new Thread(runnable).start();
        new Thread(runnable).start();
        new Thread(runnable).start();

        start.countDown();
        try {
            end.await();
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("All End");
    }


CyclicBarrier

    public static void testCyclicBarrier(String[] args) {
        final CyclicBarrier barrier = new CyclicBarrier(4, new Runnable() {
            @Override
            public void run() {
                System.out.println("In Handler");
            }
        });

        final Runnable runnable = new Runnable() {
            @Override
            public void run() {
                int id = new Random().nextInt(10);
                try {
                    for (int i = 0; i < 3; i++) {
                        System.out.println(id + " Start");
                        Thread.sleep(id * 1000);
                        System.out.println(id + " end");
                        barrier.await();
                    }
                }
                catch (InterruptedException | BrokenBarrierException e) {
                    throw new RuntimeException(e);
                }
            }
        };

        new Thread(runnable).start();
        new Thread(runnable).start();
        new Thread(runnable).start();
        new Thread(runnable).start();
    }


Semaphore

    public static void testSemaphore(String[] args) {
        final Semaphore semaphore = new Semaphore(3);

        final Runnable runnable = new Runnable() {
            @Override
            public void run() {
                int id = new Random().nextInt(10);
                try {
                    semaphore.acquire();
                    System.out.println(id + " Start");
                    Thread.sleep(3000);
                    System.out.println(id + " end");

                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
                finally {
                    semaphore.release();
                }
            }
        };

        new Thread(runnable).start();
        new Thread(runnable).start();
        new Thread(runnable).start();
        new Thread(runnable).start();
    }


SynchronousQueue

    public static void testSynchronousQueue(String[] args) {
        final SynchronousQueue<Integer> sq = new SynchronousQueue<>();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for (int i = 0; i < 3; i++) {
                        Thread.sleep(new Random().nextInt(5) * 1000);
                        sq.put(i);
                        System.out.println("Put " + i);
                    }
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 3; i++) {
                    try {
                        int m = sq.take();
                        System.out.println("Take " + m);
                    }
                    catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        }).start();
    }


Exchanger

    public static void testExchanger(String[] args) {
        final Exchanger<byte[]> exchanger = new Exchanger<>();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    byte[] a = "123456".getBytes();
                    byte[] b = exchanger.exchange(a);
                    System.out.println("123456 ==> " + new String(b));
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    byte[] a = "456789".getBytes();
                    byte[] b = exchanger.exchange(a);
                    System.out.println("456789 ==> " + new String(b));
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }).start();
    }


Java同步器示例

标签:java

原文地址:http://wangzhichao.blog.51cto.com/2643325/1729854

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!