concurrency

How atomicity is achieved in the classes defined in java.util.concurrent.atomic package?

て烟熏妆下的殇ゞ 提交于 2020-01-03 08:26:15
问题 I was going through the source code of java.util.concurrent.atomic.AtomicInteger to find out how atomicity is achieved by the atomic operations provided by the class. For instance AtomicInteger.getAndIncrement() method source is as follows public final int getAndIncrement() { for (;;) { int current = get(); int next = current + 1; if (compareAndSet(current, next)) return current; } } I am not able to understand the purpose of writing the sequence of operations inside a infinite for loop. Does

Is there a .thenCompose() for CompletableFuture that also executes exceptionally?

霸气de小男生 提交于 2020-01-03 08:26:10
问题 I want to execute a CompletableFuture once another CompletableFuture finishes, regardless of whether or not the first one completes exceptionally ( .thenCompose() only runs when execution completes normally). For example: CompletableFuture.supplyAsync(() -> 1L) .whenComplete((v, e) -> CompletableFuture.runAsync(() -> { try { Thread.sleep(1000); System.out.println("HERE"); } catch(InterruptedException exc) { return; } })) .whenComplete((v, e) -> System.out.println("ALL DONE")); This prints ALL

Is there a .thenCompose() for CompletableFuture that also executes exceptionally?

廉价感情. 提交于 2020-01-03 08:26:02
问题 I want to execute a CompletableFuture once another CompletableFuture finishes, regardless of whether or not the first one completes exceptionally ( .thenCompose() only runs when execution completes normally). For example: CompletableFuture.supplyAsync(() -> 1L) .whenComplete((v, e) -> CompletableFuture.runAsync(() -> { try { Thread.sleep(1000); System.out.println("HERE"); } catch(InterruptedException exc) { return; } })) .whenComplete((v, e) -> System.out.println("ALL DONE")); This prints ALL

Access Java JFrame from another class

倖福魔咒の 提交于 2020-01-03 06:38:28
问题 I have a class that creates a JFrame. When the JFrame is created it has a start button. When the start button is clicked, it runs two threads until the stop button is clicked. The two threads are in another class file. From the class that contains the threads, how can I access the JFrame instance in order to change value that are displayed? 回答1: In order to acheive this you have to pass the reference of JFrame using this keyword. 回答2: To have access to a private instance within another class,

Performance of Concurrent Program Degrading with Increase in Threads?

て烟熏妆下的殇ゞ 提交于 2020-01-03 05:37:11
问题 I have been trying to implement the below code on quad core computer and average running times with No of threads in the Executor service over 100 iterations is as follows 1 thread = 78404.95 2 threads = 174995.14 4 thread = 144230.23 But according to what I have studied 2*(no of cores) of threads should give optimal result for the program which is clearly not the case in my program which bizarrely gives best time for single thread. Code : import java.util.Collections; import java.util.Random

How to get the JMXConnectorServer of the platform MBeanServer?

走远了吗. 提交于 2020-01-03 05:28:09
问题 I have a Java program providing services which can be invoked by calling methods on an JMX MBean via RMI. The service is running without problems, but I am facing the question of how to shut down the service without interrupting a potential new concurrent request to the service. One solution for this problem would be to wait for all JMX connections to be closed and only then (and when there is no more background activity) to shut down the process. JMXConnectorServer has a method

Best way to wait on tasks to complete before adding new ones to threadpool in Java?

断了今生、忘了曾经 提交于 2020-01-03 05:08:13
问题 I want to use something like a ThreadPoolExecutor to manage running a bunch of tasks on available threads. These tasks are all of the same type but deal with different accounts. New tasks for these accounts can be added at regular intervals and I want it to check and not allow the new tasks to start until the old tasks for the same account have already completed. What's the best way to do this? EXAMPLE Task for account "234" is started (via ThreadPoolExecutor.execute()) Task for account "238"

Optimistic concurrency out of session in NHibernate

风流意气都作罢 提交于 2020-01-03 05:05:09
问题 I'm having trouble implementing optimisitc concurrency in NHibernate in a meaningful way in a web application. Here is the desired scenario: User A opens a form to edit a record User B opens the same form to edit the same record User A saves their data User B tries to save their data but get a warning that the data has been updated. A common scenario. Here is the update code and entity mapping file: <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Entities" assembly="Domain">

Trying To Implement Concurrent TCP Server and Client in c

风流意气都作罢 提交于 2020-01-03 04:52:09
问题 I have implemented TCp concurrent server and client in c using threads as well as forks. But I don't have any way to check whether there is any other standard way of implementing this. I have goggled for standard coding stuff but didnt find anything useful. Can someone pl z share some good links or code so that I can have a standard idea of implementing Concurrent servers. Thanks for help 回答1: There's no "standard idea". Your approach is going to depend on requirements, performance,

In Go, can we synchronize each key of a map using a lock per key?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-03 04:10:29
问题 In Go, can we synchronize each key of a map using a lock per key? Is map level global lock always required? The documentation says that any access to map is not thread safe. But if a key exists, then can it be locked individually? 回答1: Not exactly, but if you are only reading pointers off a map and modifying the referents, then you aren't modifying the map itself. 回答2: This is a simple implementation of what you want: mapmutex. Basically, a mutex is used to guard the map and each item in the