16.4. Summary

In this chapter you've learned about threads and the basics of how you can create and manage them. You'll be using threads from time to time in examples later in this book, so be sure you don't move on from here without being comfortable with the basic ideas of how you create and start a thread.

The essential points that I have covered in this chapter are:

  • Threads are subtasks in a program that can be in execution concurrently.

  • A thread is represented by an object of the class Thread. Execution of a thread begins with the execution of the run() method defined in the class Thread.

  • You define the code to be executed in a thread by implementing the run() method in a class derived from Thread, or in a class that implements the interface Runnable.

  • A thread specified as daemon will cease execution when the thread that created it ends.

  • A thread that isn't a daemon thread is called a user thread. A user thread will not be terminated automatically when the thread that created it ends.

  • You start execution of a thread by calling the start() method for its Thread object. If you need to halt a thread before normal completion, you can stop execution of a thread by calling the interrupt() method for its Thread object.

  • Methods can be declared as synchronized. Only one synchronized instance method for an object can execute at any given time. Only one synchronized static method for a class can execute at one time.

  • A code block can be declared as synchronized on an object. Only one synchronized ...

Get Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.