实现一个容器,提供两个方法,add(),size() 写两个线程,线程1添加10个元素到容器中,线程2实现监控元素的个数为5时,线程2 给出提示并结束。

一、 wait(),notify() 方法

public class Test1 {

    private static ArrayList<Object> lists = new ArrayList<Object>();

    public void add(Object o){
        lists.add(o);
    }

    public int size(){
        return lists.size();
    }

    public static void main(String[] args) {
        final Object lock = new Object();
        Thread t1 = new Thread(new Runnable() {
            public void run() {
                System.out.println("t1 执行");
                synchronized (lock){
                    for (int i = 1; i <= 10; i++) {
                        lists.add(i);
                        System.out.println("t1 add " + i);
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        if(lists.size() == 5){
                            // 通知 t2 执行
                            lock.notify();
                            try {
                                // t1 释放锁
                                lock.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
               
                    }

                }
            }
        });

        Thread t2 = new Thread(new Runnable() {
            public void run() {
                System.out.println("t2 执行");
                synchronized (lock){
                    if(lists.size() != 5){
                        try {
                            lock.wait(); // t2 释放锁
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("t2 结束");
                    // 通知t1执行
                    lock.notify();
                }
            }
        });
        t2.start();
        t1.start();
    }
}

二、 CountDownLatch

public class TestCountDownLatch {

    private static ArrayList<Object> lists = new ArrayList<Object>();

    public void add(Object o){
        lists.add(o);
    }

    public int size(){
        return lists.size();
    }

    public static void main(String[] args) {

        final CountDownLatch latch1 = new CountDownLatch(1);
        final CountDownLatch latch2 = new CountDownLatch(1);
        
        Thread t1 = new Thread(new Runnable() {
            public void run() {
                System.out.println("t1 执行");
                    for (int i = 1; i <= 10; i++) {
                        lists.add(i);
                        System.out.println("t1 add " + i);
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        if(lists.size() == 5){
                            latch2.countDown(); // 打开门栓
                            try {
                                latch1.await();  // 拴住
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }

                    }

                }
        });

        Thread t2 = new Thread(new Runnable() {
            public void run() {
                System.out.println("t2 执行");
                    if(lists.size() != 5){
                        try {
                            latch2.await(); // 拴住门栓
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("t2 结束");
                    // 打开门栓
                    latch1.countDown();
                }
        });
        t2.start();
        t1.start();
    }
}

三、LockSupport

public class TestLockPark {
    private static ArrayList<Object> lists = new ArrayList<Object>();

    public void add(Object o){
        lists.add(o);
    }

    public int size(){
        return lists.size();
    }

     static Thread t1 = null;
     static Thread t2 = null;
    public static void main(String[] args) {


        t1 = new Thread(new Runnable() {
            public void run() {
                System.out.println("t1 执行");
                for (int i = 1; i <= 10; i++) {
                    lists.add(i);
                    System.out.println("t1 add " + i);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if(lists.size() == 5){
                        LockSupport.unpark(t2);
                        try {
                            LockSupport.park();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                }

            }
        });

        t2 = new Thread(new Runnable() {
            public void run() {
                System.out.println("t2 执行");
                if(lists.size() != 5){
                    try {
                        LockSupport.park();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("t2 结束");
                LockSupport.unpark(t1);
            }
        });
        t2.start();
        t1.start();
    }
}

四、volatile

public class TestVolatile {

    private static volatile List<Object> lists = Collections.synchronizedList(new LinkedList<Object>());

    public void add(Object o){
        lists.add(o);
    }

    public int size(){
        return lists.size();
    }

    public static void main(String[] args) {


        Thread t1 = new Thread(new Runnable() {
            public void run() {
                System.out.println("t1 执行");
                    for (int i = 1; i <= 10; i++) {
                        lists.add(i);
                        System.out.println("t1 add " + i);
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
            }
        });

        Thread t2 = new Thread(new Runnable() {
            public void run() {
                System.out.println("t2 执行");
                while (true){
                    if (lists.size() == 5){
                        break;
                    }
                }
                System.out.println("t2 结束");
                }

        });
        t2.start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        t1.start();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值