此代碼是着名的8皇后難題不同的實施嘗試。我試圖用多線程爲此工作。以下代碼段是迄今爲止的實現。但有一個問題,等待方法永遠等待主線程。我添加了一些SOUT以使測試變得簡單,因此它確認它被卡住了。通知和等待不起作用的同步塊
主類:
public class MainClass {
public static void main(String[]args)
{
Queen.board[1][3]=true;
Queen queen=new Queen();
queen.placeNextQueen();
}
}
女王級
public class Queen {
private static final Object syncOb=new Object();
public static boolean[][]board=new boolean[10][10];
public static int onBoard=0;
private int callbacks=1;
Thread runRow;
Thread runCol;
Thread runLDiag;
Thread runRDiag;
boolean rowSafe=true;
boolean colSafe=true;
boolean rDiagSafe=true;
boolean lDiagSafe=true;
public Queen()
{
}
public void placeNextQueen()
{
final Queen queen=this;
if(++onBoard<8)
{
for(int i=0;i<7;i++)
{
System.out.println("*******");
callbacks=1;
for(int r=0;r<7;r++)
{
final int finalI = i;
final int finalR = r;
runRow=new Thread() {
@Override
public void run() {
isRowSafe(queen,finalI);
}
};
runCol=new Thread() {
@Override
public void run() {
isColSafe(queen,finalR);
}
};
runRDiag=new Thread() {
@Override
public void run() {
isRDiagSafe(queen,finalI,finalR);
}
};
runLDiag=new Thread() {
@Override
public void run() {
isLDiagSafe(queen,finalI,finalR);
}
};
try
{
runRow.run();
runCol.run();
runRDiag.run();
runLDiag.run();
synchronized(syncOb) {
syncOb.wait();
System.out.println("WAIT OVER*****************");
}
if(rowSafe && colSafe && rDiagSafe && lDiagSafe)
{
board[i][r]=true;
}
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("INNER LOOP OVER*****************");
}
System.out.println("TO SHOW BOARD*****************");
showBoard();
}
}
public void showBoard() {
System.out.println("SHOW BOARD*****************");
for(int i=0;i<8;i++)
{
System.out.print("|");
for(int r=0;r<8;r++)
{
if(board[i][r])
{
System.out.print("*");
}
else
System.out.print(" ");
System.out.print("|");
}
System.out.println();
}
}
public void callBack()
{
System.out.println("CALLBACK*****************"+rowSafe+" "+colSafe+" "+rDiagSafe+" "+lDiagSafe+" "+callbacks);
if(callbacks++ ==4||(!rowSafe && !colSafe && !rDiagSafe && !lDiagSafe))
{
runRow.interrupt();
runCol.interrupt();
runRDiag.interrupt();
runLDiag.interrupt();
synchronized (syncOb) {
System.out.println("NOTIFY*****************");
syncOb.notifyAll();
System.out.println("NOTIFYed*****************");
}
}
}
public void isRowSafe(Queen q,int row)
{
System.out.println("------------ SAFE");
for(int i=0;i<7;i++)
{
System.out.println("----------- LOOP");
if(board[row][i])
{
System.out.println("--------- IF");
rowSafe= false;
}
}
rowSafe= true;
q.callBack();
}
public void isColSafe(Queen q,int col)
{
System.out.println("||||||||| SAFE");
for(int i=0;i<7;i++)
{
System.out.println("||||||||| LOOP");
if(board[i][col])
{
System.out.println("||||||||| IF");
colSafe = false;
}
}
colSafe= true;
q.callBack();
}
public void isRDiagSafe(Queen q,int row, int col)
{
int initRow=row;
int initCol=col;
System.out.println("////////// SAFE");
//up diagonal
if(row!=0)
for (int i=initRow-1;i>=0;i--)
{
System.out.println("///////// UP"+i+","+col);
if(++col>7)
{
rDiagSafe = true;
q.callBack();
return;
}
if(board[i][col])
rDiagSafe= false;
}
col=initCol;
//down diagonal
if(row!=7)
for(int i=initRow+1;i<8;i++)
{
System.out.println("/////////// DOWN"+i+","+col);
if(--col<0) {
rDiagSafe = true;
q.callBack();
return;
}
if(board[i][col])
rDiagSafe= false;
}
q.callBack();
}
public void isLDiagSafe(Queen q,int row, int col)
{
System.out.println("DDDDDDDDDDDDDDD SAFE");
int initRow=row;
int initCol=col;
//up diagonal
if(row!=0)
for (int i=initRow-1;i>=0;i--)
{
System.out.println("DDDDDDDDDDDDDDD UP");
if(--col>7) {
lDiagSafe = true;
q.callBack();
return;
}
if(board[i][col])
lDiagSafe= false;
}
col=initCol;
//down diagonal
if(row!=7)
for(int i=initRow+1;i<8;i++)
{
System.out.println("DDDDDDDDDDDDDDD DOWN");
if(++col<0) {
lDiagSafe = true;
q.callBack();
return;
}
if(board[i][col])
lDiagSafe= false;
}
q.callBack();
}
}
我看不出這裏有什麼問題,但線程沒有醒來。請有人幫我弄清楚故障。
您在此代碼中未創建任何線程,請使用runRow.start() –