Threads Section Index
What is the Fork/Join framework (JSR 166y)?
The fork/join framework in Java 7 allows developers to take advantage of multiple processors.
What is the purpose of the CopyOnWriteArrayList and CopyOnWriteArraySet collections?
Synchronized operations are costly and if you aren't modifying a collection much, it is best not to synchronize all collection accesses. The CopyOnWriteXXX collections avoid concurrent modificatio...more
What is the primary difference between pessimistic and optimistic concurrency control?
Pessimistic assumes conflicts will occur and avoids them through exclusive locks and explicit synchronization. With the optimistic approach, it is assumed conflicts won't occur, and they are dealt...more
How do I configure a default handler for exceptions that happen in all threads?
The setDefaultUncaughtExceptionHandler() method of Thread, introduced in JDK 5.0, allows you to configure this behavior by letting you attach an implementation of Thread.UncaughtExceptionHandler. more
How can I get the number of active Groups within a ThreadGroup?
ThreadGroups activeGroupCount() returns the number of active groups within the ThreadGroup instance it was called upon. Once the number of active groups is obtained ThreadGroups enumerate(ThreadG...more
What is a ThreadGroup?
A ThreadGroup is used to represent a group of Threads. A tree structure can
be formed from ThreadsGroups containing other ThreadGroups. Threads can only access the ThreadGroup to which they belon...more
What is a ThreadGroup?
A ThreadGroup is used to represent a group of Threads. A tree structure can be formed from ThreadsGroups containing other ThreadGroups. Threads can only access the ThreadGroup to which they belon...more
How can a Thread preempt another Thread?
Any Threads running within the JVM can be preempted by creating and starting a
Thread which is of a higher priority. A Threads priority can be set using the following method.
public void setPr...more
What is a Thread identifer and how can I obtain it?
A Threads identifer is a positive unique long value which is generated when a Thread is created. Using the following method a Threads identifer can be obtained.
public long getId()
Here is a ...more
Is there a way that I can get the number of threads currently executing within the JVM?
Is there a way that I can get the number of threads currently executing
within the JVM?
How can I create a thread pool?
The Executors class of the java.util.concurrent class offers newCachedThreadPool(), newCachedThreadPool(ThreadFactory threadFactory), newFixedThreadPool(int nThreads), newFixedThreadPool(int nThre...more
How can I tell the difference between wait(timeout) timing out and actually being notified?
You can't directly by using wait and notify, but the can use a similar construct with the Lock interface of the java.util.concurrent.locks package, with its boolean tryLock(long time, TimeUnit uni...more
What purpose does the TimeUnit class have?
The TimeUnit class allows you to work with time quanities as both an amount and a unit of measurement. For instance, instead of always working with milliseconds when you want to put a thread to sl...more
How can i tell what state a thread is in?
Prior to Java 5, isAlive() was commonly used to test a threads state.
If isAlive() returned false the thread was either new or terminated but there was simply no way to differentiate between the...more
How can I determine if a thread is alive?
The thread class provides a method which can be called on an
instance of a Thread to determine if it is currently executing.
If isAlive() returns false the thread is either a new thread that is ...more