java模拟并发 CountDownLatch

//这个代码是有问题的,线程不安全


```java
package com.example;

public class MyInstance2 {
    private static MyInstance2 instance;
    private MyInstance2(){
        System.out.println("new Instance");
    }

    //在多线程的情况下 会造成实例出来多份
    public static MyInstance2 create(){
        if(instance==null){
            instance = new MyInstance2();
        }
        return instance;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 999999; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    create();
                }
            }).start();
        }
    }
}
//在线程数量足够的时候会产生第二份,甚至第三方实例

CountDownLatch

public CountDownLatch(int count) {
    //Constructs a {@code CountDownLatch} initialized with the given count.
}
//Causes the current thread to wait until the latch has counted down to
//* zero, unless the thread is {@linkplain Thread#interrupt interrupted}.
//导致当前线程等待,直到计数器为0
public void await() throws InterruptedException {
        sync.acquireSharedInterruptibly(1);
}

// Decrements the count of the latch, releasing all waiting threads if
     * the count reaches zero.
// 自减当前的计数器,释放所有等待的线程如果计数器达到0
public void countDown() {
        sync.releaseShared(1);
    }
    
定义一个计数器,闭锁,创建线程后,调用awati,会导致当前线程为等待状态。至到countDown(),至到计数器为0,
一次性释放所有的等待中的线程。
它可以用来模拟并发。
package com.example;

import java.util.concurrent.CountDownLatch;

public class TestCountDownLatch {
    public static void main(String[] args) {
        CountDownLatch cdl = new CountDownLatch(1);
        for (int i = 0; i < 10; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        cdl.await();
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("Thread be called");
                }
            }).start();
        }
        cdl.countDown();//在这个时候10个线程都已经完全就绪了,模拟并发
        System.out.println("ok");
    }
}

package com.example;

import java.util.concurrent.CountDownLatch;

public class MyInstance3 {
    private static MyInstance3 instance;
    private MyInstance3(){
        System.out.println("new Instance");
    }

    //在多线程的情况下 会造成实例出来多份
    public static MyInstance3 create(){
        if(instance==null){
            instance = new MyInstance3();
        }
        return instance;
    }

    public static void main(String[] args) {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        for (int i = 0; i < 999; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        countDownLatch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    create();
                }
            }).start();
        }
        countDownLatch.countDown();
    }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容