多线程实战一

问题

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

解法一

  使用wait()和notiry()方法实现两个线程之间的通信。

public class MyContainer {
    List<Object> list = new ArrayList<>();

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

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

    public static void main(String[] args) {
        MyContainer myContainer = new MyContainer();
        final Object lock = new Object();
        new Thread(() -> {
            synchronized (lock) {
                if (myContainer.size() != 5) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                lock.notify();
                System.out.println("t2结束并唤醒t1...");
            }
        }).start();

        new Thread(() -> {
            synchronized (lock) {
                for (int i = 0; i < 10; i++) {
                    myContainer.add(new Object());
                    System.out.println("add " + i + "...");
                    if (myContainer.size() == 5) {
                        System.out.println("t1通知t2并暂停...");
                        lock.notify();
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();

    }
}
输出结果:
add 0...
add 1...
add 2...
add 3...
add 4...
t1通知t2并暂停...
t2结束并唤醒t1...
add 5...
add 6...
add 7...
add 8...
add 9...

解法二

  使用两个CountDonwLatch来实现两个线程之间的通信。

public class MyContainer2 {
    List<Object> list = new ArrayList<>();

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

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

    public static void main(String[] args) {
        MyContainer2 myContainer = new MyContainer2();
        CountDownLatch latch1 = new CountDownLatch(1);
        CountDownLatch latch2 = new CountDownLatch(1);

        new Thread(() -> {
            try {
                latch2.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("t2结束并唤醒t1...");
            latch1.countDown();
        }).start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                myContainer.add(new Object());
                System.out.println("add " + i + "...");
                if (myContainer.size() == 5) {
                    System.out.println("t1通知t2并暂停...");
                    latch2.countDown();
                    try {
                        latch1.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}

解法三

  使用LockSupport解决。

public class MyContainer3 {
    List<Object> list = new ArrayList<>();

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

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

    static Thread t1;
    public static void main(String[] args) {
        MyContainer3 myContainer = new MyContainer3();
        Thread t2 = new Thread(() -> {
            LockSupport.park();
            System.out.println("t2结束并唤醒t1...");
            LockSupport.unpark(t1);
        });

        t1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                myContainer.add(new Object());
                System.out.println("add " + i + "...");
                if (myContainer.size() == 5) {
                    System.out.println("t1通知t2并暂停...");
                    LockSupport.unpark(t2);
                    LockSupport.park();
                }
            }
        });

        t2.start();
        t1.start();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值