- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试最近学到的一些多线程概念,但无法运行它。
它给出了 IllegalMonitorStateException 但没有弄清楚错误发生的原因。
因此,关于代码2线程引用名称填充器和写入器都共享一个数组列表,填充器正在用整数填充数组列表(但最多填充30个),并且在填充器和写入器线程添加每个数字后读取数组列表获取该项目将其写入文件将其从数组列表中删除。为了更好地理解,在代码中添加了注释。
package com.utsav.pratice;
import java.io.*;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
//shared arraylist-synchronized
ArrayList<Integer> integerArrayList = new ArrayList<>();
//writter will write to this file numbers added to arraylist by filler and than will remove it from arraylist
FileOutputStream file = new FileOutputStream("Numbers.txt");
//filler will mark it true after completing all numbers
final boolean[] numbersCompleted = {false};
Thread filler=new Thread(new Runnable(){
@Override
public void run(){
//1-30 numbers one by one will be written to Arraylist
for (int i = 1; i < 31; i++) {
synchronized (integerArrayList) {
//if arraylist is not empty that means writter not performed on arraylist hence invoking wait to release lock so writter could perform
while(!integerArrayList.isEmpty()){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//so arraylist is empty now lets fill it,notify that releasing lock and than put thread to sleep
integerArrayList.add(i);
System.out.println("Number added");
if(i==30){
numbersCompleted[0] =true;}
notifyAll();
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Numbers adding completed");
}
});
Thread writter=new Thread(new Runnable(){
@Override
public void run(){
//if numbers are completed than nothing to write come out of loop
while(!numbersCompleted[0]) {
synchronized (integerArrayList) {
//if arraylist is empty than its time for filler to work hence putting thread to wait so lock could be released for filler
while (integerArrayList.isEmpty()){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//so arraylist is not empty now lets write it & than remove it from arraylist,notify that releasing lock and than put thread to sleep
try (DataOutputStream fileWritter = new DataOutputStream(new BufferedOutputStream(file));) {
fileWritter.writeInt(integerArrayList.get(0));
System.out.println("Random number written");
} catch (IOException e) {
e.printStackTrace();
}
integerArrayList.remove(0);
notifyAll();
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("File written completely");
}
});
//starting both threads-2cases filler takes the key-ok(will fill & wait) or filler takes the key(will wait since will be empty)
writter.start();
filler.start();
最佳答案
这里:
synchronized (integerArrayList)
您正在列表上进行同步。
但是您正在等待/通知您的匿名线程对象的this!作为 javadoc 中的第一个信息说:
抛出该异常表示线程尝试在对象的监视器上等待,或通知在不拥有指定监视器的情况下在对象的监视器上等待的其他线程。
所以当你将它们更改为
时,事情应该会起作用integerArrayList.wait()
例如(所有使用 wait/notify/...)!
并提示:不要做类似的事情
final ArrayList<Integer> integerArrayList = new ArrayList<>();
只是去
List<Integer> numbers = new ArrayList<>();
简单地不使用具体的 impl 类类型作为变量的类型;而且也不作为其名称的一部分!
关于Java多线程代码给出错误-Illegal Monitor Exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42573841/
我是 iOS 开发新手。我正在解决的是如何在 iPhone 中运行我的应用程序时获取有关该应用程序的信息。 例如:当我在 Android 中开发时,我连接手机(使用净化模式)并打开 Android M
我仍然不确定这两个调用之间的区别。来自 MSDN, Monitor.Enter(Object) 获取指定对象的独占锁。 Monitor.Wait(Object) 释放对象上的锁并阻塞当前线程,直到它重
我是 GCP 的新手并且来自 Azure 背景。在 GCP 端是否有等效的“Azure Application Insights”用于监控应用程序? 让我用一个例子更清楚地解释我的用例:如果我有一个基
这是说明问题的最小代码: StringBuilder input = new StringBuilder(); void ToUpper() { lock (input) {
我在 ASP.NET 中有一个生产者-消费者场景。我设计了一个Producer 类,一个Consumer 类和一个用于保存共享对象并负责Producer 和Consumer 之间通信的类,我们称它为M
是否可以检测是否是同一个线程试图释放锁?我们在代码中有很多地方看起来像: try { try { if(!Monitor.TryEnter(obj, 2000))
我对并发编程有点陌生,正在尝试了解使用 Monitor.Pulse 和 Monitor.Wait 的好处。 MSDN 的例子如下: class MonitorSample { const in
如果您想在退出和清理对象时阻止执行某些代码块,是否可以使用锁来阻止执行? Monitor.TryEnter(cleanupLock, ref acquiredLock); TryEnter 可用于确保
Monitor.Enter 和 Monitor.Exit 设计为从同一线程调用。但是,如果我需要在与获得的线程不同的线程中释放锁怎么办? 例如:有共享资源和使用该资源的异步操作。该操作以 BeginO
Monitor.PulseAll 通知队列中的所有等待线程。 Monitor.Pulse 通知等待队列中的一个线程。 (下一个等待线程) 只有下一个线程(一个线程)才能获取锁。那有什么区别呢? 什么时
我正在尝试在我的 terraform 代码库中集成对 sshd 进程的 Datadog 监视器检查,但我收到 datadog_monitor.host_is_up2: error updating m
这里的问题是:如果获取对象独占锁的线程(例如通过使用 Monitor.Enter)终止,是否会神奇地释放该对象的独占锁?如果那是真的,那么假设我们从另一个线程调用 Monitor.Exit - 因为我
我正在研究 .NET 中的 Monitor 类,所以我找到了一段似乎可以正常工作的代码,但是当我将它循环一段时间时,它会抛出 OutOfMemoryException。 我在具有 8 GB RAM 的
ECMA-335 规范规定如下: *获取锁(System.Threading.Monitor.Enter 或进入同步方法)应隐式执行 volatile 读取操作,并释放锁(System.Threadi
我在 dll 中使用 OmniThreadLibrary 2.09,主应用程序和 dll 使用相同的 SimpleShareMem 内存管理器。 我用以下代码创建了自己的监视器: FMonitor
我正在使用 R 包 monitoR并收到一条我无法理解的错误消息。 我正在尝试使用 dbUploadTemplate 命令将关联模板列表(“bithTemps”)上传到 MySQL 数据库(“noh”
我想我遗漏了一些关于 Monitor.Enter 和 Monitor.TryEnter 正确行为的信息。这是我编写的一段代码,用于将问题与其余代码分开: object lockObj = new ob
我正在尝试实现一个多线程库,该库将使用线程池同时运行任务。基本上它会从它收到的收集参数中将任务添加到线程池,然后等待直到正在处理的最后一个任务发送脉冲信号。我在早期的测试中取得了成功,但是当我想测试处
我想用redis lua来实现monitor命令,而不是redis-cli monitor。但我不知道怎么办。 redis.call('monitor') 不起作用。 最佳答案 您不能从 Redis
根据语言规范,lock(obj) statement; 会被编译为: object lockObj = obj; // (the langspec doesn't mention this var,
我是一名优秀的程序员,十分优秀!