multithreading

ThreadAbortException when calling Task.Result

China☆狼群 提交于 2020-08-24 06:26:13
问题 I have the following code where I'm trying to make a request to a remote endpoint using HttpClient : using (var client = new HttpClient()) { client.BaseAddress = _serviceBaseAddress; Task<HttpResponseMessage> readResponseTask = client.GetAsync(relativeUri); readResponseTask.Wait(); using (var response = readResponseTask.Result) { if (response.StatusCode == HttpStatusCode.NotFound || !response.IsSuccessStatusCode) { return default(TResult); } Task<TResult> readContentTask = response.Content

atomic<bool> vs bool protected by mutex

本秂侑毒 提交于 2020-08-23 04:55:28
问题 Let's assume we have a memory area where some thread is writing data to. It then turns its attention elsewhere and allows arbitrary other threads to read the data. However, at some point in time, it wants to reuse that memory area and will write to it again. The writer thread supplies a boolean flag ( valid ), which indicates that the memory is still valid to read from (i.e. he is not reusing it yet). At some point he will set this flag to false and never set it to true again (it just flips

Use of Thread.currentThread().join() in Java

好久不见. 提交于 2020-08-21 11:02:55
问题 The following code is taken from an example in the Jersey project. See here. public class App { private static final URI BASE_URI = URI.create("http://localhost:8080/base/"); public static final String ROOT_PATH = "helloworld"; public static void main(String[] args) { try { System.out.println("\"Hello World\" Jersey Example App"); final ResourceConfig resourceConfig = new ResourceConfig(HelloWorldResource.class); final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI,

Use of Thread.currentThread().join() in Java

你。 提交于 2020-08-21 11:00:43
问题 The following code is taken from an example in the Jersey project. See here. public class App { private static final URI BASE_URI = URI.create("http://localhost:8080/base/"); public static final String ROOT_PATH = "helloworld"; public static void main(String[] args) { try { System.out.println("\"Hello World\" Jersey Example App"); final ResourceConfig resourceConfig = new ResourceConfig(HelloWorldResource.class); final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI,

A ThreadPoolExecutor inside a ProcessPoolExecutor

倾然丶 夕夏残阳落幕 提交于 2020-08-21 09:44:08
问题 I am new to the futures module and have a task that could benefit from parallelization; but I don't seem to be able to figure out exactly how to setup the function for a thread and the function for a process. I'd appreciate any help anyone can shed on the matter. I'm running a particle swarm optimization (PSO). Without getting into too much detail about PSO itself, here's the basic layout of my code: There is a Particle class, with a getFitness(self) method (which computes some metric and

Send or post a message to a Windows Forms message loop

喜夏-厌秋 提交于 2020-08-21 05:13:45
问题 I have a thread that reads messages from a named pipe. It is a blocking read, which is why it's in its own thread. When this thread reads a message, I want it to notify the Windows Forms message loop running in the main thread that a message is ready. How can I do that? In win32 I would do a PostMessage, but that function does not seem to exist in .Net (or at least I could not find it). 回答1: In WinForms you can achieve this with Control.BeginInvoke. An example: public class

Tracking progress of joblib.Parallel execution

末鹿安然 提交于 2020-08-21 05:02:06
问题 Is there a simple way to track the overall progress of a joblib.Parallel execution? I have a long-running execution composed of thousands of jobs, which I want to track and record in a database. However, to do that, whenever Parallel finishes a task, I need it to execute a callback, reporting how many remaining jobs are left. I've accomplished a similar task before with Python's stdlib multiprocessing.Pool, by launching a thread that records the number of pending jobs in Pool's job list.

Thread Safety in C

我的梦境 提交于 2020-08-21 02:53:50
问题 imagine I write a library in C. Further, imagine this library to be used from a multi-threaded environment. How do I make it thread-safe? More specific: How do I assure, that certain functions are executed only by one thread at a time? In opposite to Java or C# for example, C has no means to deal with threads/locks/etc., nor does the C standard library. I know, that operating systems support threads, but using their api would restrict the compatibility of my library very much. Which

Java access object outside thread

别说谁变了你拦得住时间么 提交于 2020-08-20 12:39:22
问题 I want to access the instance created in t1 from outside the thread, is this possible? So I can close the socket after the thread is executed. Network class: public class Network { Socket socket; public void changeConnection(String command) throws Exception { // Whatever exceptions might be thrown if (command.equals("connect")) { socket = new Socket(server, port); } else if (command.equals("disconnect")) { socket.close(); } } } Main class: public class Project1 { public static void main

Java access object outside thread

爱⌒轻易说出口 提交于 2020-08-20 12:39:22
问题 I want to access the instance created in t1 from outside the thread, is this possible? So I can close the socket after the thread is executed. Network class: public class Network { Socket socket; public void changeConnection(String command) throws Exception { // Whatever exceptions might be thrown if (command.equals("connect")) { socket = new Socket(server, port); } else if (command.equals("disconnect")) { socket.close(); } } } Main class: public class Project1 { public static void main