【Java多线程】Java 多个线程交替打印数字

两个线程交替打印数字

/**
 *  两个线程顺序交替打印数字
 */
public class Test5 implements Runnable {
    int i = 0;
    @Override
    public void run() {
        synchronized (this){
            while (i < 200) {
                try {
                    this.notify();
                    System.out.println(Thread.currentThread().getName() + "\t " + i);
                    i++;
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public static void main(String[] args) {
        Test5 t = new Test5();
        for (int i = 0; i < 2; i++) {
            new Thread(t, "线程" + i).start();
        }
    }

}

两个线程顺序打印可以直接使用java 的 synchronized 关键字

  • 首先锁住当前对象,唤醒阻塞的线程 此时另一个线程虽然被唤醒,但是当前线程任持有对象的锁,一直再尝试获取的操作
  • 打印数字并自增 这里因为每个时刻都是单线程操作对象无需考虑可见性与原子性
  • 然后调用 wait() 方法释放锁 此时另一个线程就获得了锁 如此往复
  • 在while >=200 时候执行结束

多个线程交替打印数字

但是我们知道Object 唤醒的时候是调用底层方法随机唤醒的 超过了两个线程就无法这样使用。
这个时候就要使用 ReentrantLock 中对应的 Condition了 简单来说 Condition 就是能够按照队列等待的顺序挨个唤醒线程

/**
 *  多个线程顺序交替打印数字
 * 
 */
public class Test5 implements Runnable {
    Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();
    int i = 0;
    @Override
    public void run() {

        while (i < 200) {
            lock.lock();
            try {
                condition.signalAll();
                System.out.println(Thread.currentThread().getName() + "\t " + i);
                i++;
                Thread.sleep(10);
                condition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        Test5 t = new Test5();
        for (int i = 0; i < 4; i++) {
            new Thread(t, "线程" + i).start();
        }
    }

}

执行步骤:

  • 先唤醒队列中等待的线程
  • 打印当前线程对于 数字的值 并递增
  • 当前线程sleep一小段时间 这个时间是为了让所有的线程能进入顺序的进入等待的队列
  • 阻塞当前线程加入等待队列
发布了29 篇原创文章 · 获赞 19 · 访问量 6480

猜你喜欢

转载自blog.csdn.net/wenzhouxiaomayi77/article/details/104921312