Questions tagged [multithreading]

For questions regarding multi-threading, the ability of a computer or a program to perform work concurrently or asynchronously by utilizing multiple concurrent streams of execution (generally referred to as threads).

Multi-threading is a common model to implement SM in multi-cored machines, where threads share memory through shared variables. In this model in parallel programming a processor can create multiple threads that will be executed in parallel. Each thread has its own stack, variables and ID, considered private state. For every thread there is a unique heap shared by all, then considered shared memory.

In order to allow a volume of work to be most effectively and safely divided into multiple concurrent streams of execution, a number of critical areas need to be addressed.

  • Scheduling: ensure worker tasks are able to progress independently and effectively (e.g. deadlock/livelock avoidance)
  • Publication: ensure data altered in one thread is only visible to others as expected
  • Synchronization: ensure critical regions are protected from multiple concurrent updates causing data loss/corruption.

The underlying tenet of concurrent processing is Amdahl's law (graph). This law governs the diminishing amount of throughput that can be achieved by greater numbers of concurrent processors/cores. Therefore the overall aim of multi-threading is to minimize the amount of serial execution (exclusive locking) within any concurrent system.

Frequently Asked Questions

Books

More information:

Related tags

139166 questions
257
votes
3 answers

Use cases for RxJava schedulers

In RxJava there are 5 different schedulers to choose from: immediate(): Creates and returns a Scheduler that executes work immediately on the current thread. trampoline(): Creates and returns a Scheduler that queues work on the current thread to…
bcorso
  • 45,608
  • 10
  • 63
  • 75
251
votes
14 answers

What's the difference between Thread start() and Runnable run()

Say we have these two Runnables: class R1 implements Runnable { public void run() { … } … } class R2 implements Runnable { public void run() { … } … } Then what's the difference between this: public static void main() { R1 r1 =…
Ori Popowski
  • 10,432
  • 15
  • 57
  • 79
245
votes
13 answers

Handling exceptions from Java ExecutorService tasks

I'm trying to use Java's ThreadPoolExecutor class to run a large number of heavy weight tasks with a fixed number of threads. Each of the tasks has many places during which it may fail due to exceptions. I've subclassed ThreadPoolExecutor and I've…
Tom
  • 18,685
  • 15
  • 71
  • 81
244
votes
15 answers

C# Events and Thread Safety

I frequently hear/read the following advice: Always make a copy of an event before you check it for null and fire it. This will eliminate a potential problem with threading where the event becomes null at the location right between where you check…
Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
244
votes
7 answers

Creating Threads in python

I have a script and I want one function to run at the same time as the other. The example code I have looked at: import threading def MyThread (threading.thread): # doing something........ def MyThread2 (threading.thread): # doing…
chrisg
  • 40,337
  • 38
  • 86
  • 107
241
votes
16 answers

ExecutorService, how to wait for all tasks to finish

What is the simplest way to to wait for all tasks of ExecutorService to finish? My task is primarily computational, so I just want to run a large number of jobs - one on each core. Right now my setup looks like this: ExecutorService es =…
george smiley
  • 2,721
  • 4
  • 21
  • 13
236
votes
5 answers

Task continuation on UI thread

Is there a 'standard' way to specify that a task continuation should run on the thread from which the initial task was created? Currently I have the code below - it is working but keeping track of the dispatcher and creating a second Action seems…
Greg Sansom
  • 20,442
  • 6
  • 58
  • 76
236
votes
19 answers

How to test methods that call System.exit()?

I've got a few methods that should call System.exit() on certain inputs. Unfortunately, testing these cases causes JUnit to terminate! Putting the method calls in a new Thread doesn't seem to help, since System.exit() terminates the JVM, not just…
Chris Conway
  • 55,321
  • 43
  • 129
  • 155
234
votes
14 answers

Can't pickle when using multiprocessing Pool.map()

I'm trying to use multiprocessing's Pool.map() function to divide out work simultaneously. When I use the following code, it works fine: import multiprocessing def f(x): return x*x def go(): pool = multiprocessing.Pool(processes=4) …
ventolin
  • 2,971
  • 3
  • 21
  • 25
234
votes
2 answers

Redis is single-threaded, then how does it do concurrent I/O?

Trying to grasp some basics of Redis I came across an interesting blog post . The author states: Redis is single-threaded with epoll/kqueue and scale indefinitely in terms of I/O concurrency. I surely misunderstand the whole threading thing,…
233
votes
10 answers

The calling thread must be STA, because many UI components require this

I am using http://www.codeproject.com/KB/IP/Facebook_API.aspx I am trying to call the XAML which is created using WPF. But it gives me an error: The calling thread must be STA, because many UI components require this. I don't know what to do. I am…
C..
  • 6,897
  • 10
  • 30
  • 37
227
votes
11 answers

Java synchronized method lock on object, or method?

If I have 2 synchronized methods in the same class, but each accessing different variables, can 2 threads access those 2 methods at the same time? Does the lock occur on the object, or does it get as specific as the variables inside the synchronized…
wuntee
  • 12,170
  • 26
  • 77
  • 106
227
votes
6 answers

Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on

I want to send temperature value from a microcontroller using UART to C# interface and Display temperature on Label.Content. Here is my microcontroller code: while(1) { key_scan(); // get value of temp if (Usart_Data_Ready()) { …
Fatima Zohra
  • 2,929
  • 2
  • 17
  • 17
226
votes
7 answers

What is the Haskell response to Node.js?

I believe the Erlang community is not envious of Node.js as it does non-blocking I/O natively and has ways to scale deployments easily to more than one processor (something not even built-in in Node.js). More details at…
gawi
  • 13,940
  • 7
  • 42
  • 78
225
votes
9 answers

What is the difference between a thread and a fiber?

What is the difference between a thread and a fiber? I've heard of fibers from ruby and I've read heard they're available in other languages, could somebody explain to me in simple terms what is the difference between a thread and a fiber.
tatsuhirosatou
  • 25,149
  • 14
  • 39
  • 40