Java应用Thread类定义两个线程对象要求每个线程输出它的名字每隔300毫秒输出一次共输出20次。
下面是Java代码实现:
public class MyThread extends Thread {
private String name;
public MyThread(String name) {
this.name = name;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println(name);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
MyThread thread1 = new MyThread("Thread1");
MyThread thread2 = new MyThread("Thread2");
thread1.start();
thread2.start();
}
}
``
原文地址: http://www.cveoy.top/t/topic/gIou 著作权归作者所有。请勿转载和采集!