128 java multithreading exception java-threads
我有2个矩阵,我需要将它们相乘,然后打印每个单元格的结果.一旦一个单元格准备就绪,我需要打印它,但是例如我需要在单元格[2] [0]之前打印[0] [0]单元格,即使[2] [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();
}
Run Code Online (Sandbox Code Playgroud)
打印每个单元格结果的线程:
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] + " ");
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在它抛出了我这些例外:
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)
Run Code Online (Sandbox Code Playgroud)
第49行multiplyThread是"notify()"..我想我需要使用不同的同步,但我不知道如何.
如果有人可以帮助这些代码工作,我将非常感激.
Bom*_*mbe 212
为了能够调用notify(),您需要在同一个对象上进行同步.
synchronized (someObject) {
someObject.wait();
}
/* different thread / object */
synchronized (someObject) {
someObject.notify();
}
Run Code Online (Sandbox Code Playgroud)
小智 63
在Java中使用wait和notify或notifyAll方法时,必须记住以下内容:
notifyAll而不是notify. wait和notify方法必须在同步上下文中调用.请参阅链接以获取更详细的说明. wait()在循环中调用该方法,因为如果多个线程正在等待锁定并且其中一个线程获得锁定并重置条件,那么其他线程需要在它们唤醒之后检查条件以查看它们是否需要再次等待或可以开始处理. wait()和notify()方法; 每个对象都有自己的锁,因此调用wait()对象A和notify()对象B将没有任何意义.Bri*_*new 20
你需要解决这个问题吗?我想知道你的矩阵有多大,一个线程打印是否有任何好处,另一个是乘法.
也许在进行相对复杂的线程工作之前测量这段时间是否值得?
如果你确实需要线程化,我会创建'n'个线程来执行单元格的乘法运算(也许'n'是可用的核心数),然后使用ExecutorService和Future机制同时分派多个乘法.
这样您就可以根据内核数量优化工作,并且您正在使用更高级别的Java线程工具(这应该会让生活变得更轻松).将结果写回接收矩阵,然后在完成所有Future任务后打印.
Max*_*tin 14
假设您有一个"黑匣子"应用程序,其中一些名为BlackBoxClass具有方法的类doSomething();.
此外,您有一个名字的观察者或听众onResponse(String resp),将BlackBoxClass在未知时间之后调用.
流程很简单:
private String mResponse = null;
...
BlackBoxClass bbc = new BlackBoxClass();
bbc.doSomething();
...
@override
public void onResponse(String resp){
mResponse = resp;
}
Run Code Online (Sandbox Code Playgroud)
让我们说我们不知道发生了什么,什么BlackBoxClass时候应该得到答案,但你不想继续你的代码,直到你得到答案或换句话说得到onResponse电话.这里输入'Synchronize helper':
public class SyncronizeObj {
public void doWait(long l){
synchronized(this){
try {
this.wait(l);
} catch(InterruptedException e) {
}
}
}
public void doNotify() {
synchronized(this) {
this.notify();
}
}
public void doWait() {
synchronized(this){
try {
this.wait();
} catch(InterruptedException e) {
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我们可以实现我们想要的:
public class Demo {
private String mResponse = null;
...
SyncronizeObj sync = new SyncronizeObj();
public void impl(){
BlackBoxClass bbc = new BlackBoxClass();
bbc.doSomething();
if(mResponse == null){
sync.doWait();
}
/** at this momoent you sure that you got response from BlackBoxClass because
onResponse method released your 'wait'. In other cases if you don't want wait too
long (for example wait data from socket) you can use doWait(time)
*/
...
}
@override
public void onResponse(String resp){
mResponse = resp;
sync.doNotify();
}
}
Run Code Online (Sandbox Code Playgroud)
您只能在拥有其监视器的对象上调用notify.所以你需要类似的东西
synchronized(threadObject)
{
threadObject.notify();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
229013 次 |
| 最近记录: |