Java threading introduction Thread-safety Thread methods Interruption Thread scheduling Context switching Thread priorities sleep() yield() Deadlock Threading with Swing invokeLater() Thread pools CoundDownLatch ThreadPoolExecutor CyclicBarrier
Thread scheduling implications in Java
Our discussion of how threads work and in particular how thread scheduling operates on typical platform leads to implications and restrictions that we can expect from Java threads, which we'll outline here. In some cases, we point to separate pages with fuller discussions.
Firstly, the way that thread scheduling works has implications on various Java methods that control threads (which generally interact with the underlying operating system thread APIs):
"Granularity" of threads
Although our introduction to threading focussed on how to create a thread, it turns out that it isn't appropriate to create a brand new thread just for a very small task. Threads are actually quite a "coarse-grained" unit of execution, for reasons that are hopefully becoming clear from the previous sections.
Overhead and limits of creating and destroying threads
We mentioned that certain structures need to be allocated and deallocated when a thread is created or killed, including a stack and some kind of thread status structure or "control block". In particular, the latter links in to global, shared resources about the currently running threads and its access requires proper synchronization by the OS. The upshot is that:
Although it's rare to do so, as of Java 1.4, it is possible to specify a stack size to the Thread constructor.
Avoiding thread overhead in Java
In applications such as servers that need to continually execute short, multithreaded tasks, the usual way to avoid the overhead of repeated thread creation is to create a thread pool. That is, a number of threads are initially created and then sit permanently waiting for jobs to be sent to them.
Next: context switches
On the next page, we look in more detail at the issue of context switches: namely, what happens when the CPU "juggles" the different threads between the available CPUs. We outline techniques to reduce the number of context switches in Java.
Copyright © Javamex UK 2013. All rights reserved.