import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class thread {
public static class chapter8_1 extends Thread {
boolean isRunning = true;
int timer = 0;
/**
* 线程体代码
*/
@Override
public void run() {
while (isRunning) {
try {
Thread.currentThread().sleep(1000);
timer++;
System.out.println("逝去了 "+timer+" 秒");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
chapter8_1 t1 = new chapter8_1();
t1.start();
System.out.println("计时器启动...");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
String line = br.readLine();
if (line.equalsIgnoreCase("1")) {
t1.isRunning = false;
/*t1.stop();*/
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
关于JAVA中开个线程的例子(转)
最新推荐文章于 2024-08-18 21:36:31 发布