面试官:怎么使用两个线程循环打印出1~100?三个线程呢?

一、使用两个线程循环打印出1~100

    static class printNum{
        public static void main(String[] args) {
            TestThread testThread = new TestThread();
            Thread A = new Thread(testThread,"线程1");
            Thread B = new Thread(testThread,"线程2");
            A.start();
            B.start();
        }
    }
   static class TestThread implements Runnable{
        private int num = 0;
        @Override
        public void run() {
            while (true){
                synchronized (this){
                    notify();
                    if(num++ < 100){
                        System.out.println(Thread.currentThread().getName()+"输出了:"+num);
                    }else {
                        break;
                    }
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    }

二、使用三个线程循环打印出1~100

  • 使用wait/notifyAll机制
public class TestThread2 {
    public static void main(String[] args) throws Exception{
        Thread t1 = new Thread(new MyThread1(0));
        Thread t2 = new Thread(new MyThread1(1));
        Thread t3 = new Thread(new MyThread1(2));
        t1.start();
        t2.start();
        t3.start();

    }
    static class MyThread1 implements Runnable {

        private static Object lock = new Object();

        private static int count = 0;

        int no;

        public MyThread1(int no) {
            this.no = no;
        }

        @Override
        public void run() {
            while (true) {
                synchronized (lock) {
                    if (count >= 100) {
                        break;
                    }
                    if (count % 3 == this.no) {
                        count++;
                        System.out.println(this.no + "--->" + count);
                    } else {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    lock.notifyAll();
                }
            }

        }
    }

}
  • 使用Reentrantlock+Condition实现(和wait/notifyAll类似)
public class Test3 {
    public static void main(String[] args) {
        Thread t1 = new Thread(new MyThread2(0));
        Thread t2 = new Thread(new MyThread2(1));
        Thread t3 = new Thread(new MyThread2(2));
        t1.start();
        t2.start();
        t3.start();
    }
    static class MyThread2 implements Runnable{

        private int no;

        private static ReentrantLock lock = new ReentrantLock();

        private static Condition condition = lock.newCondition();

        private static int count;

        public MyThread2(int no){
            this.no = no;
        }
	        @Override
	        public void run() {
	            lock.lock();
	
	            try {
	                while (true){
	                    if (count>=100){
	                        break;
	                    }else {
	                        if (count%3 == this.no){
	                            count++;
	                            System.out.println(this.no+"-->"+count);
	                        }else {
	                            try {
	                                condition.await();
	                            } catch (InterruptedException e) {
	                                e.printStackTrace();
	                            }
	                        }
	                    }
	                    condition.signalAll();
	                }
	            }  finally {
	                //最后要释放锁,不然会导致死锁
	                lock.unlock();
	            }
	        }
	    }
	}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值