class NewThread implements Runnable {
  String name;
  Thread t;
  boolean suspendFlag;
  NewThread(String threadname) {
    name=threadname;
    t=new Thread(this,name);
    System.out.println("New thread: "+t);
    suspendFlag=false;
    t.start();
  }
  public void run() {
     try {
       for(int i=15;i>0;i--) {
         System.out.println(name+": "+i);
         Thread.sleep(200);
         synchronized(this) {
           while(suspendFlag) {
             wait();
           }
         }
       }
     } catch(InterruptedException e) {
        System.out.println(name+" interrupted.");
    }
    System.out.println(name+" exiting.");
  }
  void mysuspend() {
    suspendFlag=true;
  }
  synchronized void myresume() {
    suspendFlag=false;
    notify();
  }
}class SuspendResume {
  public static void main(String args[]) {
    NewThread ob1=new NewThread("One");
    NewThread ob2=new NewThread("Two");
    try {
      Thread.sleep(1000);
      ob1.mysuspend();
      System.out.println("Suspending thread One");
      Thread.sleep(1000);
      ob1.myresume();
      System.out.println("Resuming thread One");
      ob2.mysuspend();
      System.out.println("Suspending thread Two");
      Thread.sleep(1000);
      ob2.myresume();
      System.out.println("Resuming thread Two");
    } catch (InterruptedException e) {
     System.out.println("Main thread Interrupted");
    }
    try {
      System.out.println("Waiting for threads to finish.");
      ob1.t.join();
      ob2.t.join();
    } catch (InterruptedException e) {
        System.out.println("Main thread Interrupted");
    }
    System.out.println("Main thread exiting.");
   }
}

解决方案 »

  1.   

             synchronized(this) { 
               while(suspendFlag) { 
                 wait(); 
               } 
             }
    这里的synchronized(this) 的作用
      

  2.   

    利用this对象定义了一个同步块
    因为下面定义了一个同步方法,同步方法默认也是对this对象加锁
    这样说了你应该明白了吧?
      

  3.   

    while(suspendFlag) { 在 synchronized 的代码,必须拿到当前对象的锁,保证多个线程只有1个在运行。里面的 wait() 会释放这个锁,让其它线程有机会运行。
      

  4.   

    帮你插入代码看看先^_^
    class NewThread implements Runnable { 
      String name; 
      Thread t; 
      boolean suspendFlag; 
      NewThread(String threadname) { 
        name=threadname; 
        t=new Thread(this,name); 
        System.out.println("New thread: "+t); 
        suspendFlag=false; 
        t.start(); 
      } 
      public void run() { 
         try { 
           for(int i=15;i>0;i--) { 
             System.out.println(name+": "+i); 
             Thread.sleep(200); 
             synchronized(this) { 
               while(suspendFlag) { 
                 wait(); 
               } 
             } 
           } 
         } catch(InterruptedException e) { 
            System.out.println(name+" interrupted."); 
        } 
        System.out.println(name+" exiting."); 
      } 
      void mysuspend() { 
        suspendFlag=true; 
      } 
      synchronized void myresume() { 
        suspendFlag=false; 
        notify(); 
      } 
    } class SuspendResume { 
      public static void main(String args[]) { 
        NewThread ob1=new NewThread("One"); 
        NewThread ob2=new NewThread("Two"); 
        try { 
          Thread.sleep(1000); 
          ob1.mysuspend(); 
          System.out.println("Suspending thread One"); 
          Thread.sleep(1000); 
          ob1.myresume(); 
          System.out.println("Resuming thread One"); 
          ob2.mysuspend(); 
          System.out.println("Suspending thread Two"); 
          Thread.sleep(1000); 
          ob2.myresume(); 
          System.out.println("Resuming thread Two"); 
        } catch (InterruptedException e) { 
         System.out.println("Main thread Interrupted"); 
        } 
        try { 
          System.out.println("Waiting for threads to finish."); 
          ob1.t.join(); 
          ob2.t.join(); 
        } catch (InterruptedException e) { 
            System.out.println("Main thread Interrupted"); 
        } 
        System.out.println("Main thread exiting."); 
       } 
    }
      

  5.   

    synchronized(this)用于定义一个临界区(即是:其后面大括弧所包含的部分),以保证在多线程下只能有一个线程可以进入this对象的临界区。
      

  6.   

    synchronized(对象或者变量){},表示在{}内的这段代码中,对(对象或者变量)中的对象或者变量进行同步处理,也就是说当访问这段代码时,一个线程对对象或者变量的访问完成后才能够交给另外一个线程,即有了同步锁
      

  7.   

    其实它用synchronized
    是为了防止suspendFlag变量被同时访问,
    是为了多线程安全考虑的,否则如果多个线程修改和读取同时出现的时候
    while(suspendFlag)
    这一行就可能做出错误的判断synchronized的意思可以到网上 google