我并不精通多线程。我试图通过一个生产者线程重复截取屏幕截图,它将BufferedImage
对象添加到ConcurrentLinkedQueue
,而消费者线程将poll
队列用于BufferedImage
对象以将它们保存在文件中。我可以通过重复轮询(循环)消耗它们,但我不知道如何使用notify()
和wait()
消耗它们。我曾尝试在较小的程序中使用wait()
和notify
,但在此处无法实现。
我有以下代码:
class StartPeriodicTask implements Runnable {
public synchronized void run() {
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e1) {
e1.printStackTrace();
}
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit()
.getScreenSize());
BufferedImage image = robot.createScreenCapture(screenRect);
if(null!=queue.peek()){
try {
System.out.println("Empty queue, so waiting....");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}else{
queue.add(image);
notify();
}
}
}
public class ImageConsumer implements Runnable {
@Override
public synchronized void run() {
while (true) {
BufferedImage bufferedImage = null;
if(null==queue.peek()){
try {
//Empty queue, so waiting....
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}else{
bufferedImage = queue.poll();
notify();
}
File imageFile = getFile();
if (!imageFile.getParentFile().exists()) {
imageFile.getParentFile().mkdirs();
}
try {
ImageIO.write(bufferedImage, extension, imageFile);
//Image saved
catch (IOException e) {
tracer.severe("IOException occurred. Image is not saved to file!");
}
}
}
以前我有一个重复的民意调查来检查BufferedImage
对象的存在。现在我已经将run
方法更改为synchronised
并尝试实现wait()
和notify()
。我做得对吗?请帮忙。谢谢。
你使用错误的Queue
来完成这项工作。 ConcurrentLinkedQueue
是一个非阻塞队列,这意味着没有生成者消费者语义。如果你只是做一个读者和一个作家,看看SynchronousQueue
简单地说你的代码可以像这样重写
BlockingQueue<?> queue = new SynchrnousQueue<?>();
class StartPeriodicTask implements Runnable {
public void run() {
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e1) {
e1.printStackTrace();
}
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit()
.getScreenSize());
BufferedImage image = robot.createScreenCapture(screenRect);
queue.offer(image); //1
}
public class ImageConsumer implements Runnable {
@Override
public void run() {
while (true) {
BufferedImage bufferedImage = queue.poll(); //2
File imageFile = getFile();
if (!imageFile.getParentFile().exists()) {
imageFile.getParentFile().mkdirs();
}
try {
ImageIO.write(bufferedImage, extension, imageFile);
//Image saved
catch (IOException e) {
tracer.severe("IOException occurred. Image is not saved to file!");
}
}
真的是这样的。
让我解释。在第//行,生产线程将“放置”在队列上的图像。我引用的地方是因为SynchrnousQueue没有深度。实际发生的是线程告诉队列“如果有任何线程要求来自此队列的元素然后给它该线程并让我继续。如果不是,我会等到另一个线程准备好”
第// 2行与1类似,其中消费线程只等待线程提供。这对于单读者单作者来说非常有用
第一个问题是您在生产者中不必要的等待:
if(null!=queue.peek()){ // You are the producer, you don't care if the queue is empty
try {
System.out.println("Empty queue, so waiting....");
wait(); // This puts you to bed, your waiting and so is your consumer
} catch (InterruptedException e) {
e.printStackTrace();
}
}else{
queue.add(image);
notify();
}
这就是你应该需要的:
queue.add(image);
notify();
下一个问题是消费者中不必要的notify
。它在那时产生了对其处理的控制,我相信你的目的是让你的生产者继续前进,但当然你的代码永远不会达到这一点。所以这:
}else{
bufferedImage = queue.poll();
notify();
}
File imageFile = getFile();
if (!imageFile.getParentFile().exists()) {
imageFile.getParentFile().mkdirs();
}
try {
ImageIO.write(bufferedImage, extension, imageFile);
//Image saved
catch (IOException e) {
tracer.severe("IOException occurred. Image is not saved to file!");
}
}
应该看起来更像这样:
}else{
bufferedImage = queue.poll();
File imageFile = getFile();
if (!imageFile.getParentFile().exists()) {
imageFile.getParentFile().mkdirs();
}
try {
ImageIO.write(bufferedImage, extension, imageFile);
//Image saved
catch (IOException e) {
tracer.severe("IOException occurred. Image is not saved to file!");
}
}
一旦java.util.concurrent
库进入JDK1.5,就需要编写自己的等待/通知逻辑。在2012年,如果您正在进行自己的等待/通知,那么您工作太辛苦了,应该强烈考虑尝试过的真实的java.util.concurrent等价物。
话虽这么说,我相信民意调查是内置java.util.concurrent.ConcurrentLinkedQueue
背后的想法。换句话说,只要它是!isEmpty()
,消费者就会坐在他们自己的Thread和.poll()项目中来自ConcurrentLinkedQue。我见过的大多数实现在!isEmpty()
的测试之间抛出了一秒钟的睡眠,但我不认为这实际上是必要的。另外,请注意Vint家伙对我的回答的评论,.poll()
可能会返回null
。考虑java.util.AbstractQueue
的替代实现,它可能具有更接近您正在寻找的阻塞行为。
这家伙有一个简单的例子:http://www.informit.com/articles/article.aspx?p=1339471&seqNum=4
最后,获取Goetz的书“Java Concurrency In Practice”并阅读它。我几乎可以肯定它有一个用于替换你自己的本土等待/通知的方法。