Book contents
- Frontmatter
- Contents
- Preface
- Acknowledgements
- Part I Introduction to Java
- 1 Introduction
- 2 Language basics
- 3 Classes and objects in Java
- 4 More about objects in Java
- 5 Organizing Java files and other practicalities
- 6 Java graphics
- 7 Graphical User Interfaces
- 8 Threads
- 9 Java input/output
- 10 Java utilities
- 11 Image handling and processing
- 12 More techniques and tips
- Part II Java and the network
- Part III Out of the sandbox
- Appendix 1 Language elements
- Appendix 2 Operators
- Appendix 3 Java floating-point
- Index
- References
8 - Threads
Published online by Cambridge University Press: 08 January 2010
- Frontmatter
- Contents
- Preface
- Acknowledgements
- Part I Introduction to Java
- 1 Introduction
- 2 Language basics
- 3 Classes and objects in Java
- 4 More about objects in Java
- 5 Organizing Java files and other practicalities
- 6 Java graphics
- 7 Graphical User Interfaces
- 8 Threads
- 9 Java input/output
- 10 Java utilities
- 11 Image handling and processing
- 12 More techniques and tips
- Part II Java and the network
- Part III Out of the sandbox
- Appendix 1 Language elements
- Appendix 2 Operators
- Appendix 3 Java floating-point
- Index
- References
Summary
Introduction
Threads in Java are processes that run in parallel within the Java Virtual Machine. When the JVM runs on a single real processor the parallelism is, of course, only apparent because of the high speed switching of threads in and out of the processor. Yet even in that case, threading can provide significant advantages. For example, while one thread deals with a relatively slow I/O operation such as downloading a file over the network, other threads can do useful work. Threads can assist in modularizing program design. An animation can assign different threads to rendering the graphics, to sound effects, and to user interactions so that all of these operations appear to take place simultaneously. Furthermore, a JVM can assign Java threads to native OS threads (but isn't required to) and on a multiprocessor system it could thus provide true parallel performance.
Java makes the creation and running of threads quite easy. We will concentrate on the basics of threading and only briefly touch on the subtle complications that arise when multiple threads interact with each other and need to access and modify common resources. Such situations can result in data race, deadlock, and other interference problems that result in distorted data or hung programs.
Introduction to threads
In Java you can create one or more threads within your program just as you can run one or more programs in an operating system [1–4].
- Type
- Chapter
- Information
- Publisher: Cambridge University PressPrint publication year: 2005