concept thread in category software development

This is an excerpt from Manning's book Test Driven: Practical TDD and Acceptance TDD for Java Developers.
Let’s clarify the role of the Intimate Inner Class through a code example. Listing 4.16 presents an example scenario where we’d like to test that a Server object obtains a Thread from its configured ThreadFactory upon startup and suspends it when stopped. The solution is to use an Intimate Inner Class, which assigns the newly created Thread instance to the thread field on the surrounding test class.
A deadlock is a situation where two or more threads enter a state where each is waiting for an action on the other thread’s part (which will never happen because both threads are waiting). Starvation, on the other hand, is a situation where a thread doesn’t get a chance to execute, because the thread scheduler for one reason or another doesn’t schedule CPU cycles for the starving thread. Deadlocks and starvation basically take away the program’s liveness—that is, the system’s threads’ ability to eventually finish execution.
One of the things multithreaded systems do is start and stop threads. Some systems start and stop a lot of them, and some less. Some software architectures hide the threads from the application developer, and some expose all the gritty details. Just in case we need to write code that starts and stops threads, let’s look at a small example using some of the new APIs introduced as part of the java.util.concurrent package in Java 5.
Let’s say we are developing some kind of a Server class, which we want to be able to start and stop. When the Server is started, it starts a thread in the background to listen for (for example—we don’t care) incoming network connections. When the Server is stopped, the background thread is also stopped.
We’re going to give the Server a custom ThreadFactory, which creates a Thread object we can observe from our test. First, we need a custom Thread class that provides the necessary information. Listing 7.15 shows one such Thread class.
What we have in listing 7.15 is a simple class that
wraps the regular Thread implementation’s run method with two synchronization objects we call latches. These latches are used for waiting until the thread has started and until it is stopping—that is, exiting its run method. The
waitForStarted and waitForStopped methods can be used by other threads to block until this thread has been started or until it has completed its execution.
the Intimate Inner Class is
sharing the thread field with the test class, allowing the test to
access the underlying Thread instance for its assertions.