Java:使用BlockingQueue的生产者/消费者:使用消费者线程wait()直到另一个对象排队(Java: Producer/Consumer using BlockingQueue: having the consumer thread wait() until another object is queued)
我最近遇到了一些线程相关的问题,消费者需要积分。 这是原始的,除了占用大量的cpu不断检查队列之外,它工作正常。 这个想法是可以随便调用cuePoint并且主线程继续运行。
import java.util.List; import java.util.ArrayList; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; public class PointConsumer implements Runnable { public static final int MAX_QUEUE_SIZE=500; BlockingQueue<Point> queue; public PointConsumer (){ this.queue=new ArrayBlockingQueue<Point>(MAX_QUEUE_SIZE); } public void cuePoint(Point p){ try{ this.queue.add(p); } catch(java.lang.IllegalStateException i){} } public void doFirstPoint(){ if(queue.size()!=0){ Point p=queue.poll(); //operation with p that will take a while } } public void run() { while(true){ doFirstPoint(); } } }
我试图通过每次调用cue函数时添加notify()来修复cpu问题,并将doFirstPoint()重新处理为这样的事情:
public void doFirstPoint(){ if(queue.size()!=0){ //operation with p that will take a while } else{ try{ wait(); } catch(InterruptedException ie){} } }
但是,我发现notify()和wait()仅适用于同步函数。 当我使doFirstPoint和cuePoint同步时,调用cuePoint的主线程将保持等待状态。
我有一些想法来解决这个问题,包括将线程作为一个对象并直接通知它,但我不确定这是否会导致比修复更多的问题,是非常糟糕的形式,还是根本无法工作。 我错过了这个问题的简单解决方案吗?
I've been having some thread related problems recently with a consumer that takes points. Here is the original, which works fine except for taking up a lot of cpu constantly checking the queue. The idea is that cuePoint can be called casually and the main thread keeps going.
import java.util.List; import java.util.ArrayList; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; public class PointConsumer implements Runnable { public static final int MAX_QUEUE_SIZE=500; BlockingQueue<Point> queue; public PointConsumer (){ this.queue=new ArrayBlockingQueue<Point>(MAX_QUEUE_SIZE); } public void cuePoint(Point p){ try{ this.queue.add(p); } catch(java.lang.IllegalStateException i){} } public void doFirstPoint(){ if(queue.size()!=0){ Point p=queue.poll(); //operation with p that will take a while } } public void run() { while(true){ doFirstPoint(); } } }
I tried to fix the cpu issue by adding notify() every time the cue function is called, and re-working doFirstPoint() to something like this:
public void doFirstPoint(){ if(queue.size()!=0){ //operation with p that will take a while } else{ try{ wait(); } catch(InterruptedException ie){} } }
However, I found out that notify() and wait() only work in synchronized functions. When I make doFirstPoint and cuePoint synchronized, the main thread which has called cuePoint will be kept waiting.
I had a few ideas to get around this, including making the thread an object and having it be directly notified, but I wasn't sure if that would cause more problems than it would fix, be very bad form, or simply not work. Is there a simple solution to this problem that I'm missing?
原文:https://stackoverflow.com/questions/5252141
最满意答案
根据JEP 261 , - module
--module-source-path
选项(用于“多模块模式”中的编译)必须指向一个目录,该目录包含每个包含模块的一个子目录,其中目录名称必须等于模块名称。为了适应源模块未直接包含在模块目录中的布局,该选项支持模式 ,其中token
*
可用于表示路径任何部分中的模块名称,例如"./*/src/main/java/"
,它将在./my.mod1/src/main/java/module-info.java
等中找到模块./my.mod1/src/main/java/module-info.java
。JEP 261没有提到关于模式
*
可能出现在哪里的任何约束,但显然javac
不喜欢以*
开头的模式。 这可能是也可能不是故意的。稍微相关,我可能会补充一点,在之前的讨论中,我被告知JEP 261包含过时的信息,但我的问题是,在JEP完成后是否以及在何处维持此规范,没有产生任何答案。 javac手动条目不是为
--module-source-path
等选项提供足够详细信息的地方。As per JEP 261 the
--module-source-path
option (for compilation in "multi-module mode") must point to a directory that holds one subdirectory for each contained module, where the directory name must equal the module name.To accommodate layouts where sources are not directly contained in the module directory, the option supports patterns where the token
*
can be used to represent the module name in any part of the path such as in"./*/src/main/java/"
, which will find the modulemy.mod1
in./my.mod1/src/main/java/module-info.java
etc.JEP 261 does not mention any contraints on where in the pattern
*
may occur, but apparentlyjavac
doesn't like patterns starting with*
. This may or may not be intentional.Slightly related, I might add that in a previous discussion I was informed that JEP 261 contains outdated information, but my question whether and where this specification would be maintained after the JEP was completed, produced no answer. The javac manual entry is not the place that gives sufficient details for options like
--module-source-path
.
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
一种方法是简单地修改你的路径 : import sys sys.path.append('C:/full/path') from foo import util,bar 请注意,这需要foo作为一个python包,即包含一个__init__.py文件。 如果您不想修改sys.path ,还可以修改PYTHONPATH环境变量或在系统上安装该模块 。 请注意,这意味着该目录中的其他目录或.py文件可能无意中加载。 因此,您可能需要使用imp.load_source 。 它需要文件名,而不是目录(允许当前用户 ...
-
首先检查被调用的编译器版本: javac -version 可能会使用以前的安装。 在这种情况下,调整PATH环境变量,以便Java 9安装的路径是它的开始。 (或者在调用javac时使用完整路径,如\some\dir\java\jdk9\bin\javac ) 我使用以下批处理文件(Windows)将我的环境更改为Java 9: @set JAVA_HOME=%JDK9_HOME% @PATH .;%JAVA_HOME%\bin;%PATH% @java -version 已经将JDK9_HOME设置 ...
-
根据JEP 261 , - module --module-source-path选项(用于“多模块模式”中的编译)必须指向一个目录,该目录包含每个包含模块的一个子目录,其中目录名称必须等于模块名称。 为了适应源模块未直接包含在模块目录中的布局,该选项支持模式 ,其中token *可用于表示路径任何部分中的模块名称,例如"./*/src/main/java/" ,它将在./my.mod1/src/main/java/module-info.java等中找到模块./my.mod1/src/main/java/ ...
-
我认为最后一个(根目录中的声明)是正确的,它必须正常工作,尽管它会在IDE中引发警告。 它没有说,声明它的首选方式是什么, 官方文档中只有一个例子,它用作: sourceCompatibility = 1.6 在build.gradle的根目录中。 I think that the last one(declaration in the root) is the correct one and it has to work properly, though it causes warnings in ID ...
-
从模块名称获取路径(get path from a module name)[2023-11-11]
sys.modules['path.to.module'].__file__ Python 2.7.2+ (default, Oct 4 2011, 20:06:09) [GCC 4.6.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import collections >>> import sys >>> sys.modules['collections'].__file_ ... -
在sys.path中添加路径 - > sys.path.append('path_to_module') import语句搜索sys.path中的路径列表 sys.path始终包含在命令行上调用的脚本的路径,并且与命令行上的工作目录无关。 导入包在概念上与导入该包的init .py文件相同 阅读更多 : https://chrisyeh96.github.io/2017/08/08/definitive-guide-python-imports.html add the path in sys.path - ...
-
我发现了我的问题。 --module-path标志应指向已编译(已构建)的模块。 对于Ant,这是构建目录。 对于源文件(我最初的问题是尝试做什么),请使用--module-source-path 。 有关模块系统JEP的更多信息。 I found my problem. The --module-path flag should point to compiled (built) modules. In the case of Ant, this is the build directory. For s ...
-
注1 -使用gwt标准时,请使用您的文件夹结构 src/main/java/showcase/web/gwt/yourmodule/yourmodule.gwt.xml src/main/java/showcase/web/gwt/yourmodule/client src/main/java/showcase/web/gwt/yourmodule/public Your pom references
showcase.web.gwt.addressb ... -
从Java 7开始,您可以使用NIO Files API执行此操作(旧的File API也可以使用,但使用NIO可以更好地完成相对路径分辨率) File file = Paths.get("../../pom.xml").toFile(); 要确保文件正确,您可以调用toRealPath()来解析相对路径段 File file = Paths.get("../../pom.xml").toRealPath().toFile(); 如果你有一个工作目录,你可以从中解决相对路径: File file = P ...