1.实现奇数和偶数的交叉打印

2.打印时间间隔1秒

public class TestThread02 {
  public static void main(String[] args) {
    Thread t1 = new EvenThread();
    Thread t2 = new EvenThread();
    t1.setName("奇数线程");
    t2.setName("偶数线程");

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

class EvenThread extends Thread{
  private static int num = 0;
  public void run() {
    while (true) {
      synchronized (Thread.class) {
        System.out.println(getName() + ":" + ++num);
        try {
          Thread.sleep(1000);
          Thread.class.notify();
          Thread.class.wait();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }
  }
}

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/dirsoen/p/12564729.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!