Today we will study about Introduction to Threads. This lesson is part of Chapter 8.5 of Introduction to Programming Using Java.
Introduction to Threads
Like people, computers can multitask. That is, they can be working on several different tasks at the same time. A computer that has just a single central processing unit can’t literally do two things at the same time, any more than a person can, but it can still switch its attention back and forth among several tasks.
Furthermore, it is increasingly common for computers to have more than one processing unit, and such computers can literally work on several tasks simultaneously.
It is likely that from now on, most of the increase in computing power will come from adding additional processors to computers rather than from increasing the speed of individual processors. To use the full power of these multiprocessing computers, a programmer must do parallel programming, which means writing a program as a set of several tasks that can be executed simultaneously.
Even on a single-processor computer, parallel programming techniques can be useful, since some problems can be tackled most naturally by breaking the solution into a set of simultaneous tasks that cooperate to solve the problem.
In Java, a single task is called a thread. The term “thread” refers to a “thread of control” or “thread of execution,” meaning a sequence of instructions that are executed one after another— the thread extends through time, connecting each instruction to the next.
In a multithreaded program, there can be many threads of control, weaving through time in parallel and forming the complete fabric of the program. (Ok, enough with the metaphor, already!) Every Java program has at least one thread; when the Java virtual machine runs your program, it creates a thread that is responsible for executing the main routine of the program.
This main thread can in turn create other threads that can continue even after the main thread has terminated. In a GUI program, there is at least one additional thread, which is responsible for handling events and drawing components on the screen. This GUI thread is created when the first window is opened.
So in fact, you have already done parallel programming! When a main routine opens a window, both the main thread and the GUI thread can continue to run in parallel. Of course, parallel programming can be used in much more interesting ways.
Unfortunately, parallel programming is even more difficult than ordinary, single-threaded programming. When several threads are working together on a problem, a whole new category of errors is possible. This just means that techniques for writing correct and robust programs are even more important for parallel programming than they are for normal programming.
(That’s one excuse for having this section in this chapter—another is that we will need threads at several points in future chapters, and I didn’t have another place in the book where the topic fits more naturally.) Since threads are a difficult topic, you will probably not fully understand everything in this section the first time through the material. Your understanding should improve as you encounter more examples of threads in future sections.
Source:
- Chapter 8.5 – Introduction to Threads | Introduction to Programming Using Java
SEE MORE:
- Chapter 8.5.3 – Mutual Exclusion with synchronized | Introduction to Programming Using Java
- Chapter 8.5.5 Volatile Variables | Introduction to Programming Using Java
- Quiz on Chapter 8 | Introduction to Programming Using Java
- Chapter 9.1 Recursion | Introduction to Programming Using Java
- Chapter 9.1.2 Towers of Hanoi | Introduc’tion to Programming Using Java
- Chapter 9.3.1 Stacks | Introduction to Program’ming Using Java
- Chapter 9.4.1 Tree Traversal | Introduction to Program,ming Using Java
- Chapter 10.1.1 Generic Programming in Smalltalk | Introduc’tion to Program ming Using Java
- Chapter 12 Advanced GUI Programming | Images and Resources | Introduction to Program ming Using Java