Skip to main content

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).

Filter by
Sorted by
Tagged with
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() { //...
user65374's user avatar
  • 24.4k
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 ...
Sarfaraz Nawaz's user avatar
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 ...
James Fassett's user avatar
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 ...
CruelIO's user avatar
  • 18.5k
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.
albruno's user avatar
  • 15.2k
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 ...
Geek's user avatar
  • 23.3k
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 ...
bmurphy1976's user avatar
  • 30.5k
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 ...
Ku Jon's user avatar
  • 12.6k
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 ...
herpderp's user avatar
  • 16k
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 ...
Johanna's user avatar
  • 27.4k
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 ...
John's user avatar
  • 11.2k
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.?
Sudden Def's user avatar
  • 10.3k
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?
user avatar
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?
bmurphy1976's user avatar
  • 30.5k
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?
O..'s user avatar
  • 11.2k
920 votes
27 answers
554k views

What is a daemon thread in Java?

Can anybody tell me what daemon threads are in Java?
rocker's user avatar
  • 11.5k
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 ...
Richard's user avatar
  • 10.3k
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 ...
roiberg's user avatar
  • 13.7k
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 ...
jkp's user avatar
  • 80.6k
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 ...
core's user avatar
  • 32.8k
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 ...
Prerak K's user avatar
  • 11.1k
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)) ...
wim's user avatar
  • 355k
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 ...
Tom K.'s user avatar
  • 6,827
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?
Ben McNiel's user avatar
  • 8,771
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?
Scottm's user avatar
  • 7,084
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)?
hazzen's user avatar
  • 17.3k
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 *...
Ralph's user avatar
  • 6,339
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 ...
Teifion's user avatar
  • 110k
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 ...
Khawar Raza's user avatar
  • 16.1k
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 ...
chmike's user avatar
  • 21.7k
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 ...
user2038135's user avatar
  • 5,201
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 ...
Steve Obbayi's user avatar
  • 6,055
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?
Charlie-Blake's user avatar
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 ...
Varun Mahajan's user avatar
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()); } //......
serg's user avatar
  • 111k
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 ...
Kerrek SB's user avatar
  • 473k
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 ...
user5648283's user avatar
  • 6,113
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 ...
Nathan W's user avatar
  • 55.3k
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(...
softwarematter's user avatar
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.
Chintu's user avatar
  • 4,493
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 ...
user avatar
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?
bmurphy1976's user avatar
  • 30.5k
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, "...
Kuntady Nithesh's user avatar
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)?
Jacek's user avatar
  • 12k
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?
Jeremy Dunck's user avatar
  • 5,804
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())); }];
Anshul's user avatar
  • 4,625
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 ...
Sergey Mikhanov's user avatar
423 votes
17 answers
621k views

How do you kill a Thread in Java?

How do you kill a java.lang.Thread in Java?
flybywire's user avatar
  • 269k
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): ...
Martin's user avatar
  • 12.5k

1
2 3 4 5
2813