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()
但是不行~~
希望大家能帮忙想想怎么做?
谢谢了

解决方案 »

  1.   

    thread.start();
    // 主线程睡十秒    
    Thread.sleep(10000)
    thread.interrupt();
    我能想到的办法,LZ看看能满足需求不?
      

  2.   

    主要是使用一个标志来判断是否继续进行循环, 线程停止一般都是这样处理的, 写了个片段, 不防参考一下.
    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.
      

  3.   

    public class Console {    private static class InputThread extends Thread {
            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);
            }    }
    }