nameusingthread.java
来自「Java 入门书的源码」· Java 代码 · 共 36 行
JAVA
36 行
//Copyright (c) 1998, Arthur Gittleman
//This example is provided WITHOUT ANY WARRANTY either expressed or implied.
/* Revises Example 11.1 to implement the Runnable
* interface rather than extending Thread.
*/
import java.io.*;
public class NameUsingThread implements Runnable {
private int time;
private Thread thread;
public NameUsingThread(String n, int t) {
time=t;
thread = new Thread(this,n);
thread.start();
}
public void run() {
for (int i=1;i<=5;i++) {
System.out.println(thread.getName() + " " + i);
try {
Thread.sleep(time);
} catch (InterruptedException e) {return;}
}
}
public static void main(String argv[]) {
NameUsingThread bonnie = new NameUsingThread("Bonnie",1000);
NameUsingThread clyde = new NameUsingThread("Clyde",700);
for (int i=1;i<=5;i++) {
System.out.println(Thread.currentThread().getName() + " " + i);
try {
Thread.sleep(1100);
} catch (InterruptedException e) {return;}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?