Multithreaded Programming in Java
When Two Threads are Better Than One
By Ken Arnold and James Gosling
Many real-world software problems can best be solved by using multiple threads of control. For example, an interactive program that displays data graphically often needs to let users change display parameters in real time. Interactive programs often obtain their best dynamic behavior using threads.
Single-threaded systems usually provide an illusion of multiple threads, by either using interrupts or polling. Polling mixes display and user-input parts of an application, and the display code particularly must be written so it will poll often enough to respond to user input in fractions of a second. Display code must either ensure that display operations take minimal time, or interrupt its own operations to poll. The consequent mixing of two unrelated functional aspects of a program leads to complex and frequently unmaintainable code.
Such problems are more easily solved in a multithreaded system. One thread of control updates the display with current data, and another responds to user input. If user input is complexýfilling out a form, for exampleýdisplay code can run independently until it receives new data. In a polling model, either display updates would have to stop for complex input, or complicated handshaking would have to occur so that display updates could continue while the user typed data into the form. Such a model of sharing control within a process can be directly supported in a multithreaded system instead of being handcrafted for each new polling case.<>