这个JAVA代码是我自己写的。新人。目的是做一个并发的哈密圈寻路系统。但是有好多好多的Null pointer exception和小问题。求高手帮忙!!给大家磕头了!!
这是NODE的代码。
import java.util.*;
/**
* Title: Node.java
* Description: Every Node can do the same job as Worklist down.because this node just process there own solution, so the solution set is not necessary
*
*
* Copyright: Copyright (c) 2001
* Company: Bond University
* @author Chi Zhang
* @version 1.0 25/10/01
*/
class Node implements Runnable
{
protected Graph graph;
protected Vector buffer=new Vector();
protected String name;
protected Set contents; // Information stored at this node
private volatile boolean stop;
private Node dest[];
private Vector sources;
private Vector collection;
private Path oldPath;
private Warning[] allWarn=new Warning[10];
private boolean askEnd = false;
private Message[] messageCollection = new Message[10];
private Iterator nodeIter = contents.iterator();
/**
* * Constructors
*/
public Node(String n, Set c)
{
name = n;
contents = c;
}
public Node(String n)
{
name = n;
}
/**
* some graphiv method;
*/
public void setGraph(Graph g)
{
graph = g;
dest = g.getDestinations(this);
for (int i = 0; i< dest.length;i++)
{
allWarn[i]= new Warning(dest[i]);
}
for (int i = 0; i< dest.length;i++)
{
messageCollection[i]= new Message(dest[i]);
}
}
public Node() { name = ""; }
public String getName() { return name; }
public Object getContents() { return contents; }
public void setContents(Set o) { contents = o; }
public boolean isEmpty() { return (contents == null); }
public boolean equals(Node input){return ((input.getName()).equals(name));}
public String toString() { return getName(); }
public void getOn(){stop =true;}
/**
* it's the input buffer;
*/
public Vector getBuffer(){return buffer;}
/**
* check the path is whether a HamiltonianCycle.
*/
private boolean isHamiltonianCycle(Path p, Graph g)
{
return ( p.hasCycle() && p.length() == g.size()+1 && p.first().equals(p.last()) );
}
/**
* main method in the program. all program about the buffer control.
*/
public void processBuffer() throws InterruptedException
{
Message a = (Message)buffer.get(0);
if (a.getMission().equals(askEnd))//check whether this an end message
{
synchronized(this)
{
buffer.remove(0);
for (int i = 0;i<messageCollection.length;i++)
{
if ((messageCollection[i].getSource()).equals(a.getSource()))//save the last receive message into every sender code.
{
messageCollection[i].setMission(a.getMission());
}
}
notify();
}// wake up the thread(s) waiting for the buffer to empty
}
else
{
synchronized(this)
{
buffer.remove(0);
notify(); // wake up the thread(s) waiting for the buffer to empty
}
Set ss = (Set)a.getMission();//get the input code.
Node n = a.getSource();
if (contents.contains(ss))//if this is already in the solution.
{
for (int i = 0;i<dest.length;i++)
{
if (allWarn[i].getPosition()== n)//set the warning if receive an old solution.
{
allWarn[i].setAnswer(false);
}
}
}
else
{
for (int i = 0;i<messageCollection.length;i++)//change the last receive message
{
if ((messageCollection[i].getSource()).equals(a.getSource()))
{
messageCollection[i].setMission(a.getMission());
}
}
for (int i = 0;i<dest.length;i++)
{
if (allWarn[i].getPosition()== n)
{
allWarn[i].setAnswer(true);
}
}
contents.addAll(ss);
if (!contents.isEmpty())
{
Set out = calcOutSet(this, ss);
contents = out;
for(int i= 0; i<dest.length;i++)
{
Message message = new Message(this, out);
dest[i].add(a);
}
}
}
}
}
/**
* check the solution and send this solution to every linked node.
*/
private Set calcOutSet(Node n, Set in)
{
Set out = new HashSet();
Iterator paths = in.iterator();
while (paths.hasNext())
{
Path p = (Path) paths.next();
if (!isHamiltonianCycle(p, graph))
{
// References to paths stored in sets. Therefore need to make a new path object to
// avoid unwanted changes through aliases
p = new Path(p);
p.add(n);
}
if (propagate(p))
{
out.add(p);
oldPath = p;
}
}
return (out);
}
/**
* check the path whether can be sperate.
*/
private boolean propagate(Path p)
{
return ((!oldPath.equals(p))&&(isHamiltonianCycle(p, graph) || !p.hasCycle()) );
}
public synchronized void add(Message p)
{
buffer.add(p);
notify();
}
/**
* define how to run the each node.
*/
public synchronized void run()
{
try
{
while(!stop)
{
if(buffer.isEmpty()!=true)
{processBuffer();}
else
for (int i = 0; i<dest.length;i++)
{
Message askStop = new Message(this,askEnd);
dest[i].add(askStop);
}
wait();
}
}catch(InterruptedException ie){return;}
}
/**
* define how to finish.
*/
public void finish()
{
synchronized(this)
{
while(checkEnd()!= true||(buffer.isEmpty()!= true)||(otherWarning()!=true))//only if receive no new information and input buffer
try{wait();}catch(InterruptedException ie){} //is empty and receive all and request
stop = true;
notify();
}
}
/**
* check whether recieve no new information
*/
public boolean checkEnd()
{
for(int i = 0; i< allWarn.length; i++)
{
if (allWarn[i].getAnswer()!=true){return false;}
}
return true;
}
/**
* send the end request
*/
public boolean otherWarning()
{
for (int i=0;i<messageCollection.length;i++)
{
if ((messageCollection[i].getMission()).equals(askEnd)!=true){return false;}
}
return true;
}
/**
* use to print
*/
public String toResult()
{
String s = "{";
Iterator solIt = nodeIter;
while (solIt.hasNext())
s += solIt.next() + ", ";
return ( ((s.length() > 1) ? s.substring(0,s.length()-2) : s) + "}");
}
}
/**
* build a class to tell others the warning;
*/
class Warning
{
protected Node position;
protected boolean judge;
/**
* basic stucture.
*/
public Warning(Node n)
{
position=n;
judge = false;
}
public Warning(Node n, boolean b)
{
position = n;
judge = b;
}
/**
* basic method;
*/
public Node getPosition() { return position; }
public boolean getAnswer() { return judge; }
public void setAnswer(boolean a){judge = a;}
}