你这个代码过不了javac. 另外也没有见线程的启动啊?

解决方案 »

  1.   

    To: helpall()只是说明一下问题的伪码,忽略语法问题。
    再请教 :)
      

  2.   

    线程在stare()后等待CPU空闲,一空闲就进入RUN()多线程可以用到的方法很多,具体看看书,网上很多这方面的资料
      

  3.   

    应该在
    class B
        {
            void check(Mydata data)
            {
                //若干操作
            }
        }
     的check()方法中加上同步:
     synchronized void check(Mydata data)
      

  4.   

    如果在多个Thread中需要对同一数据部分进行操作,那肯定会出现问题的。问题不一定是产生Exception,而是直接影响你的程序逻辑,可能程序会发生些莫名其妙的问题。如果要在多个Thread中对同一数据部分进行读写操作,最好在这个操作方法前加上synchronized关键字,他可以保证同时只有一个线程对该数据进行操作。如:public synchronized void setId(int i)
    {
        this.id = i;
    }public synchronized int getId()
    {
        return id;
    }
      

  5.   

    如果你的A实例只有一个,而且触发事件的那段经常发生--产生很多线程进行B.check的查询的话就肯定出现资源竞争,会出现不确定的错误。所以建议还是采用cooled的方法,比较使用。
      

  6.   

    下面是一个线程同步问题,你好好体会一下吧!class q {//产生同步序列
       int n;
       boolean val=false;
       synchronized int get(){
         if(!val){
           try{
             wait();
             }catch(java.lang.InterruptedException e){}
         }     System.out.println("get: "+n);
         val=false;
         notify();
         return n;
       }   synchronized int put(int n){
         if(val){
           try{
             wait();
             }catch(java.lang.InterruptedException e){}
         }
         this.n=n;
         val=true;
         System.out.println("put:"+n);     notify();
         return n;
       }
    }class pro implements Runnable{//生产者
      q q1;  pro(q q1){
        this.q1=q1;
        new Thread(this).start();
      }  public void run(){
        int i=0;
        while(i!=5)
        try{
          q1.put(i++);
          Thread.sleep(1000);
        }catch(java.lang.InterruptedException e){}
      }
    }
    class cust implements Runnable{//消费者
      q q1;
      cust(q q1){
        this.q1=q1;
        new Thread(this).start();
      }  public void run(){
        while(true){
          q1.get();
        }
      }
      public static void main(String[] args) {
        q q1=new q();
        new pro(q1);
        new cust(q1);
      }
    }