Introduction to Java threads
Presented by developerWorks, your source for great tutorials ibm.com/developerWorks
Table of Contents
If you're viewing this document online, you can click any of the topics below to link directly to that section.
1. About this tutorial....................................................... 2. Thread basics ........................................................... 3. A thread's life............................................................ 4. Threads everywhere ................................................... 5. Sharing access to data ................................................ 6. Synchronization details................................................ 7. Additional thread API details ......................................... 8. Wrapup and resources ................................................
2 3 8 13 16 22 26 28
Introduction to Java threads
Page 1 of 30
ibm.com/developerWorks
Presented by developerWorks, your source for great tutorials
Section 1. About this tutorial What is this tutorial about?
This tutorial explores the basics of threads -- what they are, why they are useful, and how to get started writing simple programs that use them. We will also explore the basic building blocks of more sophisticated threading applications -how to exchange data between threads, how to control threads, and how threads can communicate with each other.
Should I take this tutorial?
This tutorial is for Java programmers who have a good working knowledge of the Java language, but who have limited experience with multithreading or concurrency. At the completion of this tutorial, you should be able to write simple programs that use threads. You should also be able to read and understand programs that use threads in straightforward ways.
About the author
Brian Goetz is a regular columnist on the developerWorks Java technology zone and has been a professional software developer for the past 15 years. He is a Principal Consultant at Quiotix, a software development and consulting firm located in Los Altos, California. See Brian's published and upcoming articles in popular industry publications. Contact Brian at brian@quiotix.com.
Page 2 of 30
Introduction to Java threads
Presented by developerWorks, your source for great tutorials
ibm.com/developerWorks
Section 2. Thread basics What are threads?
Nearly every operating system supports the concept of processes -- independently running programs that are isolated from each other to some degree. Threading is a facility to allow multiple activities to coexist within a single process. Most modern operating systems support threads, and the concept of threads has been around in various forms for many years. Java is the first mainstream programming language to explicitly include threading within the language itself, rather than treating threading as a facility of the underlying operating system. Threads are sometimes referred to as lightweight processes. Like processes, threads are independent, concurrent paths of execution through a program, and each thread has its own stack, its own program counter, and its own local variables. However, threads within a process are less insulated from each other than separate processes are. They share memory, file handles, and other per-process state. A process can support multiple threads, which appear to execute simultaneously and asynchronously to each other. Multiple threads within a process share the same memory address space, which means they have access to the same variables and objects, and they allocate objects from the same heap. While this makes it easy for threads to share information with each other, you must take care to ensure that they do not interfere with other threads in the same process. The Java thread facility and API is deceptively simple. However, writing complex programs that use threading effectively is not quite as simple. Because multiple threads