我在写一个游戏,目前碰到一个问题:我要在线程A中调用线程B的某对象的方法,请问怎么做?

解决方案 »

  1.   

    是这样的,我在线程B的run()函数中创建了一个对象C,现在想要在线程A中调用C的方法,其中线程B也是在线程A中创建的。大虾请赐教。
      

  2.   

    // Try this
    class X {
      StringBuffer variable;
      public void test() {
        new Thread( new Runnable(){
            public void run() {
              variable = new StringBuffer("abc");
              new Thread( new Runnable(){
                public void run() {
                  while(variable == null || variable.toString().equals("abc")){
                    try {Thread.sleep(10000);} catch (Exception e) {}
                    if(variable != null && variable.toString().equals("abc"))
                      variable.append("abc");
                  }
                  System.out.println("sub thread ends.");
                }
              }).start();
              while(variable.toString().equals("abc")){
                System.out.println("variable="+variable);
                try{Thread.sleep(1000);}catch(Exception e){}
              }
              System.out.println("parent thread ends");
            }
          }).start();
          System.out.println("test() ends");
      }
    }
      

  3.   

    我在线程B的run()函数中创建了一个对象C,现在想要在线程A中调用C的方法??????
    c究竟是对象还是方法?
      

  4.   

    lindows,看看helpall的例子,把变量的作用于声明在外面,然后再在线程里初始化和使用,不过要注意同步,也要防止初始化之前的使用。