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).
multithreading
140,607
questions
2371
votes
42
answers
771k
views
"implements Runnable" vs "extends Thread" in Java
From what time I've spent with threads in Java, I've found these two ways to write threads:
With implements Runnable:
public class MyRunnable implements Runnable {
public void run() {
//...
2212
votes
8
answers
297k
views
C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?
C++11 introduced a standardized memory model, but what exactly does that mean? And how is it going to affect C++ programming?
This article (by Gavin Clarke who quotes Herb Sutter) says that,
The ...
2031
votes
35
answers
1.4m
views
What is the difference between a process and a thread?
What is the technical difference between a process and a thread?
I get the feeling a word like 'process' is overused and there are also hardware and software threads. How about light-weight processes ...
1597
votes
47
answers
842k
views
How do I update the GUI from another thread?
Which is the simplest way to update a Label from another Thread?
I have a Form running on thread1, and from that I'm starting another thread (thread2).
While thread2 is processing some files I would ...
1507
votes
24
answers
1.2m
views
How do I use threading in Python?
I would like a clear example showing tasks being divided across multiple threads.
1351
votes
33
answers
1.0m
views
Difference between "wait()" vs "sleep()" in Java
What is the difference between a wait() and sleep() in Threads?
Is my understanding that a wait()-ing Thread is still in running mode and uses CPU cycles but a sleep()-ing does not consume any CPU ...
1332
votes
19
answers
890k
views
What is a race condition?
When writing multithreaded applications, one of the most common problems experienced is race conditions.
My questions to the community are:
What is the race condition?
How do you detect them?
How do ...
1258
votes
8
answers
329k
views
How do servlets work? Instantiation, sessions, shared variables and multithreading
Suppose, I have a webserver which holds numerous servlets. For information passing among those servlets I am setting session and instance variables.
Now, if 2 or more users send request to this ...
1204
votes
35
answers
820k
views
Android "Only the original thread that created a view hierarchy can touch its views."
I've built a simple music player in Android. The view for each song contains a SeekBar, implemented like this:
public class Song extends Activity implements OnClickListener,Runnable {
private ...
1138
votes
18
answers
567k
views
What does 'synchronized' mean?
I have some questions regarding the usage and significance of the synchronized keyword.
What is the significance of the synchronized keyword?
When should methods be synchronized?
What does it mean ...
1035
votes
12
answers
586k
views
Multiprocessing vs Threading Python [duplicate]
I am trying to understand the advantages of multiprocessing over threading. I know that multiprocessing gets around the Global Interpreter Lock, but what other advantages are there, and can threading ...
1005
votes
31
answers
1.3m
views
Is there any way to kill a Thread?
Is it possible to terminate a running thread without setting/checking any flags/semaphores/etc.?
983
votes
26
answers
382k
views
When and how should I use a ThreadLocal variable?
When should I use a ThreadLocal variable?
How is it used?
971
votes
10
answers
489k
views
What is a mutex?
A mutex is a programming concept that is frequently used to solve multi-threading problems. My question to the community:
What is a mutex and how do you use it?
949
votes
5
answers
470k
views
What is thread safe or non-thread safe in PHP?
I saw different binaries for PHP, like non-thread or thread safe?
What does this mean?
What is the difference between these packages?
920
votes
27
answers
554k
views
What is a daemon thread in Java?
Can anybody tell me what daemon threads are in Java?
830
votes
25
answers
368k
views
What is the volatile keyword useful for?
At work today, I came across the volatile keyword in Java. Not being very familiar with it, I found this explanation.
Given the detail in which that article explains the keyword in question, do you ...
824
votes
11
answers
345k
views
Service vs IntentService in the Android platform
I am seeking an example of something that can be done with an IntentService that cannot be done with a Service (and vice-versa)?
I also believe that an IntentService runs in a different thread and a ...
805
votes
29
answers
218k
views
How should I unit test multithreaded code?
I have thus far avoided the nightmare that is testing multi-threaded code since it just seems like too much of a minefield. I'd like to ask how people have gone about testing code that relies on ...
783
votes
10
answers
172k
views
Volatile vs. Interlocked vs. lock
Let's say that a class has a public int counter field that is accessed by multiple threads. This int is only incremented or decremented.
To increment this field, which approach should be used, and ...
681
votes
22
answers
513k
views
Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on
I have a scenario. (Windows Forms, C#, .NET)
There is a main form which hosts some user control.
The user control does some heavy data operation, such that if I directly call the UserControl_Load ...
656
votes
30
answers
743k
views
How to get the return value from a thread?
The function foo below returns a string 'foo'. How can I get the value 'foo' which is returned from the thread's target?
from threading import Thread
def foo(bar):
print('hello {}'.format(bar))
...
642
votes
9
answers
467k
views
When to use Task.Delay, when to use Thread.Sleep?
Are there good rule(s) for when to use Task.Delay versus Thread.Sleep?
Specifically, is there a minimum value to provide for one to be effective/efficient over the other?
Lastly, since Task.Delay ...
583
votes
11
answers
205k
views
What is the difference between ManualResetEvent and AutoResetEvent in .NET?
I have read the documentation on this and I think I understand. An AutoResetEvent resets when the code passes through event.WaitOne(), but a ManualResetEvent does not.
Is this correct?
568
votes
14
answers
367k
views
The difference between the Runnable and Callable interfaces in Java
What is the difference between using the Runnable and Callable interfaces when designing a concurrent thread in Java, why would you choose one over the other?
558
votes
21
answers
280k
views
Programmatically find the number of cores on a machine
Is there a way to determine how many cores a machine has from C/C++ in a platform-independent way? If no such thing exists, what about determining it per-platform (Windows/*nix/Mac)?
542
votes
16
answers
976k
views
Undefined reference to pthread_create in Linux
I picked up the following demo off the web from https://computing.llnl.gov/tutorials/pthreads/
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
void *PrintHello(void *...
537
votes
18
answers
189k
views
Why is lock(this) {...} bad?
The MSDN documentation says that
public class SomeObject
{
public void SomeOperation()
{
lock(this)
{
//Access instance variables
}
}
}
is "a problem if the instance can ...
535
votes
24
answers
679k
views
Timeout on a function call
I'm calling a function in Python which I know may stall and force me to restart the script.
How do I call the function or what do I wrap it in so that if it takes longer than 5 seconds the script ...
532
votes
14
answers
225k
views
What is the purpose of Looper and how to use it?
I am new to Android. I want to know what the Looper class does and also how to use it. I have read the Android Looper class documentation but I am unable to completely understand it.
I have seen it in ...
531
votes
7
answers
273k
views
std::unique_lock<std::mutex> or std::lock_guard<std::mutex>?
I have two use cases.
A. I want to synchronise access to a queue for two threads.
B. I want to synchronise access to a queue for two threads and use a condition variable because one of the threads ...
518
votes
22
answers
623k
views
The application may be doing too much work on its main thread
I am new to Android SDK/API environment. It's the first I am trying to draw a plot/chart. I tried running different kinds of sample codes on the emulator using 3 different free libraries, nothing is ...
504
votes
17
answers
635k
views
How can one use multi threading in PHP applications
Is there a realistic way of implementing a multi-threaded model in PHP whether truly, or just simulating it. Some time back it was suggested that you could force the operating system to load another ...
474
votes
15
answers
178k
views
How to check if current thread is not main thread
I need to check if the thread running a certain piece of code is the main (UI) thread or not. How can I achieve this?
471
votes
16
answers
253k
views
What is the meaning of the term "thread-safe"?
Does it mean that two threads can't change the underlying data simultaneously? Or does it mean that the given code segment will run with predictable results when multiple threads are executing that ...
464
votes
27
answers
444k
views
How to wait for all threads to finish, using ExecutorService?
I need to execute some amount of tasks 4 at a time, something like this:
ExecutorService taskExecutor = Executors.newFixedThreadPool(4);
while(...) {
taskExecutor.execute(new MyTask());
}
//......
459
votes
9
answers
130k
views
What is std::promise?
I'm fairly familiar with C++11's std::thread, std::async and std::future components (e.g. see this answer), which are straight-forward.
However, I cannot quite grasp what std::promise is, what it does ...
456
votes
2
answers
181k
views
What is the difference between asynchronous programming and multithreading?
I thought that they were basically the same thing — writing programs that split tasks between processors (on machines that have 2+ processors). Then I'm reading this, which says:
Async methods are ...
455
votes
6
answers
225k
views
What's the difference between Invoke() and BeginInvoke()
Just wondering what the difference between BeginInvoke() and Invoke() are?
Mainly what each one would be used for.
EDIT: What is the difference between creating a threading object and calling ...
451
votes
7
answers
276k
views
Handling InterruptedException in Java
What is the difference between the following ways of handling InterruptedException? What is the best way to do it?
try {
// ...
} catch (InterruptedException e) {
Thread.currentThread().interrupt(...
449
votes
7
answers
306k
views
When to use AtomicReference in Java?
When do we use AtomicReference?
Is it needed to create objects in all multithreaded programs?
Provide a simple example where AtomicReference should be used.
445
votes
8
answers
277k
views
What is the difference between task and thread?
In C# 4.0, we have Task in the System.Threading.Tasks namespace. What is the true difference between Thread and Task. I did some sample program(help taken from MSDN) for my own sake of learning with
...
443
votes
15
answers
297k
views
What is a semaphore?
A semaphore is a programming concept that is frequently used to solve multi-threading problems. My question to the community:
What is a semaphore and how do you use it?
442
votes
15
answers
390k
views
The calling thread cannot access this object because a different thread owns it
My code is as below
public CountryStandards()
{
InitializeComponent();
try
{
FillPageControls();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "...
438
votes
3
answers
402k
views
Task vs Thread differences [duplicate]
There are two classes available in .NET: Task and Thread.
What is the difference between those classes?
When is it better to use Thread over Task (and vice-versa)?
425
votes
7
answers
345k
views
time.sleep -- sleeps thread or process?
In Python for *nix, does time.sleep() block the thread or the process?
424
votes
18
answers
400k
views
How to use background thread in swift? [closed]
How to use threading in swift?
dispatchOnMainThread:^{
NSLog(@"Block Executed On %s", dispatch_queue_get_label(dispatch_get_current_queue()));
}];
423
votes
26
answers
236k
views
Java: notify() vs. notifyAll() all over again
If one Googles for "difference between notify() and notifyAll()" then a lot of explanations will pop up (leaving apart the javadoc paragraphs). It all boils down to the number of waiting threads being ...
423
votes
17
answers
621k
views
How do you kill a Thread in Java?
How do you kill a java.lang.Thread in Java?
422
votes
11
answers
471k
views
Threading pool similar to the multiprocessing Pool?
Is there a Pool class for worker threads, similar to the multiprocessing module's Pool class?
I like for example the easy way to parallelize a map function
def long_running_func(p):
...