concurrency

Java Wait and Notify: IllegalMonitorStateException

梦想与她 提交于 2020-04-07 08:33:05
问题 I don't completely understand how wait and notify (of Object ) work, and as a result I'm forced to slim down my attempts into the following section of code. Main.java: import java.util.ArrayList; class Main { public static Main main = null; public static int numRunners = 4; public static ArrayList<Runner> runners = null; public static void main(String[] args) { main = new Main(); } Main() { runners = new ArrayList<Runner>(numRunners); for (int i = 0; i < numRunners; i++) { Runner r = new

Solution for Insert Intention Locks in MySQL

纵然是瞬间 提交于 2020-04-05 15:06:52
问题 I have very simple table: CREATE TABLE `d` ( `id` int(11) DEFAULT NULL, UNIQUE KEY `id` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 without records: select * from d; Empty set (0,01 sec) Then I try to open two transactions in different sessions: Session #1: begin; Query OK, 0 rows affected (0,00 sec) select * from d where id = 100 for update; Empty set (0,00 sec) Session #2: begin; Query OK, 0 rows affected (0,00 sec) select * from d where id = 700 for update; Empty set (0,00 sec) Now I try

Semaphore vs Mutex in Producer/Consumer

☆樱花仙子☆ 提交于 2020-03-22 09:22:28
问题 In the Producer-Consumer problem, why are we often suggested use to semaphores instead of using a lock/mutex? I don't see a valid reason to use a semaphore because we only have 2 threads coordinating. In this case a lock seems much easier to code and reason about because a thread will lock the buffer then free it so the other thread can do the same. There are only 2 threads so I don't see the use of signaling. Can anyone say why it is suggested to use semaphores usually for producer-consumer?

Safely removing list mapping from ConcurrentDictionary

☆樱花仙子☆ 提交于 2020-03-19 06:06:39
问题 I have a ConcurrentDictionary which maps a simple type to a list: var dict = new ConcurrentDictionary<string, List<string>>(); I can use AddOrUpdate() to cater for both initialization of the list when the first value is added, and addition of subsequent values to the list. However, the same isn't true for removal. If I do something like: public void Remove(string key, string value) { List<string> list; var found = dict.TryGetValue(key, out list); if (found) { list.Remove(value); if (list

what is wrong with this thread-safe byte sequence generator?

时光总嘲笑我的痴心妄想 提交于 2020-03-18 12:08:12
问题 I need a byte generator that would generate values from Byte.MIN_VALUE to Byte.MAX_VALUE. When it reaches MAX_VALUE, it should start over again from MIN_VALUE. I have written the code using AtomicInteger (see below); however, the code does not seem to behave properly if accessed concurrently and if made artificially slow with Thread.sleep() (if no sleeping, it runs fine; however, I suspect it is just too fast for concurrency problems to show up). The code (with some added debug code): public

Is assigning a pointer atomic in golang?

六月ゝ 毕业季﹏ 提交于 2020-03-17 10:01:48
问题 Is assigning a pointer atomic in go? Do I need to assign a pointer in a lock? Suppose I just want to assign the pointer to nil, and would like other threads to be able to see it. I know in java we can use volatile for this. But there is no volatile in go. Thanks. 回答1: The only things which are guaranteed to be atomic in go are the operations in sync.atomic. So if you want to be certain you'll either need to take a lock, eg sync.Mutex or use one of the atomic primitives. I don't recommend

Synchronizing a Method Across JVM's

天大地大妈咪最大 提交于 2020-03-17 08:45:09
问题 How does one go about synchronizing a method across JVM's? My example is a web application that restricts a user name from being logged in more than once (in other words, the first user can log on, but if another user signs on with the same user name, he gets rejected). The web application is deployed across multiple servers, so there are multiple JVM's, and users can be attempting to sign on using different servers, depending on the load balancer. Here's how the method looks public

When to reset CyclicBarrier in java multithreading

半腔热情 提交于 2020-03-13 06:05:30
问题 I was reading CyclicBarrier in the following link http://java-latte.blogspot.in/2013/10/cyclicbarrier-in-java-concurrency.html. In the example 1, CyclicRaceDemo.java main method, CyclicBarrier is being reused without calling reset method. I ran the example and it worked fine. So, I am wondering what's the use of reset method. When should it be called? Or do we need to call it at all? 回答1: A CyclicBarrier is cyclic because it can be reused without resetting. From the Javadoc A synchronization

Progress bar not updating during a loop

痴心易碎 提交于 2020-03-06 09:29:10
问题 My progress bar doesn't update until the loop has finished? Why is this? for (String theURL : IPArray) { URL url = new URL(theURL); InetAddress address = InetAddress.getByName(url.getHost()); String temp = address.toString(); String IP = temp.substring(temp.indexOf("/") + 1, temp.length()); URLArray.add(IP); Progress.percentage = (URLArray.size() * 100) / Progress.totalToDo; Progress.ipProgress.setString(Progress.percentage + "%"); Progress.ipProgress.setValue(Progress.percentage); Progress

Progress bar not updating during a loop

一世执手 提交于 2020-03-06 09:28:28
问题 My progress bar doesn't update until the loop has finished? Why is this? for (String theURL : IPArray) { URL url = new URL(theURL); InetAddress address = InetAddress.getByName(url.getHost()); String temp = address.toString(); String IP = temp.substring(temp.indexOf("/") + 1, temp.length()); URLArray.add(IP); Progress.percentage = (URLArray.size() * 100) / Progress.totalToDo; Progress.ipProgress.setString(Progress.percentage + "%"); Progress.ipProgress.setValue(Progress.percentage); Progress