Java中的线程不是同时启动,而是按顺序启动
我在用Java编写多线程算法时遇到了一些问题。以下是我得到的:Java中的线程不是同时启动,而是按顺序启动,java,multithreading,parallel-processing,Java,Multithreading,Parallel Processing,我在用Java编写多线程算法时遇到了一些问题。以下是我得到的: public class NNDFS implements NDFS { //Array of all worker threads private Thread[] threadArray; //Concurrent HashMap containing a mapping of graph-states and //algorithm specific state objects (NDFSState) private
public class NNDFS implements NDFS {
//Array of all worker threads
private Thread[] threadArray;
//Concurrent HashMap containing a mapping of graph-states and
//algorithm specific state objects (NDFSState)
private ConcurrentHashMap<State, NDFSState> stateStore;
//Whether the algorithm is done and whether a cycle is found
private volatile boolean done;
private volatile boolean cycleFound;
/**
Constructor that creates the threads, each with their own graph
@param file The file from which we can create the graph
@param stateStore Mapping between graph-states and state belonging to our algorithm
@param nrWorkers Number of working threads we need
*/
public NNDFS(File file, Map<State, NDFSState> stateStore, int nrWorkers) throws FileNotFoundException {
int i;
this.stateStore = new ConcurrentHashMap<State, NDFSState>(stateStore);
threadArray = new Thread[nrWorkers];
for(i=0;i<nrWorkers;i++){
Graph graph = GraphFactory.createGraph(file);
threadArray[i] = new Thread(new NDFSRunnable(graph, i));
}
}
/**
Class which implements a single thread running the NDFS algorithm
*/
class NDFSRunnable implements Runnable{
private Graph graph;
//Neccesary as Java apparently doesn't allow us to get this ID
private long threadId;
NDFSRunnable(Graph graph, long threadId){
this.graph = graph;
this.threadId = threadId;
}
public void run(){
try {
System.out.printf("Thread id = %d\n", threadId);
//Start by executing the blue DFS for the first graph
mcdfsBlue(graph.getInitialState(), threadId);
} catch (CycleFound e) {
//We must catch all exceptions that are thrown from within our thread
//If exceptions "exit" the thread, the thread will silently fail
//and we dont want that. We use 2 booleans instead, to indicate the status of the algorithm
cycleFound = true;
}
//Either the algorithm was aborted because of a CycleFound exception
//or we completed our Blue DFS without finding a cycle. We are done!
done = true;
}
public void mcdfsBlue(State s, long id) throws CycleFound {
if(done == true){
return;
}
//System.out.printf("Thread %d begint nu aan een dfsblue\n", id);
int i;
int counter = 0;
NDFSState state = stateStore.get(s);
if(state == null){
state = new NDFSState();
stateStore.put(s,state);
}
state.setColor(id, Color.CYAN);
List<State> children = graph.post(s);
i = state.incNextBlue();
while(counter != children.size()){
NDFSState child = stateStore.get(children.get(i%children.size()));
if(child == null){
child = new NDFSState();
stateStore.put(children.get(i % children.size()),child);
}
if(child.getLocalColor(id) == Color.WHITE && !child.isRed()){
mcdfsBlue(children.get(i % children.size()), id);
}
i++;
counter++;
}
if(s.isAccepting()){
state.incRedDFSCount();
mcdfsRed(s, id);
}
state.setColor(id, Color.BLUE);
}
public void mcdfsRed(State s, long id) throws CycleFound {
if(done == true){
return;
}
int i;
int counter = 0;
NDFSState state = stateStore.get(s);
state.setPink(id, true);
List<State> children = graph.post(s);
i = state.incNextRed();
while(counter != children.size()){
NDFSState child = stateStore.get(children.get(i%children.size()));
if(child == null){
child = new NDFSState();
stateStore.put(children.get(i%children.size()),child);
}
if(child.getLocalColor(id) == Color.CYAN){
throw new CycleFound();
}
if(!child.isPink(id) && !child.isRed()){
mcdfsRed(children.get(i%children.size()), id);
}
i++;
counter++;
}
if(s.isAccepting()){
state.decRedDFSCountAndWait();
}
state.setRed();
state.setPink(id, false);
}
}
public void init() {}
public void ndfs() throws Result {
int i;
done = false;
cycleFound = false;
for(i=0;i<threadArray.length;i++){
System.out.printf("Launch thread %d\n",i);
threadArray[i].run();
}
try {
for(i=0;i<threadArray.length;i++){
threadArray[i].join();
}
} catch (InterruptedException e) {
}
//We want to show the result by throwing an exception (weird, but yeah :-/)
if (cycleFound) {
throw new CycleFound();
} else {
throw new NoCycleFound();
}
}
public类NNDFS实现NDFS{
//所有工作线程的数组
私有线程[]线程数组;
//包含图形状态映射和
//特定于算法的状态对象(NDFSState)
私有ConcurrentHashMap状态存储;
//算法是否完成,是否找到循环
完成私有化;
私有易失布尔循环基金;
/**
创建线程的构造函数,每个线程都有自己的图
@param file我们可以从中创建图形的文件
@图状态和状态之间的param stateStore映射属于我们的算法
@param nrWorkers我们需要的工作线程数
*/
公共NNDFS(文件文件、映射状态存储、int nrWorkers)引发FileNotFoundException{
int i;
this.stateStore=新的ConcurrentHashMap(stateStore);
threadArray=新线程[nrWorkers];
对于(i=0;i使用threadArray[i].start();
启动线程
如果您使用threadArray[i].run();
,它所做的只是在调用程序所在的同一线程中正常调用该方法。Dis您的意思是调用threadArray[i].start();
而不是.run()
?抛出异常以返回结果?不要习惯这样做,这只是…错了。@SoboLAN是的,我知道,这只是对我们得到的框架的改进。我们也不满意。。。