想要个示例代码,键盘按p就线程暂停,按r就继续执行,按s停止。
谢谢了。

解决方案 »

  1.   

    比如一个简单的程序,打印1-100,按p暂停,r继续,s停止,谢谢各位了。
      

  2.   


    public class test{
      test() throws Exception{
        Thread t[] = new Thread[5];
        final Thread o = new Thread();
        for(int i=0;i<t.length;i++){
          final int j = i;
          t[i] = new Thread(){
            {start();}
            public void run(){
                try{
                  System.out.println("my name is "+j);
                  synchronized(o){
                    o.wait();
                  }
                  run();
                }catch(Exception e){System.out.println(e);}
                
            }
          };
        }
        for(int i=0;i<10;i++){
          synchronized(this){
            wait(500);
          }
          synchronized(o){
            o.notifyAll();
          }
        }
        System.exit(0);
      }
      public static void main(String args[]) throws Exception{
        new test();
      }
    }这样行不?
    每半秒暂停一次
      

  3.   

    不行?我这边可以运行啊
    C:\java>javac test.javaC:\java>java test
    my name is 0
    my name is 1
    my name is 2
    my name is 3
    my name is 4
    my name is 0
    my name is 1
    my name is 2
    my name is 3
    my name is 4
    my name is 0
    my name is 1
    my name is 2
    my name is 3
    my name is 4
    my name is 0
    my name is 1
    my name is 2
    my name is 3
    my name is 4
    my name is 4
    my name is 3
    my name is 2
    my name is 1
    my name is 0
    my name is 4
    my name is 3
    my name is 2
    my name is 1
    my name is 0
    my name is 4
    my name is 3
    my name is 2
    my name is 1
    my name is 0
    my name is 4
    my name is 2
    my name is 0
    my name is 3
    my name is 1
    my name is 4
    my name is 2
    my name is 3
    my name is 1
    my name is 0
    my name is 4
    my name is 2
    my name is 3
    my name is 1
    my name is 0
    my name is 4
    my name is 2
    my name is 1
    my name is 0
    my name is 3
      

  4.   

    那很奇怪啊,我运行就什么都没有output.
      

  5.   

    那我能再请问下thread是怎么让它暂停和继续的么?
      

  6.   

    就是先做一把锁,然后用wait();和notify();来控制就行了
      

  7.   

    就是先做一把锁,然后用wait();和notify();来控制就行了
      

  8.   

    能帮我看看这段代码为什么不能控制暂停和继续么?public class testThread {
        
        public static void main(String[] args) throws InterruptedException {
            
            A a = new A();
            a.start();
            synchronized(a){
                a.wait(10000);
              }
              synchronized(a){
                a.notifyAll();
              }
        }
    }
    class A extends Thread{
        
        public void run(){
            
                    try {
                     for(int i=0; i<10; i++)
                     {
                     System.out.println(" do ......" + i);
                     }
                    } catch (Exception dx) {
                        dx.printStackTrace();
                    }
                }
            
    }
      

  9.   

    Java_2000老竹竹和其他版主帮帮我啊
      

  10.   

    FROM:http://www.java2000.net/viewthread.jsp?tid=1735
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;public class Test {
      public static void main(String[] args) throws IOException, InterruptedException {
        TestThread t = new TestThread();
        t.start();
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        while (true) {
          line = reader.readLine();
          System.out.println("line=" + line);
          if ("p".equalsIgnoreCase(line)) {
            t.pause = true;
          } else {
            t.pause = false;
            synchronized (t) {
              t.notifyAll();
            }
          }
        }
      }
    }class TestThread extends Thread {
      boolean pause;  public void run() {
        while (true) {
          try {
            if (pause) {
              synchronized (this) {
                wait();
              }
            }
            System.out.println("I am working ...");
            Thread.sleep(1000);
          } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        }
      }
    }
      

  11.   

    谢谢老竹竹先,我去运行下看看。
    不过怎么程序有错,运行不了呢?第一行import报错?这种是什么错误啊?
      

  12.   

    如果异常在catch范围内,程序能正常切换,那么就是可控异常,不用管它!