concurrency

Concurrent Cache in Java

大城市里の小女人 提交于 2019-12-24 15:27:55
问题 Im looking for an explanation to the following code from Brian Goetz's concurrency book. public V compute(final A arg) throws InterruptedException { while (true) { Future<V> f = cache.get(arg); if (f == null) { Callable<V> eval = new Callable<V>() { public V call() throws InterruptedException { return c.compute(arg); } }; FutureTask<V> ft = new FutureTask<V>(eval); f = cache.putIfAbsent(arg, ft); if (f == null) { f = ft; ft.run(); } } try { return f.get(); } catch (CancellationException e) {

Share data between node child processes

非 Y 不嫁゛ 提交于 2019-12-24 15:27:04
问题 I have a small node script that gets ran in several node child processes, and I do not have access to the parent process. The goal of the script is pretty simple, to return one element at random from an array. However, the element returned cannot be used by any of the other child processes. The only solutions I can think of involve using redis or a database, and because this is such a tiny script, I would like to avoid that. Here is an example of what I would like my code to look like: var

Why do I get random errors by using celluloid?

白昼怎懂夜的黑 提交于 2019-12-24 15:01:18
问题 I need to make API calls to a webservice in order to retrieve date. For this purpose I created a sample in order to get familiar with celluloid. Here I ll user the openweather API for "training" purpose. The ultimate goal is to run multiple requests in concurrently. my booking class (booking.rb) fetches data require 'celluloid' require 'open-uri' require 'json' class Booking include Celluloid def initialize end def parse(url) p "back again" begin buffer = open(url).read p JSON.parse(buffer)[

All the Swing frames get “frozen” when wait() is called in Java

≯℡__Kan透↙ 提交于 2019-12-24 14:51:25
问题 I want to wait() the put() method called from the second thread which has been connected to the Server (Monitor). But when i do this, the whole GUI frames (Swing) including their elements get frozen aftr the second put() call. How to fix this? I want the second thread keep waiting till the first thread performs a get() which frees a slot. Thanks in advance. Here's my skeleton code: Server: Buffer<String> buf = new Buffer<String>(1); while(true){ //for each socket connected new ServerHandler(.

Observing changes in a BlockingCollection without consuming

只愿长相守 提交于 2019-12-24 14:28:17
问题 A consumer thread and multiple producer threads synchronize their work with a System.Collections.Concurrent.BlockingCollection<T>. The producers call blockingCollection.Add() and the consumer runs foreach (var item in blockingCollection.GetConsumingEnumerable()) {...} Now one producer wants to "flush" the buffer: The producer wants to wait until all current items have been consumed. Other producers may add further items in the meantime, but this producer is only interested in the items that

Lifetime for passed-in function that is then executed in a thread

两盒软妹~` 提交于 2019-12-24 14:22:40
问题 I'm trying to build a simple pipeline-like functionality that executes each stage of the pipeline is separate threads and glues them all together with channel passing. Pipe::source(buffer) .pipe(|input, output| {...}) .pipe(|input, output| {...}) .sink(writer) I cannot for the life of me figure out the function signature for the pipe() function. Here's my code: use std::sync::mpsc::channel; use std::io::{ChanReader,ChanWriter}; use std::thread::Thread; struct Pipe { incoming: ChanReader }

Implementing a stock exchange using monitors

落爺英雄遲暮 提交于 2019-12-24 14:07:06
问题 I am trying to implement a stock exchange using Hoare's monitors. It has two functions buy() and sell() as follow: buy(procid, ticker, nshares, limit) sell(procid, ticker, nshares, limit) And should print information on buyer id, seller id, ticker, number of shares, and price. And fairness is always satisfied. The pseudo-code of my solution is as follows, but it's not complete. It basically uses a condition variable queue for each ticker. A seller process is put to sleep on this queue when it

How to manually lock and unlock a row?

不羁岁月 提交于 2019-12-24 13:27:50
问题 I am trying to circumvent double-writes by locking and unlocking certain rows. I have a table personnel and as soon as a user tries to edit a row, I want to lock that row. So BEGIN; // I guess I have to lock it somehow here SELECT * FROM personnel WHERE id = 12; COMMIT; and once an edit as been made, I want to submit the UPDATE in the same style: BEGIN; //Unlocking UPDATE personnel SET ... WHERE id = 12; COMMIT; In the time between, when another user tries to edit the same row, he would get a

How to safely publish mutable object from one thread to another

雨燕双飞 提交于 2019-12-24 13:24:30
问题 For example I have a mutable class Foo (or ArrayList for example) with no synchronization. Construction of such an object needs time so I'd like to perform it in a separate thread. If I store the result of the computation somewhere and later access it from another thread, that would not be thread safe, because synchronization or volatile is required, right? (I am actually not quite sure here) So I'm looking for a way to pass such an object from one thread to another without synchronizing of

Text Area not getting updated with the button action performed event

☆樱花仙子☆ 提交于 2019-12-24 12:43:44
问题 I have a Text Area swing element where I want to display some execution logs. messages should appear after each execution step. e.g. "Fetching has been started". But the problem is that all logs messages appear only once after the button action performed event is completed.Below is the way I used to display some text getXmlButton = new JButton("Fetch Reports"); getXmlButton.addActionListener((new ActionListener() { @Override public void actionPerformed(ActionEvent e) { createDirectory();