java notify 使用_java如何使用wait和notify防止IllegalMonitorStateException

一、概述

我有2个矩阵,我需要将它们相乘,然后打印每个单元格的结果。准备好一个单元格后,我就需要打印它,但是例如,即使[2] [0]的结果先准备好,我也需要在单元格[2] [0]之前打印[0] [0]单元格。所以我需要按顺序打印它。因此,我的想法是让打印机线程等待,直到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

at java.lang.Object.notify(Native Method)

at multiplyThread.run(multiplyThread.java:49)

java.lang.IllegalMonitorStateException

at java.lang.Object.notify(Native Method)

at multiplyThread.run(multiplyThread.java:49)

Exception in thread "Thread-5" java.lang.IllegalMonitorStateException

at java.lang.Object.notify(Native Method)

at multiplyThread.run(multiplyThread.java:49)

Exception in thread "Thread-8" java.lang.IllegalMonitorStateException

at java.lang.Object.notify(Native Method)

at multiplyThread.run(multiplyThread.java:49)

Exception in thread "Thread-7" java.lang.IllegalMonitorStateException

at java.lang.Object.notify(Native Method)

at multiplyThread.run(multiplyThread.java:49)

Exception in thread "Thread-11" java.lang.IllegalMonitorStateException

at java.lang.Object.notify(Native Method)

at multiplyThread.run(multiplyThread.java:49)

Exception in thread "Thread-10" java.lang.IllegalMonitorStateException

at java.lang.Object.notify(Native Method)

at multiplyThread.run(multiplyThread.java:49)

Exception in thread "Thread-12" java.lang.IllegalMonitorStateException

at java.lang.Object.notify(Native Method)

at multiplyThread.run(multiplyThread.java:49)

第49行multiplyThread是“ notify()”。我想我需要以不同的方式使用同步,但是我不确定如何使用。

如果有人可以帮助该代码正常工作,我将非常感谢。

二、详解

为了能够调用notify(),您需要在同一对象上进行同步。

synchronized (someObject) {

someObject.wait();

}

/* different thread / object */

synchronized (someObject) {

someObject.notify();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值