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
25
votes
2 answers

DestroyJavaVM thread ALWAYS running

When profiling my application I came across a weird behavior - the DestroyJavaVM thread is ALWAYS running - 100% of the time. After doing a little research on the subject, on which there's hardly any valuable information online, all I understood is…
KidCrippler
  • 1,633
  • 2
  • 19
  • 34
19
votes
2 answers

Why does 'extends Thread' exist, when 'implements Runnable' is winner in all cases

I know that implements Runnable is preferred over extends Thread in Java threads as it allows us to extend some other class if it is required. But if this is the case, does extends Thread also have its own advantages over implements Runnable and if…
rahul
  • 1,095
  • 8
  • 22
12
votes
4 answers

Is there a stack space for every thread?

If I understand correctly the stack is for local primities and references to the objects in the heap. So what happens if you have more than one threads? Do they share the same stack space at the same time (but different areas) or does the JRE…
breakline
  • 5,776
  • 8
  • 45
  • 84
11
votes
4 answers

ThreadPoolExecutor with corePoolSize 0 should not execute tasks until task queue is full

I was going through Java Concurrency In Practice and got stuck at the 8.3.1 Thread creation and teardown topic. The following footnote warns about keeping corePoolSize to zero. Developers are sometimes tempted to set the core size to zero so that…
Steve
  • 889
  • 1
  • 12
  • 26
11
votes
3 answers

Is it possible to set the priority of the threads in Stream.parallel()?

If I want to run a Stream in parallel in a background task is it possible to run it in lower priority? And if so how?
Stephane Grenier
  • 15,527
  • 38
  • 117
  • 192
8
votes
3 answers

How to limit number of threads created and wait main thread until any one thread finds answer?

This is the code to find the first pair of numbers (except 1) whose sum of LCM and HCF is equal to the number. import java.util.*; import java.util.concurrent.atomic.AtomicLong; class PerfectPartition { static long gcd(long a, long b) { …
8
votes
2 answers

Performance of Java Parallel Stream vs ExecutorService

Suppose we have a list and want to pick all the elements satisfying a property(let say some functions f). There are 3 ways to parallel this process. One : listA.parallelStream.filter(element ->…
Polaris
  • 366
  • 1
  • 4
  • 13
8
votes
6 answers

Do multiple threads Using the same object in java make a copy of it?

How do multiple threads in Java handle working with a single object reference passed to them? Do they make a copy of the object and then use it, or do they use the same one? Any technical explanation is welcome. I am unable to comprehend this in…
user7494723
8
votes
6 answers

Divide an uneven number between Threads

I am just learning Threads in Java and I want to sort a list of words alphabetical. My program read the words of a txt-file and put them in a String-array. The user can choose how many threads they want to use themselves. I want to split the array…
JonLunde
  • 151
  • 1
  • 3
  • 10
8
votes
3 answers

ExecutorService's shutdown() doesn't wait until all threads will be finished

I have a code where 4 threads run at the same time. I want to wait until all these 4 threads will be finished. And only after that to continue the app flow. I tried two approaches: Thread#join(), this approach works as expected. The code, which…
Mike
  • 14,010
  • 29
  • 101
  • 161
8
votes
5 answers

How to access running threads inside ThreadPoolExecutor?

I have a queue of running threads and would like to expose some of its data while it is executed, to monitor the process. ThreadPoolExecutor provides access to its queue and I can iterate through these objects to call my overridden toString()…
and_rew
  • 417
  • 2
  • 4
  • 14
7
votes
4 answers

Detecting if you're in the main process or the remote service process in Application

I have an application which has a remote service running in a separate process: I'm also using an Application class:
kalithlev
  • 926
  • 3
  • 12
  • 19
7
votes
2 answers

Which thread does notify wake up?

Imagine I have 3 threads with a wait condition, and a 4th thread with a notify condition. Now, all 3 wait threads run and enter a waiting state. Once this is done, the 4th thread runs and calls notify once. How will the notify determine which…
Jithin KS
  • 161
  • 2
  • 8
7
votes
2 answers

Tomcat memory leak issue of log4j2 thread

I'm using log4j2 for logging, tomcat8 and java8 version. I used attribute "monitorInterval" to periodically check my log4j2.xml. During shutdown my tomcat i'm facing issue of memory leak.How to resolve this memory leak issue? Below are the catalina…
Reetika
  • 1,227
  • 2
  • 17
  • 24
7
votes
1 answer

Shutdown Java Executor After All Submitted Tasks Finished Without Blocking

I'm wondering if there is a way to shutdown a Java ExecutorServiceand allow all submitted tasks to finish while not blocking. to be more specific, I want to schedule a Runnable with a delay and continue with the code, without the need to keep a…
1
2
3
69 70