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
5
votes
1 answer

Java stops at thread's try-catch

I'm trying to write a code that will return my raspberry's IP when it's on the same network as my computer. The idea is for it to make a broadcast like Samba (Broadcast resolution is the closest to the original NetBIOS mechanism. Basically, a client…
Laura Martins
  • 583
  • 2
  • 6
  • 15
5
votes
0 answers

What makes Android main thread keeps on running unlike other threads

How the main thread keeps on running unlike other threads which terminates after executing their task.?
S.D
  • 85
  • 8
5
votes
5 answers

multiThreading difference between join and wait,notify

I have not worked in multithreading, what is the difference between join and wait ,notify method ? is the difference only limited to obtain lock and refrain other threads from accessing it or are there other use cases? Why should I go for wait and…
GoodToLearn
  • 41
  • 1
  • 5
5
votes
6 answers

How to notify a specific thread in Java

How I can call a particular thread in inter-thread communication? In the program below I have two threads t1 and t2. When I call t1.notify() it raises: Exception in thread "Thread-1" java.lang.IllegalMonitorStateException at…
Muniyasamy V
  • 81
  • 1
  • 4
  • 9
5
votes
1 answer

What's the issue with "Long monitor contention event"

I have the following service code where I launch a thread responsible for dispatching messages are they come in. public void run() { while (! Thread.interrupted()) { try { Message msg = null; …
T. Francis
  • 169
  • 1
  • 12
5
votes
1 answer

What is the order of execution of newly created threads in java

class Test { boolean isFirstThread = true; private synchronized void printer(int threadNo) { if(isFirstThread) { try { Thread.sleep(2000); } catch (InterruptedException e) { …
siva
  • 163
  • 4
5
votes
2 answers

Why can't notifyAll() be used in a Thread instance?

I'm using a source code analyser that pointed out that notifyAll() should not be called within a Thread instance. I'm trying to articulate this to management, but I can't come up with an explanation. Can someone help me?? BTW, this is inherited…
CNDyson
  • 1,687
  • 7
  • 28
  • 63
5
votes
3 answers

How to stop thread from continuing to execute infinite loop

I have a method to return an HTTP response after a Groovy script is executed. I've created an anonymous thread that is supposed to execute the Groovy script. However, as a precaution, I want the thread to stop after 3 seconds so as to prevent bad…
L Becker
  • 685
  • 8
  • 28
5
votes
4 answers

Does synchronized (this) lock only the synchronized block or all the "this" code?

public class ObjectCounter { private static long numOfInstances = 0; public ObjectCounter(){ synchronized(this){ numOfInstances++; } } **public static synchronized long getCount(){ return…
5
votes
2 answers

Android - Difference between Thread and AsyncTask?

In my app i have buttons, when clicked will query the database and show result on screen. The query action will normally take 1 ~ 3 sec. These buttons will be clicked very often. I've implemented this action on both AsyncTask and Thread but see very…
Tran Ngu Dang
  • 2,540
  • 6
  • 29
  • 38
5
votes
1 answer

synchronized object not locked by thread before notifyAll()

I want to have a boolean to notify some sections of the system that a specific service started. For some strange reason I'm getting the error java.lang.IllegalMonitorStateException: object not locked by thread before notifyAll(). What is strange is…
J-Rou
  • 2,216
  • 8
  • 32
  • 39
4
votes
4 answers

How java threads are scheduled?

I have recently started multi-threaded programming with Java in case of Linux threads , i know that the kernel schedules them(as they are the unit entities that are scheduled)but java programs are run on JVM which in my system (RHEL 6.1) is…
Tanay
  • 49
  • 3
4
votes
1 answer

How can I gracefully end execution in a java executor without returning or using an exception?

I have codebase that calls a black box, that in turn calls other code I have control of, call it thing.innerMethod(). The code in the black box needs to execute in a different thread than the code that calls it, so I have decided to use an…
Dan
  • 12,157
  • 12
  • 50
  • 84
4
votes
0 answers

Is there any benefit to Thead.onSpinWait() while doing CPU Bound work?

Yes, this is similar to onSpinWait​() method of Thread class - Java 9, but a little more nuanced... static boolean isPrime(long candidate) { if ((candidate & 1) == 0) // filter out even numbers return (candidate == 2); // except for…
Eric Kolotyluk
  • 1,958
  • 2
  • 21
  • 30
4
votes
2 answers

Java execute multiple Threads and wait for completion

I'm writing a small program which is supposed to update Firmware on Servers. The way I currently do this is by issuing a command via the ProcessBuilder, executing it and using exitCode = process.waitFor(); to wait until the command is finished. Some…
Korlimann
  • 147
  • 14