当前位置: 移动技术网 > IT编程>开发语言>Java > java多线程实现奇数和偶数的交叉打印

java多线程实现奇数和偶数的交叉打印

2020年03月25日  | 移动技术网IT编程  | 我要评论

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();
        }
      }
    }
  }
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网