import java.io.*;
import java.util.*;
public class ThreadInterrupt extends Thread implements Runnable
{
public void run(){
try{
sleep(10000); // 延迟50秒 }
catch (InterruptedException e)
{
System.out.println("Q");
System.out.println(e.getMessage());
}
}
public static void main(String[] args) throws Exception
{ ThreadInterrupt ti=new ThreadInterrupt();
Thread thread = new Thread(ti);
System.out.println("在10秒之内按任意键中断线程!");
thread.start();
System.in.read();
thread.interrupt();
System.out.println("Here quit!");
}
}我希望一个线程能运行10秒后自己结束,或者在10秒内输入Enter结束线程!
现在只完成了输入Enter结束线程,但是如何做到运行10秒后自己结束我就
没有实现了~~
本来想在start()之间记录一个时间,之后记录一个个时间,只要两个时间之
差是10秒,就使用interrupt()
但是不行~~
希望大家能帮忙想想怎么做?
谢谢了
import java.util.*;
public class ThreadInterrupt extends Thread implements Runnable
{
public void run(){
try{
sleep(10000); // 延迟50秒 }
catch (InterruptedException e)
{
System.out.println("Q");
System.out.println(e.getMessage());
}
}
public static void main(String[] args) throws Exception
{ ThreadInterrupt ti=new ThreadInterrupt();
Thread thread = new Thread(ti);
System.out.println("在10秒之内按任意键中断线程!");
thread.start();
System.in.read();
thread.interrupt();
System.out.println("Here quit!");
}
}我希望一个线程能运行10秒后自己结束,或者在10秒内输入Enter结束线程!
现在只完成了输入Enter结束线程,但是如何做到运行10秒后自己结束我就
没有实现了~~
本来想在start()之间记录一个时间,之后记录一个个时间,只要两个时间之
差是10秒,就使用interrupt()
但是不行~~
希望大家能帮忙想想怎么做?
谢谢了
解决方案 »
- 关于synchronized的问题
- 大家都来说说 用什么方法开发SOA啊? 我想自学, 但找不到合适的开发工具
- System.runFinalization(); 这个方法,我看API还是不怎么理解。。。大家用过吗/
- java 串口通信
- 测试服务器
- 使用JasperReport生成报表时,如何去掉数据中的null值
- 我的jdk里面怎么少个东西?
- 利用javamail如何获取邮件正文?还有为什么我的邮件主题和发件人有时会乱码呢?
- 浏览器中无法显示java plug-in,显示错误原因......
- 急急急!!!
- 怎么将字串 \u4E00 转字串 "\u4E00"
- 字段做形参,能改变形参的值吗?得到答案马上结贴
// 主线程睡十秒
Thread.sleep(10000)
thread.interrupt();
我能想到的办法,LZ看看能满足需求不?
public class Test {
public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
public void run() {
long start = System.currentTimeMillis();
boolean stopped = false; // 线程是否停止的标志
try {
while (!stopped) {
Thread.sleep(1000); // 停止一秒
int seconds = (int) (System.currentTimeMillis() - start) / 1000;
System.out.println("time: " + seconds);
if (seconds >= 5) {
System.out.println("Thread will be stopped.");
stopped = true;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
}
}
输出结果:
time: 1
time: 2
time: 3
time: 4
time: 5
Thread will be stopped.
private String value; private boolean cancelled; private boolean got; public void run() { try { String line = null; StringBuffer b = new StringBuffer(); while (!cancelled) {
if (System.in.available() > 0) {
char[] cache = new char[System.in.available()]; int c = new InputStreamReader(System.in).read(cache); this.got = true; this.value = new String(cache); break; } else {
Thread.yield(); continue;
}
} } catch (IOException e) {
e.printStackTrace(); this.cancelled = true; } finally {
/* don't close System.in */
}
} public String getValue() {
return this.got ? this.value : null;
} public void cancel() {
this.cancelled = true;
}
} public static void main(String[] args) {
InputThread helper = new InputThread(); System.out.println("Please enter value (time out is 10 seconds):"); helper.start(); try {
helper.join(10 * 1000); helper.cancel(); } catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} String value = helper.getValue(); if (value == null) {
System.out.println("Timed out, use default value instead");
} else {
System.out.println("Entered :" + value);
} }
}