Questions tagged [java-threads]

For questions related to Java threads, including concurrent data structures, the fork-join framework, atomic classes, thread locking/synchronization, visibility and latency

Examples include:

  • Usages of java.util.concurrent.* data structures
  • Fork-join framework,
  • Atomic classes
  • Thread locking/synchronization, visibility and latency
1037 questions
2348
votes
42 answers

"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() { //Code } } //Started with a "new Thread(new…
user65374
  • 24,139
  • 4
  • 19
  • 7
1342
votes
33 answers

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 cycles correct? Why do we have both wait() and…
Geek
  • 23,089
  • 20
  • 71
  • 85
220
votes
12 answers

If I synchronized two methods on the same class, can they run simultaneously?

If I synchronized two methods on the same class, can they run simultaneously on the same object? For example: class A { public synchronized void methodA() { //method A } public synchronized void methodB() { // method B …
Shelef
  • 3,707
  • 6
  • 23
  • 28
151
votes
6 answers

Difference between Service, Async Task & Thread?

What is the difference between Service, Async Task & Thread. If i am not wrong all of them are used to do some stuff in background. So, how to decide which to use and when?
SpunkerBaba
  • 2,127
  • 4
  • 18
  • 13
141
votes
6 answers

Difference between WAIT and BLOCKED thread states

What is the difference between thread state WAIT and thread state BLOCKED? The Thread.State documentation: Blocked A thread that is blocked waiting for a monitor lock is in this state. Waiting A thread that is waiting indefinitely for another…
More Than Five
  • 9,959
  • 21
  • 77
  • 127
132
votes
12 answers

How to use wait and notify in Java without IllegalMonitorStateException?

I have 2 matrices and I need to multiply them and then print the results of each cell. As soon as one cell is ready I need to print it, but for example I need to print the [0][0] cell before cell [2][0] even if the result of [2][0] is ready first.…
Greg
96
votes
9 answers

Turning an ExecutorService to daemon in Java

I am using an ExecutoreService in Java 1.6, started simply by ExecutorService pool = Executors.newFixedThreadPool(THREADS). When my main thread is finished (along with all the tasks processed by the thread pool), this pool will prevent my program…
Antiz
  • 1,424
  • 1
  • 13
  • 20
48
votes
7 answers

When are daemon threads useful?

I know that Deamon threads are background threads. We can create our own daemon thread by calling setDaemon(true). My question is: Why and when do we need to create our thread as a daemon thread?
user414967
  • 5,225
  • 10
  • 40
  • 61
46
votes
2 answers

Why Thread.sleep is bad to use

Apologies for this repeated question but I haven't found any satisfactory answers yet. Most of the question had their own specific use case: Java - alternative to thread.sleep Is there any better or alternative way to skip/avoid using…
RandomQuestion
  • 6,778
  • 17
  • 61
  • 97
40
votes
4 answers

onSpinWait​() method of Thread class - Java 9

While learning Java 9 features I came across a new method of Thread class, called onSpinWait​. As per javadocs, this method is used for this: Indicates that the caller is momentarily unable to progress, until the occurrence of one or more actions…
T-Bag
  • 10,916
  • 3
  • 54
  • 118
39
votes
3 answers

Does Thread.yield() do anything if we have enough processors to service all threads?

If we are in a situation with two running threads on a machine with two processors and we call Thread.yield() within one of those threads, does it stand to reason that nothing will happen (the scheduler will essentially ignore the request) because…
Dave
  • 15,639
  • 133
  • 442
  • 830
35
votes
4 answers

Java thread state transition, WAITING to BLOCKED, or RUNNABLE?

There seems to be a discrepancy between SO consensus and nearly every Java thread state diagram on the Internet; specifically, regarding thread state transition from WAITING after notify() or notifyAll() is invoked... WAITING never goes directly to…
raffian
  • 31,267
  • 26
  • 103
  • 174
33
votes
1 answer

Explanation of the Thread-Local Handshakes

As part of the putative JDK 10 this JEP 312: Thread-Local Handshakes was proposed. I've tried to grasp its description, but I am still not confident that I got the idea properly. Is it essentially an attempt to reanimate something similar to the…
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
27
votes
3 answers

what is RMI TCP connection in Visual VM

I'm making a desktop application in java and am doing some memory optimisations. That made me come across two threads running in the JVM, both named: RMI TCP connection And they're both contributing to heap growth quite considerably (at my…
Jake
  • 843
  • 1
  • 7
  • 18
25
votes
1 answer

java code execution yields to different results in debug without breakpoints and normal run. Is ExecutorService broken?

TL:DR ExecutorService executorService = Executors.newFixedThreadPool(8); in debug runs concurrent, but in normal runtime it starts concurrent, but later runs in single thread. I have some code where I start 4 different tasks in ExecutorService. Two…
mlecz
  • 985
  • 11
  • 19
1
2 3
69 70