concurrency

Stopping a thread in java CompletableFuture after timeout

给你一囗甜甜゛ 提交于 2021-02-19 08:40:09
问题 I have an async chain in my java code that i want to stop after a certain timeout so i created a threadPool with some threads and called the CompletableFuture like this ExecutorService pool = Executors.newFixedThreadPool(10); than i have a cyclic method that loads data from the db and executes some task on it, once all the CompletableFutures are completed its doing it again CompletableFuture<MyObject> futureTask = CompletableFuture.supplyAsync(() -> candidate, pool) .thenApply(Task1::doWork)

Async method followed by a parallelly executed method in Java 8

ぐ巨炮叔叔 提交于 2021-02-19 07:56:05
问题 After spending the day of learning about the java Concurrency API, I still dont quite get how could I create the following functionality with the help of CompletableFuture and ExecutorService classes: When I get a request on my REST endpoint I need to: Start an asynchronous task (includes DB query, filtering, etc.), which will give me a list of String URLs at the end In the meanwhile, responde back to the REST caller with HTTP OK, that the request was received, I'm working on it When the

C++ monitor class/wrapper using condition variables

穿精又带淫゛_ 提交于 2021-02-19 06:07:05
问题 I'm trying to create a wrapper class W in C++, which is constructed with a pointer to a generic object OBJ . When you call one of OBJ methods through W , W (containing a condition variable cv ) issues a cv.wait() before calling OBJ method and a cv.notify() when OBJ method has finished. I've been able to do it with inheritance for a specific class, but would like a generic approach like the one described above. This is the inheritance approach: struct A { virtual void foo(int i) { bar = i; };

C++ monitor class/wrapper using condition variables

瘦欲@ 提交于 2021-02-19 06:02:03
问题 I'm trying to create a wrapper class W in C++, which is constructed with a pointer to a generic object OBJ . When you call one of OBJ methods through W , W (containing a condition variable cv ) issues a cv.wait() before calling OBJ method and a cv.notify() when OBJ method has finished. I've been able to do it with inheritance for a specific class, but would like a generic approach like the one described above. This is the inheritance approach: struct A { virtual void foo(int i) { bar = i; };

Different HTTP calls, await same Task

北慕城南 提交于 2021-02-19 03:14:17
问题 I have a Task which starts a win process, which generates file if its not created yet and returns it. The problem is that the action is called more than once. To be more precisely its src attribute of a <track> element. I have ConcurrentDictionary<Guid, Task<string>> which keeps track of for which Id a process is currently running public async Task<string> GenerateVTTFile(Guid Id) { if (_currentGenerators.TryGetValue(id, out Task<string> task)) { return await task; // problem is here? } var t

Different HTTP calls, await same Task

守給你的承諾、 提交于 2021-02-19 03:08:39
问题 I have a Task which starts a win process, which generates file if its not created yet and returns it. The problem is that the action is called more than once. To be more precisely its src attribute of a <track> element. I have ConcurrentDictionary<Guid, Task<string>> which keeps track of for which Id a process is currently running public async Task<string> GenerateVTTFile(Guid Id) { if (_currentGenerators.TryGetValue(id, out Task<string> task)) { return await task; // problem is here? } var t

Task stays in WaitingToRun state for abnormally long time

元气小坏坏 提交于 2021-02-18 17:44:10
问题 I've got a program that handles a variety of tasks running in parallel. A single task acts as a manager of sorts, making sure certain conditions are met before the next task is ran. However, I've found that sometimes a task will sit in the WaitingToRun state for a very long time. Here's the following code: mIsDisposed = false; mTasks = new BlockingCollection<TaskWrapper>(new ConcurrentQueue<TaskWrapper>()); Task.Factory.StartNew(() => { while (!mIsDisposed) { var tTask = mTasks.Take(); tTask

Download multiple files concurrently from FTP using FluentFTP with a maximum value

淺唱寂寞╮ 提交于 2021-02-18 17:36:30
问题 I would like to download multiple download files recursively from a FTP Directory, to do this I'm using FluentFTP library and my code is this one: private async Task downloadRecursively(string src, string dest, FtpClient ftp) { foreach(var item in ftp.GetListing(src)) { if (item.Type == FtpFileSystemObjectType.Directory) { if (item.Size != 0) { System.IO.Directory.CreateDirectory(Path.Combine(dest, item.Name)); downloadRecursively(Path.Combine(src, item.Name), Path.Combine(dest, item.Name),

Context package vs done channel to avoid goroutine leak

北慕城南 提交于 2021-02-18 16:21:31
问题 There are two different approaches to clean up a goroutine. Use a kill channel to signal cancellation and a done channel to indicate that goroutine has been terminated. type Worker struct { Done chan struct{} Kill chan struct{} Jobs chan Job } func (w *Worker) Run() { defer func() { w.Done <- struct{}{} } for { select { case <-w.Kill: return case j := <-w.Jobs: // Do some work } } go w.Run() w.Kill <- struct{}{} Use context to cancel type Worker struct { Ctx context.Context Cancel context

Why is compare-and-swap (CAS) algorithm a good choice for lock-free synchronization?

筅森魡賤 提交于 2021-02-18 11:22:21
问题 CAS belongs to the read-modify-write (RMW) family, a set of algorithms that allow you to perform complex transactions atomically. Specifically, Wikipedia says that CAS is used to implement synchronization primitives like semaphores and mutexes, as well as more sophisticated lock-free and wait-free algorithms. [...] CAS can implement more of these algorithms than atomic read, write, or fetch-and-add, and assuming a fairly large amount of memory, [...] it can implement all of them. https://en