multithreading

ArrayList and Multithreading in Java

让人想犯罪 __ 提交于 2020-06-24 04:50:06
问题 Under what circumstances would an unsynchronized collection, say an ArrayList, cause a problem? I can't think of any, can someone please give me an example where an ArrayList causes a problem and a Vector solves it? I wrote a program that have 2 threads both modifying an arraylist that has one element. One thread puts "bbb" into the arraylist while the other puts "aaa" into the arraylist. I don't really see an instance where the string is half modified, I am on the right track here? Also, I

Reordering of reads

ぃ、小莉子 提交于 2020-06-24 03:14:45
问题 Suppose there are two threads without synchronization, one sets n = 1 another executes method() . In the following "read" always refers to a read of field n . public class MyClass { public int n = 0; public void method() { System.out.println(n); //read 1 System.out.println(n); //read 2 } } Would the following output be possible? 1 0 The answer is yes because even though read 1 happens-before read 2, it is nevertheless possible for read 2 to be re-ordered before read 1 because it wouldn't

How to demonstrate Java instruction reordering problems?

坚强是说给别人听的谎言 提交于 2020-06-24 02:58:28
问题 With Java instruction reordering the execution order of the code is changed by the JVM at compile time or run time, possibly causing unrelated statements to be executed out-of-order. So my question is: Can someone provide an example Java program/snippet, that reliably shows an instruction reordering problem, that is not caused also by other synchronization issues ( such as caching/visibility or non-atomic r/w, as in my failed attempt at such a demo in my previous question ) To emphasize, I am

How to demonstrate Java instruction reordering problems?

痞子三分冷 提交于 2020-06-24 02:57:21
问题 With Java instruction reordering the execution order of the code is changed by the JVM at compile time or run time, possibly causing unrelated statements to be executed out-of-order. So my question is: Can someone provide an example Java program/snippet, that reliably shows an instruction reordering problem, that is not caused also by other synchronization issues ( such as caching/visibility or non-atomic r/w, as in my failed attempt at such a demo in my previous question ) To emphasize, I am

Combining Interlocked.Increment and Interlocked.Exchange

£可爱£侵袭症+ 提交于 2020-06-23 08:23:27
问题 I wish to atomically increment a static variable and simultaneously assign the new value to an instance field in a lock-free manner. The objective is for each object to get a unique, incrementing id upon creation, such that there is no chance for two objects to get the same id. Will the following code achieve this? class MyClass { private static int currentOrderingId; private int orderingId; public MyClass() { Interlocked.Exchange(ref orderingId, Interlocked.Increment(ref currentOrderingId));

Does .NET resume an await continuation on a new different thread pool thread or reuse the thread from a previous resumption?

老子叫甜甜 提交于 2020-06-23 07:41:45
问题 Does .NET resume an await continuation on a new different thread pool thread or reuse the thread from a previous resumption? Let's image below C# code in a .NET Core console application: using System; using System.Threading; using System.Threading.Tasks; namespace NetCoreResume { class Program { static async Task AsyncThree() { await Task.Run(() => { Console.WriteLine($"AsyncThree Task.Run thread id:{Thread.CurrentThread.ManagedThreadId.ToString()}"); }); Console.WriteLine($"AsyncThree

Does .NET resume an await continuation on a new different thread pool thread or reuse the thread from a previous resumption?

情到浓时终转凉″ 提交于 2020-06-23 07:40:26
问题 Does .NET resume an await continuation on a new different thread pool thread or reuse the thread from a previous resumption? Let's image below C# code in a .NET Core console application: using System; using System.Threading; using System.Threading.Tasks; namespace NetCoreResume { class Program { static async Task AsyncThree() { await Task.Run(() => { Console.WriteLine($"AsyncThree Task.Run thread id:{Thread.CurrentThread.ManagedThreadId.ToString()}"); }); Console.WriteLine($"AsyncThree

What happens to the ThreadPoolExecutor when thread dies in Java

我只是一个虾纸丫 提交于 2020-06-23 02:47:12
问题 I have created a thread which in turn creates a ThreadPoolExecutor and submits some long running tasks to it. At some point, the original thread dies due to unhandled exception/error. What should happen to the executor (it's local to that dead thread, no external references to it)? Should it be GCed or not? EDIT: this question was formulated incorrectly from the beginning, but I will leave it as Gray provided some good details of how TPE work. 回答1: Threads are so called GC roots. This means

Mutex protecting std::condition_variable

对着背影说爱祢 提交于 2020-06-23 00:43:42
问题 Even if the shared variable is atomic, it must be modified under the mutex in order to correctly publish the modification to the waiting thread. Any thread that intends to wait on std::condition_variable has to acquire a std::unique_lock, on the same mutex as used to protect the shared variable http://en.cppreference.com/w/cpp/thread/condition_variable I understand that by protecting the std::condition_variable with a mutex we are protected against missing a notify if the waiting thread is

Restarting a thread in Python

99封情书 提交于 2020-06-22 09:55:26
问题 I'm trying to make threaded flight software for a project in Python 3.4, in which I need threads to restart themselves in case an I/O error occurs during a sensor read or another fluke crash like that. Therefore I am working on making a watchdog to check if threads have died and restarting them. At first I attempted to just check if the thread was no longer alive and restart it, which did this: >>> if not a_thread.isAlive(): ... a_thread.start() Traceback (most recent call last): File "<stdin