我这里有一个java thread的例程,
你可以看着改一下啊。
//: Cartoon.java
// Constructor calls during inheritance
package wkai;
import java.util.*;
import java.lang.Object;
class  Summation extends Thread
{
  public Summation(int n){
    upper = n ;
  }
  public void run(){
    int sum=0;
    if(upper>0){
      for(int i=1;i<=upper;i++)
        sum+=i;
    }
    System.out.println("The sum of"+upper+"is "+sum );
  }
  private int upper;
}
class wkai
{
  public static void main(String[] args)
  {
    if(args.length>0){
      if(Integer.parseInt(args[0]<0))
         System.err.println(args[0]+"must be>=0");
     else
    {
        Summation thrd =new Summation(Integer.parseInt(args[0]));
        thrd.start();
      }
    }
    else
      System.err.println("……");  }
}

解决方案 »

  1.   

    public static void main(String[] args)
    里面的核心是
    Summation thrd =new Summation(Integer.parseInt(args[0]));
    thrd.start();
    其他的都可以删除。
      

  2.   

    >>>>>>>cat ThreadTester.java
    public class ThreadTester {
       public static StringBuffer isReady = new StringBuffer("no");   private static void wait0(int second) {
          try{
            Thread.sleep(second*1000);
          }catch(InterruptedException e){}
       }
       public static void main(String[] args) {
    // producer thread
          new Thread(new Runnable() {
            public void run() {
              while(true) {
                 synchronized(isReady) {
                    if(isReady.toString().equals("no") ) {
                       System.out.println("get data from DB");
                       isReady.delete(0,isReady.length());
                       isReady.append("yes");
                       isReady.notify();
                    }
                    try {
                      isReady.wait();
                    }catch(InterruptedException e){}
                    wait0(3);
                 }
              }
            }
          }).start();// consumer thread
          new Thread(new Runnable() {
            public void run() {
              while(true) {
                 synchronized(isReady) {
                    if(isReady.toString().equals("yes") ) {
                       System.out.println("                   processing data");
                       isReady.delete(0,isReady.length());
                       isReady.append("no");
                       isReady.notify();
                    }
                    try {
                      isReady.wait();
                    }catch(InterruptedException e){}
                 }
              }
            }
          }).start();
       }
    }
      

  3.   

    lybapple(阿布) 同志:你给的例子没有实现两个不同线程互相唤醒啊。这才是最重要的啊。
      

  4.   

    在class wkai里面
    Summation thrd =new Summation(Integer.parseInt(args[0]));
    thrd.start();
    那不就是唤醒B吗?
    class  Summation extends Thread里面做好了,返回,
    不就相当于唤醒了A?
      

  5.   

    解决方案:
    1、设置一个全局变量,用于标识产品是否已经被消费:isConsumed
    2、在生产者线程中,当然要注意线程同步的问题了
        while (! isConsumed)
          wait();
        生产产品;
        notify();
    3、在消费者线程中
        while (isConsumed)
          wait();
        消费产品;
        notify();
      

  6.   

    同意楼上的观点,阿布同志确实没有实现两个线程之间的通信,实际上好好研究一下helpAll()的
    代码也是这个道理,其实可以扩展到好几个线程之间也可使用此方式进行通信。