concurrency

wait() and notify() method , always IllegalMonitorStateException is happen and tell me current Thread is not Owner Why?

核能气质少年 提交于 2020-01-16 16:08:06
问题 package pkg_1; public class ExpOnWaitMethod extends Thread { static Double x = new Double(20); public static void main(String[] args) { ExpOnWaitMethod T1 = new ExpOnWaitMethod(); ExpOnWaitMethod T2 = new ExpOnWaitMethod(); T1.start(); T2.start(); } public void run() { Mag mag = new Mag(); synchronized (x) { try { for (int i = 1; i < 10; i++) { mag.nop(Thread.currentThread()); x = i * 2.0; } } catch (InterruptedException e) { e.printStackTrace(); } } } } class Mag { char ccc = 'A'; public

Why Thread.stop() method isn't safe to use?

倾然丶 夕夏残阳落幕 提交于 2020-01-16 09:11:27
问题 A quote from "Effective Java book" : " The libraries provide the Thread.stop method, but this method was deprecated long ago because it is inherently unsafe—its use can result in data corruption. Do not use Thread.stop " Anyone can tell me why ? 回答1: What if the thread you stop holds a critical lock? What if the thread has placed an object into an inconsistent state and hasn't had a chance to restore it yet? The correct way to stop a thread is with its cooperation, not by forcing it to stop

Python concurrent.futures: ProcessPoolExecutor fail to work

余生长醉 提交于 2020-01-16 08:39:09
问题 I'm trying to use the ProcessPoolExecutor method but it fails. Here is an example(calculation of the big divider of two numbers) of a failed use. I don't understand what the mistake is def gcd(pair): a, b = pair low = min(a, b) for i in range(low, 0, -1): if a % i == 0 and b % i == 0: return i numbers = [(1963309, 2265973), (2030677, 3814172), (1551645, 2229620), (2039045, 2020802)] start = time() pool = ProcessPoolExecutor(max_workers=2) results = list(pool.map(gcd, numbers)) end = time()

Pattern for concurrent cache sharing

ε祈祈猫儿з 提交于 2020-01-15 15:12:24
问题 Ok I was a little unsure on how best name this problem :) But assume this scenarion, you're going out and fetching some webpage (with various urls) and caching it locally. The cache part is pretty easy to solve even with multiple threads. However, imagine that one thread starts fetching an url, and a couple of milliseconds later another want to get the same url. Is there any good pattern for making the seconds thread's method wait on the first one to fetch the page , insert it into the cache

Pool only executes a single thread instead of 4, and how do I make it infinite?

大憨熊 提交于 2020-01-15 11:54:08
问题 So I am working on a little Python tool to stress test an API of application. I've got a pretty nice script using Threading, but then I read that it will require manual coding to maintain n number of concurrent threads (meaning, starting new ones as soon as old ones finish), and the suggestion here: How to start a new thread when old one finishes? is to use ThreadPool, I tried as follows: def test_post(): print "Executing in " + threading.currentThread().getName() + "\n" time.sleep(randint(1,

How do visibility problems occur in Java concurrency? [duplicate]

陌路散爱 提交于 2020-01-15 09:47:30
问题 This question already has answers here : question about “Java Concurrency in Practice” example (6 answers) Closed 6 years ago . I am reading the book: "Java Concurrency in Practice" to better understand how java concurrency works. On chapter 3 section 3.1:Visibility There is and example in which the book tries to show how visibility problems occur. Here is the example code (Listing 3.1 in the book): public class NoVisibility { private static boolean ready; private static int number; private

PostgreSQL - make two transactions run at the same time

99封情书 提交于 2020-01-15 06:58:26
问题 I need to make two sessions (two files) run concurrently (at the same time). Is there a way to do this using pg_sleep or some other function like "delayExecutionUntil(x_time)"? 回答1: To get two transactions at the (almost) exact same time, you could schedule two or more invocations of psql at the same time in a Linux shell with the at command. Like: at '08:00 01.12.2012' -f script.sql (The required timestamp format may depend on your system locale.) Where script.sql contains something like:

Asynchronous Swift processing in Linux not working

大憨熊 提交于 2020-01-15 05:24:09
问题 I'm trying to understand how Swift 4.0 asynchronous processing works in Linux. After looking at the documentation and some answers on SO I came up with this simple example: import Dispatch import Glibc DispatchQueue.main.asyncAfter(deadline: .now()) { print("Done!") } print("Sleeping for 2 seconds...") usleep(2 * 1_000_000) print("Exiting...") However, this only prints: Sleeping for 2 seconds... Exiting... Why does it not print Done! ? What am I missing? How do I write a simple parallel

Generating more tasks than there are threads

纵饮孤独 提交于 2020-01-15 04:59:33
问题 I read in several OpenMP tutorials that you should not generate more tasks than there are threads. For example: "Do not start more tasks than there are available threads, which means available in the enclosing parallel region." Assume that we want to traverse a binary tree, that the subtrees of a node can be traversed in parallel, and that our machine has four cores. Following the advice above, we generate two tasks at the root, one for the left and one for the right subtree. Within both

How to handle Race Condition Read Write in Swift?

最后都变了- 提交于 2020-01-15 01:49:04
问题 I have got a concurrent queue with dispatch barrier from Raywenderlich post Example private let concurrentPhotoQueue = DispatchQueue(label: "com.raywenderlich.GooglyPuff.photoQueue", attributes: .concurrent) Where write operations is done in func addPhoto(_ photo: Photo) { concurrentPhotoQueue.async(flags: .barrier) { [weak self] in // 1 guard let self = self else { return } // 2 self.unsafePhotos.append(photo) // 3 DispatchQueue.main.async { [weak self] in self?.postContentAddedNotification(