多线程_面试题_线程管理类应用

多线程面试题一:

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

方法一:

• park unpark ( LockSupport)
• semaphore 灯塔

public class thread_two_thread_01 {

    List<Integer> list = new ArrayList<>();

    public static void main(String[] args) {

        thread_two_thread_01 t = new thread_two_thread_01();

        Semaphore semaphore = new Semaphore(1);



        Thread t2 = new Thread(() -> {

            LockSupport.park();
            System.out.println("哈哈哈 五个了  我继续执行了");
            try {
                semaphore.acquire();

                System.out.println("我开始执行了,你等我三秒钟");

                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            semaphore.release();

        });

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                t.list.add(i);
                System.out.println("开始执行"+i);

                try {
                    Thread.sleep(1000);

                    if(t.list.size() == 5){
                        LockSupport.unpark(t2);
                        Thread.sleep(1);
                        semaphore.acquire();
                        System.out.println("我这边五个了");
                    }

                } catch (InterruptedException e) {

                }

            }
            semaphore.release();
        });

        t1.start();
        t2.start();
    }
}
方法二:

• semaphore 灯塔
• join 线程

public class thread_two_thread_02 {

    public static void main(String[] args) {

        List<Integer> list = new ArrayList<>();

        Semaphore semaphore = new Semaphore(1);


        Thread t2 = new Thread(() -> {

            try {
                semaphore.acquire();
                System.out.println("哈哈哈 五个了  我继续执行了");
                System.out.println("我开始执行了,你等我三秒钟");

                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            semaphore.release();
        });

        Thread t1 = new Thread(() -> {

            try {
                semaphore.acquire();
                for (int i = 0; i < 10; i++) {

                    System.out.println("开始执行" + i);

                    if (i == 5) {
                        System.out.println("开始等待");
                        semaphore.release();
                        t2.join();
                    }

                    list.add(i);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        t1.start();
        t2.start();


    }
}
方法三:
  • countDownLatch (线程门栓)
  • join 线程
public class thread_two_thread_03 {

    volatile List<Integer> list = new ArrayList<>();


    public static void main(String[] args) {

        thread_two_thread_03 t = new thread_two_thread_03();

        CountDownLatch countDownLatch = new CountDownLatch(1);

        Thread t1 = new Thread(() -> {
            try{
                System.out.println("开始等待了");
                countDownLatch.await();
                System.out.println("到我了,你等我两秒钟");
                Thread.sleep(2000);
            }catch (Exception e){

            }

        });

        Thread t2 = new Thread(() -> {
            try{
                System.out.println("我先开始执行");
                for (int i = 0; i < 10; i++) {
                    Thread.sleep(200);
                    System.out.println(i);

                    if(i == 5){
                        countDownLatch.countDown();
                        t1.join();
                    }
                }

            }catch (Exception e){

            }

        });

        t1.start();
        t2.start();
    }
}
方法四:
  • synchronized
  • wait
  • notify
  • notifyAll
  • 特备注意: wait+notify 都是跟synchronized配合使用的
  • o.notify o.wait 必须在含有锁的o对象上
public class Thread_two_thread_04 {

    public static void main(String[] args) {

        Object obj = new Object();

        new Thread(()->{
            try{
                System.out.println("我先来");
                synchronized (obj){
                    System.out.println("我抢到锁了");
                    obj.wait();
                    System.out.println("终于到我了");
                    Thread.sleep(1000);
                    System.out.println("你给我等一秒钟");
                    obj.notify();
                }

            }catch (Exception e){
                System.err.println(e);
            }
            System.out.println("是这里吗");

        }).start();

        try{
            Thread.sleep(1);
        }catch (Exception e){
            System.out.println(e);
        }


        new Thread(()->{
            try{
                synchronized (obj){
                    for (int i = 0; i < 10; i++) {
                        Thread.sleep(200);
                        System.out.println(i);

                        if(i == 5){
                            obj.notify();
                            obj.wait();
                            System.out.println("我继续执行");
                        }

                    }
                }

            }catch (Exception e){
                System.out.println(e);

            }finally {

            }
        }).start();
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值