关于一个同时遍历的问题
我写了一个图的遍历的
现在想加入多线程同时遍历并显示出时间
应该怎样加多线程跟如何写遍历的时间啊
求高手解决
import java.util.*;
import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class GraphTraverse
{
public static void main(String[] args)
{
new JFrame4();
Graph g = new Graph();
}
}
//用于实现深度优先搜索的栈类
class StackX
{
private final int SIZE=20;
private int[] st;
private int top;
public StackX(){
st=new int[SIZE];
top=-1;
}
public void push(int j){
st[++top]=j;
}
public int pop(){
return st[top--];
}
public int peek(){
return st[top];
}
public boolean isEmpty(){
return top==-1;
}
}
//用于实现广度优先搜索的队列类
class Queue
{
private final int SIZE=20;
private int[] queArray;
private int front;
private int rear;
public Queue(){
queArray=new int[SIZE];
front=0;
rear=-1;
}
public void insert(int j){
if(rear==SIZE-1)
rear=-1;
queArray[++rear]=j;
}
public int remove(){
int temp=queArray[front++];
if(front==SIZE)
front=0;
return temp;
}
public boolean isEmpty(){
return ((rear+1==front)||(front+SIZE-1==rear));
}
}
//顶点类
class Vertex
{
public char label;
public boolean wasVisited;
public Vertex(char lab){
label=lab;
wasVisited=false;
}
}
//图类
class Graph
{
String s1="";
String s="";
private final int MAX_VERTS=20;
private Vertex vertexList[];
private int adjMat[][];
private int nVerts;
private StackX theStack;
private Queue theQueue;
public Graph() {
vertexList=new Vertex[MAX_VERTS];
adjMat=new int[MAX_VERTS][MAX_VERTS];
nVerts=0;
for (int j = 0; j < MAX_VERTS; j++){
for (int k = 0; k < MAX_VERTS; k++) {
adjMat[j][k]=0;
}
}
theStack=new StackX();
theQueue=new Queue();
}
//增加一个顶点
public void addVertex(char lab){
vertexList[nVerts++]=new Vertex(lab);
}
//增加一条边
public void addEdge(int start,int end){
adjMat[start][end]=1;
adjMat[end][start]=1;
}
public void displayVertex(int v)
{
s1=vertexList[v].label+"->";
s=s+s1;
}
public String getStr()
{
return s;
}
//得到与v顶点邻接且未访问过的顶点标号
public int getAdjUnvisitedVertex(int v){
for (int j = 0; j < nVerts; j++) {
if(adjMat[v][j]==1&&vertexList[j].wasVisited==false)
return j;
}
return -1;
}
//深度
public void dfs(){
s="";
vertexList[0].wasVisited=true;
displayVertex(0);
theStack.push(0);
while(!theStack.isEmpty()){
int v=getAdjUnvisitedVertex(theStack.peek());
if(v==-1)
theStack.pop();
else{
vertexList[v].wasVisited=true;
displayVertex(v);
theStack.push(v);
}
}
for(int j=0;j<nVerts;j++)
vertexList[j].wasVisited=false;
}
//广度
public void bfs(){
s="";
vertexList[0].wasVisited=true;
displayVertex(0);
theQueue.insert(0);
int v2;
while(!theQueue.isEmpty()){
int v1=theQueue.remove();
while((v2=getAdjUnvisitedVertex(v1))!=-1){
vertexList[v2].wasVisited=true;
displayVertex(v2);
theQueue.insert(v2);
}
}
for (int j = 0; j < nVerts; j++) {
vertexList[j].wasVisited=false;
}
}
}
class JFrame4 implements ActionListener
{
String s1="";
JPanel aw;
JPanel bw;
JPanel cw;
JPanel dw;
JPanel ew;
Graph g = new Graph();
JTextField text1;
JTextField text2;
JTextField text3;
JTextField text4;
public JFrame4()
{
g.addVertex('a');
g.addVertex('b');
g.addVertex('c');
g.addVertex('d');
g.addVertex('e');
g.addEdge(0,1);
g.addEdge(0,2);
g.addEdge(0,3);
g.addEdge(1,4);
g.addEdge(2,3);
g.addEdge(2,4);
aw=new JPanel();
bw=new JPanel();
cw=new JPanel();
dw=new JPanel();
ew=new JPanel();
text1=new JTextField(20);
text2=new JTextField(20);
text3=new JTextField(20);
text4=new JTextField(20);
JFrame f = new JFrame("图");
JButton b1 = new JButton("深度遍历");
JButton b2 = new JButton("广度遍历");
JButton b3 = new JButton("同时遍历并显示时间");
cw.add(b3);
dw.add(new Label("深度遍历时间;"));
ew.add(new Label("广度遍历时间;"));
dw.add(text3);
ew.add(text4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
aw.add(b1);
bw.add(b2);
aw.add(text1);
bw.add(text2);
f.add(aw);
f.add(bw);
f.add(cw);
f.add(dw);
f.add(ew);
f.setLayout(new FlowLayout());
f.setBounds(400,300,400,400);
//f.pack();
f.setVisible(true);
}
public void actionPerformed(ActionEvent f)
{
String cmd=f.getActionCommand();
if(cmd.equals("深度遍历"))
{
g.dfs();
text1.setText(g.getStr());
}
if(cmd.equals("广度遍历"))
{
g.bfs();
text2.setText(g.getStr());
}
}
}