concurrency

Concurrent sorting in Java

萝らか妹 提交于 2020-01-02 06:07:47
问题 I am currently working on a program to sort strings concurrently. My program takes in a file, reads each line of the file into an array, and splits the array of strings into smaller arrays of strings. The program then starts up one thread for each of the smaller arrays, and quicksorts them. Once every thread has finished sorting its array, the main thread gathers all the results from the thread objects. It is then supposed to merge the smaller, now sorted, arrays into one large, sorted array.

Deterministic assignment of tasks to threads using ExecutorService

孤者浪人 提交于 2020-01-02 06:07:13
问题 Given Executor service with a fixed pool of threads, is it possible to guarantee deterministic assignment of tasks to threads? More precisely, assume there are just two threads, namely pool-thread-0 and pool-thread-1 and there is a collection of 2 tasks to be executed. What I wish to achieve is that the former thread always executes the first one, while the latter handles the remaining one. Here is an example: public static void main(String[] args) throws InterruptedException,

How to compare thread objects

霸气de小男生 提交于 2020-01-02 05:40:30
问题 I recently came across the sources of AWT's EventQueue where I saw this piece of code: final boolean isDispatchThreadImpl() { EventQueue eq = this; pushPopLock.lock(); try { EventQueue next = eq.nextQueue; while (next != null) { eq = next; next = eq.nextQueue; } if (eq.fwDispatcher != null) { return eq.fwDispatcher.isDispatchThread(); } return (Thread.currentThread() == eq.dispatchThread); } finally { pushPopLock.unlock(); } } What really struggles me is that the thread objects are being

Why should I prefer to create an NSManagedObjectContext for every new thread or NSOperation instead of calling Core Data on the Main thread?

徘徊边缘 提交于 2020-01-02 05:39:08
问题 Some developers already told me that I can create a new NSManagedObjectContext instance for every new thread, to make Core Data thread-safe. Then I would just have to take care of the merging afterwards. For me, this sounds like A LOT of extra code and overhead. Is there a reason why this solution would be bad? Here it comes: Instead of creating a new MOC for every new thread or for every NSOperation, I would perform MOC-changes on the Main Thread, just like we know from UIKit. I would just

performBlockAndWait On Child Context with Private Queue Deadlocks Parent on iOS 7

一笑奈何 提交于 2020-01-02 02:55:14
问题 I have two NSManagedObjectContext s named importContext and childContext . childContext is the child of importContext and both of them are NSPrivateQueueConcurrencyType . To keep things off the main thread, I'm doing a bunch of work on the importContext 's queue. This work involves lots of fetches and saves, so it's convenient to wrap the whole thing inside a performBlockAndWait: of the importContext (it does need to by a synchronous operation because the code I have after the

Java EE 7 - Injection into Runnable/Callable object

非 Y 不嫁゛ 提交于 2020-01-02 01:25:28
问题 Concurrency Utilities(JSR 236) has been introduced in Java EE 7. Is there any way how to inject my EJBs into Runnable/Callable object? Specifically I want something like this: ejb with business logic @LocalBean public class MyEjb { public void doSomeStuff() { ... do some stuff ... } } runnable/callable class where I want to inject instance of MyEjb public class MyTask implements Runnable { @EJB MyEjb myEjb; @Override public void run() { ... myEjb.doSomeStuff(); ... } } Object which starts the

Is it safe to reinsert the entry from Guava RemovalListener?

南笙酒味 提交于 2020-01-02 01:08:54
问题 I've got a Guava Cache (or rather, I am migrating from MapMaker to Cache ) and the values represent long-running jobs. I'd like to add expireAfterAccess behavior to the cache, as it's the best way to clean it up; however, the job may still be running even though it hasn't been accessed via the cache in some time, and in that case I need to prevent it from being removed from the cache. I have three questions: Is it safe to reinsert the cache entry that's being removed during the

Element order in BlockingCollection<>

冷暖自知 提交于 2020-01-02 00:57:09
问题 I have a Download Queue implemented with BlockingCollection<> . Now I want to prioritize some Download once in a while. I thought it might be great to move some elements 'up' the Collection, like in a list, but there is no method like Remove()/AddFirst() or Move(). What's the preferred way of arranging items in a BlockingCollection<> ? 回答1: BlockingCollection<T> works by wrapping an internal IProducerConsumerCollection<T>. The default is to use a ConcurrentQueue<T> internally, but you can

Queue and ProcessPoolExecutor in Tornado

╄→尐↘猪︶ㄣ 提交于 2020-01-01 21:47:19
问题 I'm attempting to use the new Tornado queue object along with concurrent.futures to allow my webserver to pass off cpu-intensive tasks to other processes. I want to have access to the Future object that's returned from the ProcessPoolExecutor from the concurrent.futures module so that I can query its state to show on the front-end (e.g. show the process is currently running; show that it has finished). I seem to have two hurdles with this method: How can I submit multiple q.get() objects to

Is there an implementation of std::async which uses thread pool?

假装没事ソ 提交于 2020-01-01 19:41:08
问题 The standard function std::async: The template function async runs the function f asynchronously (potentially in a separate thread which may be part of a thread pool) and returns a std::future that will eventually hold the result of that function call. There is two launch polices std::launch::async and std::launch::deferred. In my compiler's ( GCC 6.2 ) standard library impelmentation, the first one always creates a new thread and the second one does lazy evaluation on the calling thread. By