Skip to main content Accessibility help
×
Hostname: page-component-76fb5796d-25wd4 Total loading time: 0 Render date: 2024-04-28T08:13:37.176Z Has data issue: false hasContentIssue false

8 - Threads

Published online by Cambridge University Press:  08 January 2010

Clark S. Lindsey
Affiliation:
Space-H Services, Maryland
Johnny S. Tolliver
Affiliation:
Oak Ridge National Laboratory, Tennessee
Thomas Lindblad
Affiliation:
Royal Institute of Technology, Stockholm
Get access

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 Press
Print publication year: 2005

Access options

Get access to the full version of this content by using one of the access options below. (Log in options will check for institutional or personal access. Content may require purchase if you do not have access.)

References

Scott Oaks, Henry Wong, Java Threads, 2nd edn, O'Reilly, 1999
Lesson: Threads: Doing Two or More Tasks At Once – The Java Tutorial, Sun Microsystems, http://java.sun.com/docs/books/tutorial/essential/threads/
How to Use Threads in Creating a GUI with JFC/Swing – The Java Tutorial, Sun Microsystems, http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html
AWT Threading Issues – Java 2 Platform, Standard Edition, API Specification, http://java.sun.com/j2se/1.5/docs/api/
Using the Timer and TimerTask Classes – The Java Tutorial, Microsystems, http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html
Using Timers to Run Recurring or Future Tasks on a Background Thread, JDC Tech Tips, May 30, 2000, http://java.sun.com/developer/TechTips/2000/tt0530.html#tip2
John Zukowski, Using Swing Timers, JDC Tech Tips, May 21, 2002, http://java.sun.com/developer/JDCTechTips/2002/tt0521.html

Save book to Kindle

To save this book to your Kindle, first ensure coreplatform@cambridge.org is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.

Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.

Find out more about the Kindle Personal Document Service.

Available formats
×

Save book to Dropbox

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.

Available formats
×

Save book to Google Drive

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.

Available formats
×