如何在Java中使用等待和通知而不使用IllegalMonitorStateException?
multiplyThread
printerThread
public void run() { int countNumOfActions = 0; // How many multiplications have we done int maxActions = randomize(); // Maximum number of actions allowed for (int i = 0; i < size; i++) { result[rowNum][colNum] = result[rowNum][colNum] + row[i] * col[i]; countNumOfActions++; // Reached the number of allowed actions if (countNumOfActions >= maxActions) { countNumOfActions = 0; maxActions = randomize(); yield(); } } isFinished[rowNum][colNum] = true; notify();}
public void run(){ int j = 0; // Columns counter int i = 0; // Rows counter System.out.println("The result matrix of the multiplication is:"); while (i < creator.getmThreads().length) { synchronized (this) { try { this.wait(); } catch (InterruptedException e1) { } } if (creator.getmThreads()[i][j].getIsFinished()[i][j] == true) { if (j < creator.getmThreads()[i].length) { System.out.print(creator.getResult()[i][j] + " "); j++; } else { System.out.println(); j = 0; i++; System.out.print(creator.getResult()[i][j] + " "); } } }
Exception in thread "Thread-9" java.lang.IllegalMonitorStateException at java.lang.Object.notify(Native Method) at multiplyThread.run(multiplyThread.java:49)Exception in thread "Thread-6" Exception in thread "Thread-4" java.lang.IllegalMonitorStateException 如果有人能帮助这段代码工作,我会非常感激的。
繁华开满天机
手掌心
哆啦的时光机
相关分类