public class ThreadTest extends Thread{
   public void run(){
      System.out.println("in run");
      suspend();
      resume();
      System.out.println("out run");
   }
   public static void main(String[] args) {
      (new ThreadTest()).start();
   }
}
为什么结果不输出out run.

解决方案 »

  1.   

    suspend();//线程已挂起 这时候后面是不会继续执行的.
    //应该在另一个线程恢复
      

  2.   

    建议看一下 API DOC,suspend, resume, stop 三个方法在 JDK 1.2 中就已经被废弃了。
      

  3.   

    resume就是用来恢复 也就是唤醒.如下面 主线程暂停一下 等子线程挂机一段时间再resume
    public static void main(String[] args) {
        Thread thread = new Main();
        thread.start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
        }
        thread.resume();
    }
      

  4.   

    suspend,resume之类的,是控制线程运行的
    显然把这些放在线程里面,是不合适的你的程序就是线程自己把自己暂停了,它还怎么自己恢复呢你应该在new这个线程的时候记下来这个变量然后在需要暂停/恢复线程的时点,去暂停/恢复
      

  5.   

    Why are Thread.suspend and Thread.resume deprecated?
    Thread.suspend is inherently deadlock-prone. If the target thread holds a lock on the monitor protecting a critical system resource when it is suspended, no thread can access this resource until the target thread is resumed. If the thread that would resume the target thread attempts to lock this monitor prior to calling resume, deadlock results. Such deadlocks typically manifest themselves as "frozen" processes.
    What should I use instead of Thread.suspend and Thread.resume?
    As with Thread.stop, the prudent approach is to have the "target thread" poll a variable indicating the desired state of the thread (active or suspended). When the desired state is suspended, the thread waits using Object.wait. When the thread is resumed, the target thread is notified using Object.notify.For example, suppose your applet contains the following mousePressed event handler, which toggles the state of a thread called blinker:    private boolean threadSuspended;    Public void mousePressed(MouseEvent e) {
            e.consume();        if (threadSuspended)
                blinker.resume();
            else
                blinker.suspend();  // DEADLOCK-PRONE!        threadSuspended = !threadSuspended;
        }You can avoid the use of Thread.suspend and Thread.resume by replacing the event handler above with:     public synchronized void mousePressed(MouseEvent e) {
            e.consume();        threadSuspended = !threadSuspended;        if (!threadSuspended)
                notify();
        }
    and adding the following code to the "run loop": 
                    synchronized(this) {
                        while (threadSuspended)
                            wait();
                    }The wait method throws the InterruptedException, so it must be inside a try ... catch clause. It's fine to put it in the same clause as the sleep. The check should follow (rather than precede) the sleep so the window is immediately repainted when the the thread is "resumed." The resulting run method follows:     public void run() {
            while (true) {
                try {
                    Thread.currentThread().sleep(interval);                synchronized(this) {
                        while (threadSuspended)
                            wait();
                    }
                } catch (InterruptedException e){
                }
                repaint();
            }
        }Note that the notify in the mousePressed method and the wait in the run method are inside synchronized blocks. This is required by the language, and ensures that wait and notify are properly serialized. In practical terms, this eliminates race conditions that could cause the "suspended" thread to miss a notify and remain suspended indefinitely. 
    While the cost of synchronization in Java is decreasing as the platform matures, it will never be free. A simple trick can be used to remove the synchronization that we've added to each iteration of the "run loop." The synchronized block that was added is replaced by a slightly more complex piece of code that enters a synchronized block only if the thread has actually been suspended:                if (threadSuspended) {
                        synchronized(this) {
                            while (threadSuspended)
                                wait();
                        }
                    }In the absence of explicit synchronization, threadSuspended must be made volatile to ensure prompt communication of the suspend-request.The resulting run method is: 
        private boolean volatile threadSuspended;    public void run() {
            while (true) {
                try {
                    Thread.currentThread().sleep(interval);                if (threadSuspended) {
                        synchronized(this) {
                            while (threadSuspended)
                                wait();
                        }
                    }
                } catch (InterruptedException e){
                }
                repaint();
            }
        }