java多线程实现奇数和偶数的交叉打印
程序员文章站
2023-02-08 14:14:47
1.实现奇数和偶数的交叉打印 2.打印时间间隔1秒 public class TestThread02 { public static void main(String[] args) { Thread t1 = new EvenThread(); Thread t2 = new EvenThrea ......
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();
}
}
}
}
}
上一篇: HTML5实现图片文件异步上传