concurrency

DelayQueue implementation in Java

南笙酒味 提交于 2021-01-27 07:07:59
问题 I'm a beginner in understanding threads and concurrency package. Understood that to use java.util.concurrent.DelayedQueue we have to implement the java.util.concurrent.Delayed interface and it has a abstract method getDelay() long getDelay(TimeUnit unit) Returns the remaining delay associated with this object, in the given time unit. @Override public long getDelay(TimeUnit unit) { long diff = startTime - System.currentTimeMillis(); // long variable return unit.convert(diff, TimeUnit

Running CompletableFuture.thenAccept from the calling thread?

允我心安 提交于 2021-01-27 06:28:31
问题 I want to call CompletableFuture.supplyAsync() to delegate a blocking task to another thread. Once that task completes I would like for the CompletableFuture.thenAccept consumer to run in the context of the calling thread. For example: // Thread 1 CompletableFuture.supplyAsync(() -> { // Thread 2 return BlockingMethod(); }).thenAccept(( Object r) -> { // Thread 1 }); The following code suggests that CompletableFuture.thenAccept runs in its own thread; probably the same pool as

How to enforce only one running instance of a process in python Django framework?

别等时光非礼了梦想. 提交于 2021-01-27 06:17:47
问题 I have a python Django manage command that should be called upon receiving an input file but this command is not safe for parallel calls. So an input file should be processed only and only when there is no other file being processed. One solution that I have is to use a lock file. Basically, create a lock file at the start of the process and delete it at the end. I'm worried that if the process crashes the lock file won't be deleted and consequently none of the other files would be processed

Does StampedLock.writeLock() release a read-lock held by the current thread?

删除回忆录丶 提交于 2021-01-27 06:14:06
问题 http://winterbe.com/posts/2015/04/30/java8-concurrency-tutorial-synchronized-locks-examples/ contains this code: StampedLock lock = new StampedLock(); long stamp = lock.readLock(); try { if (count == 0) { stamp = lock.tryConvertToWriteLock(stamp); if (stamp == 0L) { System.out.println("Could not convert to write lock"); stamp = lock.writeLock(); } count = 23; } System.out.println(count); } finally { lock.unlock(stamp); } The author writes: Calling tryConvertToWriteLock() doesn't block but may

Suggested method of handling non-overlapping ranges (e.g. scheduling)

梦想的初衷 提交于 2021-01-27 05:16:18
问题 I've seen this sort of problems a few times and am trying to decide the best way of storing ranges in a non-overlapping manner. For example, when scheduling some sort of resource that only one person can use at a time. Mostly what I've seen is something like this: PERSON ROOM START_TIME END_TIME Col. Mustard Library 08:00 10:00 Prof. Plum Library 10:00 12:00 What's the best way of preventing new entries from overlapping an existing schedule, like say if Miss Scarlet wants to reserve the

Suggested method of handling non-overlapping ranges (e.g. scheduling)

試著忘記壹切 提交于 2021-01-27 05:15:28
问题 I've seen this sort of problems a few times and am trying to decide the best way of storing ranges in a non-overlapping manner. For example, when scheduling some sort of resource that only one person can use at a time. Mostly what I've seen is something like this: PERSON ROOM START_TIME END_TIME Col. Mustard Library 08:00 10:00 Prof. Plum Library 10:00 12:00 What's the best way of preventing new entries from overlapping an existing schedule, like say if Miss Scarlet wants to reserve the

How to make sure that method is executed only once and from one thread only?

独自空忆成欢 提交于 2021-01-26 22:43:37
问题 I have a below method which I want to execute on below conditions: This method should be executed only once. And once it is executed, it cannot be executed again so if anyone tried to execute again, it should return back by logging some useful error message already executed or anything useful. And it should be executed by only one thread only. So if multiple threads are calling below method, then it should be called by only one thread and other threads should wait for initialization to

How to make sure that method is executed only once and from one thread only?

北城余情 提交于 2021-01-26 22:34:31
问题 I have a below method which I want to execute on below conditions: This method should be executed only once. And once it is executed, it cannot be executed again so if anyone tried to execute again, it should return back by logging some useful error message already executed or anything useful. And it should be executed by only one thread only. So if multiple threads are calling below method, then it should be called by only one thread and other threads should wait for initialization to

Is it safe to use the same FluxSink from multiple threads concurrently

我怕爱的太早我们不能终老 提交于 2021-01-24 11:36:10
问题 I know a Publisher must not publish concurrently, but if I use Flux#create(FluxSink) , can I safely call FluxSink#next concurrently? In other words, does Spring have internal magic that ensures proper serial publishing of events even if FluxSink#next is called concurrently? public class FluxTest { private final Map<String, FluxSink<Item>> sinks = new ConcurrentHashMap<>(); // Store a new sink for the given ID public void start(String id) { Flux.create(sink -> sinks.put(id, sink)); } // Called

Is it safe to use the same FluxSink from multiple threads concurrently

一个人想着一个人 提交于 2021-01-24 11:36:07
问题 I know a Publisher must not publish concurrently, but if I use Flux#create(FluxSink) , can I safely call FluxSink#next concurrently? In other words, does Spring have internal magic that ensures proper serial publishing of events even if FluxSink#next is called concurrently? public class FluxTest { private final Map<String, FluxSink<Item>> sinks = new ConcurrentHashMap<>(); // Store a new sink for the given ID public void start(String id) { Flux.create(sink -> sinks.put(id, sink)); } // Called