class  loop {
 main {   
  for ( 开始  ; 间隔5秒 ; 无限 )  {
    System.out.println("");
    if (用事件判断是否q键) {
      break;
    }
    //没有按下任何键或不是q键 
    else {
      继续循环
    } 
  } 
  退出程序
 }
}

解决方案 »

  1.   

    间隔运行可以使用
    java.util.Timer
    scheduleAtFixedRate(TimerTask task, long delay, long period) 
    至于如何接收q,我就不说了。
      

  2.   

    下面应该能够解决你的问题了:)我想看看大家的解法,楼上的兄弟能不能说详细一点?class Accept
    {
    public static void main( String[] args ){
    Accept test = new Accept();
    test.go();
    } public void go(){
    Check singleChk = new Check();
    singleChk.start();

    try{
    while(!Globle.QUIT_LOOP) {
      System.out.println("programm is running...");
      Thread.sleep(5*1000);
      if(!Globle.QUIT_LOOP){
    System.out.println("programm is dosomething");
    } }
    }catch(Exception e){
    System.out.println(e);
    }
    Globle.QUIT_THREAD = true; //通知?束?程
    }
    }import java.io.*;
    public class Check extends Thread {
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    String ch=""; 
      public synchronized void run() {
    while (true && !Globle.QUIT_THREAD) {
    try{ ch = br.readLine();
    System.out.println(".....");
    if (ch.equals("q")) {
    Globle.QUIT_LOOP = true;
    break ;    //退出循?;    
    }
    }catch(Exception e){
    System.out.println(e);
    }
    }
      }
    }public class Globle {
      public static char JUST_CLICK=0;;
      public static boolean QUIT_LOOP=false;
      public static boolean QUIT_THREAD=false;
    }
      

  3.   

    楼上的要是不从System.in读取就好了
      

  4.   

    import java.io.*;
    import java.util.*;public class ThreadTest extends Thread
    {
    public void run() {
    for(;;) {
    System.out.println(new java.util.Date()); //可换成你要执行的任务。
    try {
    sleep(5*1000);
    } catch(Exception e) {
    }
    }
    }

    public static void main(String[] args) {
    char exit = ' ';
    Thread t = new ThreadTest();
    t.setDaemon(true);
    t.start();
    while(exit!='Q'&&exit!='q') {
    try {
    System.out.println("Press Q/q to exit:");
    exit = (char)System.in.read();
    } catch(IOException e){
    }
    }
    System.out.println("Process finished.");
    } }
      

  5.   

    谢谢各位!!这个问题基本解决。觉得小鱼儿的程序精简,试了OK。。我的目的是循环监控和报告一个服务的运行状况,当用户用q键时 要停止监控任务并"停止服务"的运行 不过我发现在win2k下用户按ctrl + c 键时,
    小鱼儿的程序却退出来了,却没有运行System.out.println("Process finished."),我还计划在这儿运行停止服务的程序呢。有什么方法拒绝按ctrl + c ,或者保证该程序退出时一定运行"停止服务"的语句呢?