Threads Section Index | Page 9
How can one thread wait for another thread to die before it continues execution?
The thread's join() method allows you to wait for another thread to finish execution.
Thread t1 = new Thread(runnable);
t1.start();
// do stuff
...
// wait for t1 to finish
t1.join()
more
How do I create a new thread and have it start running?
Creating a thread involves creating a new Thread and invoking its start() method. Calling start() causes the run() method of the Thread subclass or the Runnable object passed to the Thread constru...more
How do I get a thread to pause?
The static sleep() method of the Thread class will let your thread sleep for a set number of milliseconds (or nanoseconds). When the thread wakes up, it will be scheduled to execute in the future....more
How does a preemptive scheduler manage a thread's timeslice?
The lowest level preemptive scheduler (kernel layer) uses the system timer interrupt and context switching to manage timeslices. When any CPU interrupt occurs, the CPU makes a note of where it wa...more
What is a thread?
A thread is a set of instructions executing apart from other threads (with its own stack) but sharing the same memory space (with the same heap).
What is the main difference between a preemptive scheduler and a non-preemptive scheduler?
A preemptive scheduler interrupts a thread of execution when its timeslice runs out. A non-preemptive (or "cooperative") scheduler waits for the thread to yield control.
[Java native t...more
What is the use of start() function in starting a thread? Why we do not use the run() funtion directly to run the thread?
The start() method tells the Java Virtual Machine that it needs to create a system specific thread. After creating the system resource, it passes the Runnable object to it to execute its run() met...more
What object does non-static synchronized methods use for locking?
Non-static synchronized methods synchronize on the instance (this) of the class.
What object does static synchronized methods use for locking?
Static synchronized methods synchronize on the class object (this.getClass()) of the class.
Where can I find a set of data structures to deal with many of the concurrent programming concepts?
Doug Lea has produced a utility library in conjunction with his Concurrent Programming in Java that includes many common classes needed to help you synchronize your data access, like mutex and sem...more
Why are resources tied to ThreadGroups?
Every thread of execution on any given platform has its own context, which contains an execution state, and a set of access rights (among other things). A thread's access rights are equal to tha...more
How do I create a variable local to a Thread?
The Java 1.2 release introduced the concept of thread local variables as a standard class. Previously, they existed in the sun.server.util package of the Java WebServer product. Now, you get them ...more
Are there any good books on using Java threads out there?
It depends upon your needs. The O'Reilly Java Threads book is a great book for teaching about using the API. Doug Lea's book, Concurrent Programming in Java discusses how to properly design your p...more
Where can I get free source code for multi-threading in Java?
Check out
Doug Lea's
concurrent programming package.
more
How can I catch exceptions thrown in secondary threads I create?
You need to create the secondary threads in a custom ThreadGroup. Subclass ThreadGroup and override public void uncaughtException(Thread source, Throwable throwable). This method would then be not...more