每当我运行实现可调用的程序时,我都会以顺序形式获得输出。
就像,这是我的程序:
package com.handson;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class WorkSheet_1 implements Callable<String> {
/**
* @param args
*/
private int id;
static int count = 0;
public static String test[] = { "a1" , "a2" , "a3" , "a4" , "a5" , "a6" , "a7" , "a8" ,
"b1" , "b2" , "b3" , "b4" , "b5" , "b6" , "b7" , "b8" ,
"c1" , "c2" …Run Code Online (Sandbox Code Playgroud) 这是我的代码:
class Processor implements Runnable {
private int id;
private Integer interaction;
private Set<Integer> subset;
private static volatile AtomicBoolean notRemoved = new AtomicBoolean(true);
public Object<E> dcp;
public Iterator<Integer> iterator;
public Processor(int id, Integer interaction, Set<Integer> subset, Object<E> dcp, Iterator<Integer> iterator) {
this.id = id;
this.interaction = interaction;
this.subset= subset;
this.dcp = dcp;
this.iterator = iterator;
}
public void run() {
while (Processor.notRemoved.get()){
System.out.println("Starting: " + this.id);
if (this.dcp.PA.contains(this.interaction)){
this.subset.add(this.interaction);
this.dcp.increaseScore(this.subset);
if (!this.subset.contains(this.interaction) && Processor.notRemoved.get()){
Processor.notRemoved.set(false);
iterator.remove();
}
}
System.out.println("Completed: " …Run Code Online (Sandbox Code Playgroud) java concurrency multithreading java.util.concurrent threadpool
我有一个 Manager 类,多个线程向其中注册自己(用于为UUID每个请求生成唯一标识符),提供要处理的有效负载并从管理器获取相应的响应。我用来java.util.concurrent.ExecutorService启动多个线程。这是测试我的管理器功能的实现 -
public class ManagerTest {
public static void main(String[] args) {
try {
Manager myManager = new Manager();
// Start listening to the messages from different threads
myManager.consumeMessages();
int num_threads = Integer.parseInt(args[0]);
ExecutorService executor = Executors.newFixedThreadPool(num_threads);
for (int i = 0; i < num_threads; i++) {
// class implementation is given below
Runnable worker = new MyRunnable(myManager);
executor.execute(worker);
}
executor.shutdown();
// Wait until all threads are finish
while (!executor.isTerminated()) {
}
System.out.println("\nFinished all …Run Code Online (Sandbox Code Playgroud) java concurrency multithreading java.util.concurrent java-threads
我有一个 Spring boot 应用程序,其中有 Hazelcast 用于缓存。当多个实例使用 Hazelcast 进行集群时,我在解锁操作上遇到此异常:
java.lang.IllegalMonitorStateException: Current thread is not owner of the lock! -> Owner: 33ce48f8-dda3-471f-abae-994d25dcc030, thread ID: 55
at com.hazelcast.concurrent.lock.operations.UnlockOperation.unlock(UnlockOperation.java:75) ~[hazelcast-3.11.4.jar!/:3.11.4]
at com.hazelcast.concurrent.lock.operations.UnlockOperation.run(UnlockOperation.java:64) ~[hazelcast-3.11.4.jar!/:3.11.4]
at com.hazelcast.spi.Operation.call(Operation.java:170) ~[hazelcast-3.11.4.jar!/:3.11.4]
at com.hazelcast.spi.impl.operationservice.impl.OperationRunnerImpl.call(OperationRunnerImpl.java:208) [hazelcast-3.11.4.jar!/:3.11.4]
at com.hazelcast.spi.impl.operationservice.impl.OperationRunnerImpl.run(OperationRunnerImpl.java:197) [hazelcast-3.11.4.jar!/:3.11.4]
at com.hazelcast.spi.impl.operationservice.impl.OperationRunnerImpl.run(OperationRunnerImpl.java:413) [hazelcast-3.11.4.jar!/:3.11.4]
at com.hazelcast.spi.impl.operationexecutor.impl.OperationThread.process(OperationThread.java:153) [hazelcast-3.11.4.jar!/:3.11.4]
at com.hazelcast.spi.impl.operationexecutor.impl.OperationThread.process(OperationThread.java:123) [hazelcast-3.11.4.jar!/:3.11.4]
at com.hazelcast.spi.impl.operationexecutor.impl.OperationThread.run(OperationThread.java:110) [hazelcast-3.11.4.jar!/:3.11.4]
Run Code Online (Sandbox Code Playgroud)
这是我进行锁定和解锁操作的组件:
package com.companyname.service.servicename.job;
import com.hazelcast.core.ILock;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class UpdateRaceCacheJob {
@Scheduled(fixedDelay = 10000)
public void updateRaceCache() {
ILock lock = hazelcastInstance.getLock("race-lock");
try {
if (!lock.tryLock()) { …Run Code Online (Sandbox Code Playgroud) AFAIK,Java 中的每个对象都有一个标记字。第一个字(标记字)用于存储锁定信息,如果只有一个线程正在获取锁,则通过标志;如果不同线程之间存在争用,则通过指向锁定监视器对象,并且在这两种情况下,比较和交换结构用于获取锁。
但根据此链接 - https://www.baeldung.com/lmax-disruptor-concurrency
为了处理写入争用,队列通常使用锁,这可能会导致上下文切换到内核。当发生这种情况时,所涉及的处理器可能会丢失其缓存中的数据。
我缺少什么?
我写过程序解决有限的生产者和消费者问题.构建ArrayBlockingQueue我定义的容量100.我正在使用方法take并放入内部线程.而且我注意到有时候我会看到102次与他们之间的任何拍摄.为什么会这样?
生产者运行方法:
public void run() {
Object e = new Object();
while(true) {
try {
queue.put(e);
} catch (InterruptedException w) {
System.out.println("Oj, nie wyszlo, nie bij");
}
System.out.println("Element added");
}
}
Run Code Online (Sandbox Code Playgroud)
消费者运行方法:
public void run() {
while(true) {
try {
queue.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Element removed");
}
}
Run Code Online (Sandbox Code Playgroud)
部分带有输出的uniq -c文件:
102 Element removed
102 Element added
102 Element removed
102 Element added
102 Element removed
102 Element added
102 Element removed
102 Element added …Run Code Online (Sandbox Code Playgroud) 提供否定超时是否安全java.util.concurrent.Future.get(long, TimeUnit)?文件说
如果需要,最多等待计算完成的给定时间,然后检索其结果(如果可用).
这是否意味着它将与负值一起使用,或者此陈述是否仅涵盖非负面案例?我期望的行为是返回结果(如果可用)或立即超时.如果我们想要计算绝对时间点的超时并且已经过去,则可能发生这种情况.我当然可以使用max{timeout, 0},但这真的有必要吗?在我的环境中进行的测试表明它有效,但这有保证吗?/sf/ask/653303311/表明这应该是0超时的情况.
或者换句话说:假设结果Future可用.Future如果一个实现是不合规的,如果get(long, TimeUnit)使用否定超时调用会比返回此结果还要做其他事情吗?
我有两个同步块,将被两个并发线程大量访问.我想减少争用和上下文切换.有没有办法在一个AtomicBoolean?上使用CAS(CompareAndSet)操作来实现?
例如:
private final Object lock = new Object();
// Thread A executing this
public final void methodA() {
synchronized(lock) {
...
}
}
// Thread B executing this
public final void methodB() {
synchronized(lock) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
我不想使用该java.util.concurrent.locks.Lock课程,因为我认为它不会有所作为.我想用CAS.
这个超级简单的应用程序打印"Hello",但没有完成.我完全没有理由这样做.
JavaDoc,部分定稿,说
程序中不再引用且没有剩余线程的池将自动关闭.
tpe显然没有引用,这意味着线程没有完成.但我不明白为什么.有人能解释一下吗
在这种情况下的解决方案是在main结束时调用shutdown(),但我的实际应用程序更复杂.Runnables内部生成了新工作,所以我不知道什么时候会处理所有内容.
那么,我是否需要确定何时调用shutdown?或者是否有可能以某种方式指定,当tpe队列为空时,它应该自行关闭?
public class JavaApplication5 {
public static void main(String[] args) {
ThreadPoolExecutor tpe = new ThreadPoolExecutor(5, 15, 10, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
tpe.execute(new Runnable() {
@Override
public void run() {
System.out.println("Hello");
}
});
}
Run Code Online (Sandbox Code Playgroud)
}
java concurrency multithreading java.util.concurrent threadpoolexecutor
假设我有3个下载,框架为可完成的期货:
CompletableFuture<Doc> dl1 = CompletableFuture.supplyAsync(() -> download("file1"));
CompletableFuture<Doc> dl2 = CompletableFuture.supplyAsync(() -> download("file2"));
CompletableFuture<Doc> dl3 = CompletableFuture.supplyAsync(() -> download("file3"));
Run Code Online (Sandbox Code Playgroud)
然后所有这些都需要以相同的方式处理
CompletableFuture<String> s1 = dl1.thenApply(Doc::getFilename);
CompletableFuture<String> s2 = dl2.thenApply(Doc::getFilename);
CompletableFuture<String> s3 = dl3.thenApply(Doc::getFilename);
Run Code Online (Sandbox Code Playgroud)
您可以想象要应用的多个功能,所有功能都是并行的.
根据DRY原则,这个例子似乎不合适.所以我正在寻找一种解决方案来定义仅执行3次并行执行的工作流程.
如何实现这一目标?
我尝试过allOf,但这有两个问题:1)它开始阻塞,2)返回类型只能run填充而不是处理它.
java concurrency java.util.concurrent java-8 completable-future
java ×9
concurrency ×8
locking ×2
callable ×1
capacity ×1
hazelcast ×1
java-8 ×1
java-threads ×1
jvm ×1
runnable ×1
sonarqube ×1
threadpool ×1