java.util.concurrent

How to wait for data with ReentrantReadWriteLock?

醉酒当歌 提交于 2019-12-24 11:00:56
问题 It is said, that ReentrantReadWriteLock is intended for one writer and multiple readers. Nevertheless, readers should wait until some data is present in the buffer. So, what to lock? I created concurrency objects like follows: private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); protected final Lock readLock = rwl.readLock(); protected final Lock writeLock = rwl.writeLock(); protected final Condition hasData = writeLock.newCondition(); now in write method I do: writeLock

How do I know when ExecutorService has finished if items on the ES can resubmit to the ES

两盒软妹~` 提交于 2019-12-24 04:14:11
问题 My Java application works on music files within folders, it is designed to process multiple folders in parallel and independently. To do this each folder is processed by an ExecutorService that has a maximum pool size that matches no of CPUs of the computer. For example, if we have 8-CPU computer then eight folders can (in theory) be processed concurrently, if we have a 16-CPU computer then 16 folders can be processed concurrently. If we only have 1 CPU then we set pool-size to 3, to allow

Confused by docs and source of CountedCompleter

隐身守侯 提交于 2019-12-24 04:09:28
问题 Here is a code fragment of java.util.concurrent.CountedCompleter class (JDK 1.8.0_25). /** * If the pending count is nonzero, decrements the count; * otherwise invokes {@link #onCompletion(CountedCompleter)} * and then similarly tries to complete this task's completer, * if one exists, else marks this task as complete. */ public final void tryComplete() { CountedCompleter<?> a = this, s = a; for (int c;;) { if ((c = a.pending) == 0) { a.onCompletion(s); if ((a = (s = a).completer) == null) {

Synchronized List/Map in Java if only one thread is writing to it

最后都变了- 提交于 2019-12-24 04:06:47
问题 The first thread is filling a collection continuously with objects. A second thread needs to iterate over these objects, but it will not change the collection. Currently I use Collection.synchronized for making it thread-safe, but is there a fast way to doing it? Update It's simple: The first thread (ui) continuously writes the mouse position to the ArrayList, as long as the mousebutton is pressed down. The second thread (render) draws a line based on the list. 回答1: Even if you synchronize

Creating ScheduledThreadPoolExecutor Using Executors

大兔子大兔子 提交于 2019-12-23 23:07:39
问题 I am extremely confused as to why the following cast does not work: ScheduledThreadPoolExecutor timeoutControl = (ScheduledThreadPoolExecutor) Executors.newSingleThreadScheduledExecutor(); ScheduledThreadPoolExecutor implements the ScheduledExecutorService. What's the point of this Executors call if I can't use it with an actual class. Am I using it wrong (probably), could someone offer some guidance please? 回答1: The problem is that Executors.newSingleThreadScheduledExecutor(); actually doesn

How to lazily create tasks for use by a Java thread-pool

独自空忆成欢 提交于 2019-12-23 22:27:21
问题 I'm writing a load-testing application in Java, and have a thread pool that executes tasks against the server under test. So to make 1000 jobs and run them in 5 threads I do something like this: ExecutorService pool = Executors.newFixedThreadPool(5); List<Runnable> jobs = makeJobs(1000); for(Runnable job : jobs){ pool.execute(job); } However I don't think this approach will scale very well, because I have to make all the 'job' objects ahead of time and have them sitting in memory until they

How to cancel ShceduledFuture and wait for runnable to stop, if runnable is in progress at the moment of cancellation?

十年热恋 提交于 2019-12-23 19:31:50
问题 When any command scheduled with fixed rate at any ScheduledExecutorService, it returns ScheduledFuture which can be cancelled as well. But "cancel" does not provide guarantee that command is not still executing after cancel returns, for example because command was already in the middle of execution when "cancell" was called. For mostly use cases it is enough functionality. But I have deal with usecase when need to block current thread after cancel, if command already is in progress, and wait

Calling Different Webservices in parallel from Webapp

让人想犯罪 __ 提交于 2019-12-23 13:52:08
问题 We've got a stipes (java) web-app that needs to make about 15 different webserivce calls all from one method. For example: ... public Resolution userProfile() { serviceOneCall(); serviceTwoCall(); serviceThreeCall(); serviceFourCall(); .... serviceTenCall(); return new RedirectResolution("profiel.jsp"); } All of these can be called in parallel and are not dependent on each other. The one thing that most all of these calls are doing is putting data in the session, and one or two may put data

java - stealing bits from references

强颜欢笑 提交于 2019-12-23 09:33:05
问题 How do I steal 2 MSBs from an address to do an atomic operation? I'm trying to do a single word CAS An example public class Node { long key; long value; Node lchild; // format is flag1,flag2,address Node rchild; // format is flag1,flag2,address } public void createNode() { Node n1 = new Node(); //this should create a node with format 0,0,address1 } public void setFlag1(Node n1) { Now the new address should be in format 1,0,address1 } public void setFlag2(Node n1) { Now the new address should

Concurrent Map with fixed size

两盒软妹~` 提交于 2019-12-23 07:00:54
问题 I need a map with the following requirements : It should be highly concurrent. The put() , get() and remove() methods may be called by multiple threads simultaneously. It should be of fixed size. If the size of the HashMap reaches to the max value (e.g. 10000), addition of a new entry to the map should not be allowed. It CAN'T be LRU cache where the oldest entry gets removed on reaching maximum size. ConcurrentHashMap may satisfy #1. But, not sure how #2 can be implemented on top of