可否对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]
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]
楼主【denverbenjamin2000】截止到2008-06-25 19:24:01的历史汇总数据(不包括此帖):
发帖数:10 发帖分:720
结贴数:7 结贴分:470
未结数:3 未结分:250
结贴率:70.00 % 结分率:65.28 %
楼主加油
private synchronized void recieveRemoteEvent(Event aEvent) {
我做了一个小实验,就是让两个线程在运行中互相引用对方的成员,还是可以并发起来的啊
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");
}
}
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(); //出队
Runnable是一个接口,里面只有一个抽象方法run();
Thread类实现了Runnable接口.但是,实际上实现的是一个空方法,里面没有任何代码.
我们当然可以写一个什么类来实现Runnable方法,或者写一个类继承Thread方法,然后覆盖Thread中的run方法.
基于第二种情况,调用run方法,有两种方式,一种是楼主问的那种方法,一种是调用Thread的start方法.两钟方法最显著的区别是,调用他们的线程是不相同的.