concurrency

Implement a thread-safe ArrayList in Java by locking

≯℡__Kan透↙ 提交于 2021-02-09 07:27:10
问题 I want to write a simple thread-safe arraylist which supports: add(), remove(int i), insert(int i), update(int i), and get(int i) One simple implementation is to add lock to the internal data structure(an object array for example), but it is not good enough because only one thread could access the list at a time. Therefore my initial plan is to add lock to each data slot so that different threads could have access to elements in different indexes at the same time. The data structure will look

OpenMP Parallel for-loop showing little performance increase

£可爱£侵袭症+ 提交于 2021-02-08 14:42:05
问题 I am in the process of learning how to use OpenMP in C, and as a HelloWorld exercise I am writing a program to count primes. I then parallelise this as follows: int numprimes = 0; #pragma omp parallel for reduction (+:numprimes) for (i = 1; i <= n; i++) { if (is_prime(i) == true) numprimes ++; } I compile this code using gcc -g -Wall -fopenmp -o primes primes.c -lm ( -lm for the math.h functions I am using). Then I run this code on an Intel® Core™2 Duo CPU E8400 @ 3.00GHz × 2 , and as

Java - atomically delete a (non-empty) directory

谁都会走 提交于 2021-02-08 14:04:39
问题 In a Java application, is there a way to atomically (with regards to other running processes) delete a non-empty directory? 回答1: The OS (Windows, Unix) doesn't support this operation, but what you can do is rename the directory or even move it away and then delete it later. 来源: https://stackoverflow.com/questions/13890949/java-atomically-delete-a-non-empty-directory

Run parallel op with different inputs and same placeholder

送分小仙女□ 提交于 2021-02-08 11:34:40
问题 I have the necessity to calculate more then one accuracy in the same time, concurrently. correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})) The piece of code is the same of the mnist example in the tutorial of TensorFlow but instead of having: W = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10])) I have two placeolder

guzzle concurrent request, wait for finished batch before sending next

主宰稳场 提交于 2021-02-08 10:17:41
问题 I thought this following code would work this way: a batch in the number of CONCURRENT_REQUESTS is send it is waited until all of these requests are finished the next batch of the above number is send and so on but in reality if I comment line 14 [ usleep(...) ] it seems the request batches are send as fast as possible generating thousands of queries to the server. Is it possible to change it? How do I change this behavior? <?php $pool = $this->getPool(); if (false !== $pool) { $pool->promise

Lock script execution on a document when script is deployed as web app

拥有回忆 提交于 2021-02-08 07:32:07
问题 My Google Apps Script is deployed as web app and can be accessed by any user. Its functionality is to open and change the text in that document. I send the script a document ID as a query parameters like so: https://script.google.com/a/macros/s/AKfycbzCP...TnwHUbXxzDM/exec?docId=1_cMN0nuJadBw6QVjKtdEA6eXhE8ubIoxIJai1ticxnE` Web app opens the document and changes the text in the document. function doGet(e){ var params=e.parameters; var doc = DocumentApp.openById(params['docId']); ... /* change

Make Clojure's println “thread-safe” in the same way as in Java

一笑奈何 提交于 2021-02-08 07:28:32
问题 While playing around with concurrent calls to println in Clojure I found that its behaviour is different from Java's System.out.println . What in Java I would write class Pcalls { public static void main(String[] args) { Runnable[] fns = new Runnable[3]; for (int i = 0; i < 3; i++) { fns[i] = new Runnable() { @Override public void run() { for (int i = 1; i <= 5; i++) { System.out.println("Hello iteration " + i); } } }; } for (Runnable fn : fns) new Thread(fn).start(); } } I paraphrased in

How to perform the transformation on CompletableFuture tasks

烂漫一生 提交于 2021-02-08 06:53:45
问题 I am trying to perform some transformation on the list of CompletableFuture tasks, but facing some issue because of lack of experience. I have a list of CompletableFuture but I am failing to perform further transformation on it. import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; public class thenapply { public static void main(String[] args) throws InterruptedException, ExecutionException { List<Integer> list = Arrays.asList(5, 9

Wait for callback from other thread

扶醉桌前 提交于 2021-02-08 05:26:13
问题 First of all, I've decided to make my class blocking (to make it easier for the consumer to use - but maybe more tedious for me to write). As opposed to having the consumer define asynchronous callbacks. Is this a good design pattern? This way, a user can get expected behaviour, but implement their own multi-threading if they're dissatisfied with how long the thread's blocked for. I've got a constructor that sets a final field in a class, based on the result of an async callback: class

Reading a stale value after a newer value was read [duplicate]

断了今生、忘了曾经 提交于 2021-02-08 02:37:34
问题 This question already has answers here : Reordering of reads (2 answers) Closed 2 months ago . Consider this example. We're having: int var = 0; Thread A: System.out.println(var); System.out.println(var); Thread B: var = 1; The threads run concurrently. Is the following output possible? 1 0 That is, the original value is read after the new value was read. The var isn't volatile. My gut feeling is that it's not possible. 回答1: You are using System.out.println that internally does a synchronized