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
295
votes
9 answers

Daemon Threads Explanation

In the Python documentation it says: A thread can be flagged as a "daemon thread". The significance of this flag is that the entire Python program exits when only daemon threads are left. The initial value is inherited from the creating…
Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
293
votes
20 answers

Catch a thread's exception in the caller thread?

I'm very new to Python and multithreaded programming in general. Basically, I have a script that will copy files to another location. I would like this to be placed in another thread so I can output .... to indicate that the script is still…
Phanto
  • 1,147
  • 5
  • 16
  • 19
291
votes
12 answers

What is the use of join() in threading?

I was studying the python threading and came across join(). The author told that if thread is in daemon mode then i need to use join() so that thread can finish itself before main thread terminates. but I have also seen him using t.join() even…
user192362127
  • 11,385
  • 8
  • 24
  • 21
287
votes
9 answers

Volatile vs Static in Java

Is it correct to say that static means one copy of the value for all objects and volatile means one copy of the value for all threads? Anyway a static variable value is also going to be one value for all threads, then why should we go for volatile?
Jothi
  • 14,720
  • 22
  • 68
  • 93
283
votes
16 answers

Threads vs Processes in Linux

I've recently heard a few people say that in Linux, it is almost always better to use processes instead of threads, since Linux is very efficient in handling processes, and because there are so many problems (such as locking) associated with…
user17918
  • 3,018
  • 3
  • 19
  • 7
282
votes
18 answers

How to timeout a thread

I want to run a thread for some fixed amount of time. If it is not completed within that time, I want to either kill it, throw some exception, or handle it in some way. How can it be done? One way of doing it as I figured out from this thread is to…
java_geek
  • 17,585
  • 30
  • 91
  • 113
276
votes
10 answers

What does java.lang.Thread.interrupt() do?

Could you explain what java.lang.Thread.interrupt() does when invoked?
oneat
  • 10,778
  • 16
  • 52
  • 70
272
votes
3 answers

What exactly is std::atomic?

I understand that std::atomic<> is an atomic object. But atomic to what extent? To my understanding an operation can be atomic. What exactly is meant by making an object atomic? For example if there are two threads concurrently executing the…
user4386938
271
votes
7 answers

Semaphore vs. Monitors - what's the difference?

What are the major differences between a Monitor and a Semaphore?
user919860
  • 3,083
  • 4
  • 19
  • 16
271
votes
13 answers

Get a list of all threads currently running in Java

Is there any way I can get a list of all running threads in the current JVM (including the threads not started by my class)? Is it also possible to get the Thread and Class objects of all threads in the list? I want to be able to do this through…
Kryten
  • 3,843
  • 5
  • 37
  • 42
270
votes
7 answers

Running multiple AsyncTasks at the same time -- not possible?

I'm trying to run two AsyncTasks at the same time. (Platform is Android 1.5, HTC Hero.) However, only the first gets executed. Here's a simple snippet to describe my problem: public class AndroidJunk extends Activity { class PrinterTask extends…
rodion
  • 6,087
  • 4
  • 24
  • 29
269
votes
5 answers

Difference between CompletableFuture, Future and RxJava's Observable

I would like to know the difference between CompletableFuture,Future and Observable RxJava. What I know is all are asynchronous but Future.get() blocks the thread CompletableFuture gives the callback methods RxJava Observable --- similar to…
shiv455
  • 7,384
  • 19
  • 54
  • 93
268
votes
8 answers

How to obtain a Thread id in Python?

I have a multi-threading Python program, and a utility function, writeLog(message), that writes out a timestamp followed by the message. Unfortunately, the resultant log file gives no indication of which thread is generating which message. I would…
265
votes
11 answers

Is the C# static constructor thread safe?

In other words, is this Singleton implementation thread safe: public class Singleton { private static Singleton instance; private Singleton() { } static Singleton() { instance = new Singleton(); } public static…
urini
  • 32,483
  • 14
  • 40
  • 37
257
votes
14 answers

How many threads can a Java VM support?

How many threads can a Java VM support? Does this vary by vendor? by operating system? other factors?
McGovernTheory
  • 6,556
  • 4
  • 41
  • 75