可否对runnable一类 线程中的方法 以对象引用的方式进行调用,而非线程具体来说,两个类LogicalProcess GlobalControl。
 LogicalProcess 实现了Runnable ,并含有一个私有对象RemoteEventList,以及对RemoteEventList添加对象的公有方法。
GlobalControl 
保持了对一个LogicalProcess 数组的静态引用,又通过ExecutorService 对LogicalProcess 进行线程化,那么,在一个LogicalProcess的代码中直接调用另一个LogicalProcess 对象的公有方法,而不是以线程方式交互,是否可行,会出啥问题呢?谢谢!
public class LogicalProcess implements Runnable {
private Queue<Event> RemoteEventList
private void recieveRemoteEvent(Event aEvent) {
this.RemoteEventList.add(aEvent);
}
public class GlobalControl {public static LogicalProcess LP[] = new LogicalProcess[NumOfLp];

ExecutorService exe = null;//
exe = Executors.newFixedThreadPool(POOL_SIZE);// 
for (int j = 0; j < NumOfLp; j++) {
exe.execute(LP[j]);
[/code]

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【denverbenjamin2000】截止到2008-06-25 19:24:01的历史汇总数据(不包括此帖):
    发帖数:10                 发帖分:720                
    结贴数:7                  结贴分:470                
    未结数:3                  未结分:250                
    结贴率:70.00 %            结分率:65.28 %            
    楼主加油
      

  2.   

    线程类也是类,当然可以随意调用。.start 是一个特殊的方法而已,其它的都和普通的没有区别。唯一注意的,你调用时,类在你当前的线程,不排除调用一半,切换到那个线程哦。。
      

  3.   

    照理来说,只要这个add方法是同步的,就应该没有问题啊!
      

  4.   

    您是说,我该锁住这个线程,还是锁住被调用的方法所在的线程呢?抑或,都锁上用synchronized 另外,是不是,此处应该用同步。
    private synchronized void recieveRemoteEvent(Event aEvent) {
      

  5.   

    您的意思是,引用本身没有错,但是,这样违背了多个线程并发的初衷?
    我做了一个小实验,就是让两个线程在运行中互相引用对方的成员,还是可以并发起来的啊
    package thread1.ParaNull.test;public class test { /**
     * @param args
     */
    public static void main(String[] args) {
    Thread t1 = new Thread(singletonTest.getInstance(0), "t1");
            Thread t2 = new Thread(singletonTest.getInstance(1), "t2");
            
            t1.start();
            t2.start(); }}
    package thread1.ParaNull.test;import thread1.ParaNull.GlobalControl;
    public class singletonTest
    {
        private singletonTest() { }    synchronized public static LogicalProcess getInstance(int i)
        {
        if (m_instance == null)
        {
            m_instance = new LogicalProcess[2];
            for (int j = 0; j < 2; j++) {

             m_instance[j]= new LogicalProcess(j);

    }
            
        }
        return m_instance[i];
    }
          /**
         * @label Creates 
         */
        private static LogicalProcess m_instance[] = null;
    }
    package thread1.ParaNull.test;public class LogicalProcess implements Runnable {
       public int name;
    public LogicalProcess(int i){
    name=i;
    }
        
    public void run() {
          System.out.println(this.name
                     + " enter ");
             int temp;
          for(int j=0;j<10000;j++)// reference to the other thread
              System.out.println(singletonTest.getInstance((name+1)%2).name);
          try {
                 Thread.sleep(3000);
             } catch (InterruptedException e) {
                 // do nothing
             }
             System.out.println(this.name
                     + " exit");
            }
    }
      

  6.   

    RemoteEventList 应该是个待处理的事件队列,而你的每个 LogicalProcess 应该是个事件处理器,每个事件处理器有个与之关联的线程在内部不断地从队列中取出一个事件并对其进行处理,而你的 recieveRemoteEvent 正是向事件队列添加新事件的方法,如果我以上猜的没错,那你只需对你的事件队列作同步处理,防止并发访问带来的问题,比如:
    import java.util.Collections;
    import java.util.LinkedList;
    import java.util.Queue;
    ...
    Queue<Event> eq = (Queue<Event>) Collections.synchronizedList(new LinkedList<Event>());
    eq.offer(new Event());  //入队
    Event e = eq.poll();  //出队
      

  7.   

    当然可以.
    Runnable是一个接口,里面只有一个抽象方法run();
    Thread类实现了Runnable接口.但是,实际上实现的是一个空方法,里面没有任何代码.
    我们当然可以写一个什么类来实现Runnable方法,或者写一个类继承Thread方法,然后覆盖Thread中的run方法.
    基于第二种情况,调用run方法,有两种方式,一种是楼主问的那种方法,一种是调用Thread的start方法.两钟方法最显著的区别是,调用他们的线程是不相同的.