ch09 - 9 Threads CERTIFICATION OBJECTIVES Defining,...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 9 Threads CERTIFICATION OBJECTIVES Defining, Instantiating, and Starting Threads Preventing Thread Execution Synchronizing Code Thread Interaction Two-Minute Drill Q&A Self Test 2 Chapter 9: Threads CERTIFICATION OBJECTIVE Defining, Instantiating, and Starting Threads (Exam Objective 7.1) Write code to define, instantiate, and start new threads using both java.lang.Thread and java.lang.Runnable. Imagine a stockbroker application with a lot of complex behavior that the user initiates. One of the applications is download last stock option prices, another is check prices for warnings, and a third time-consuming operation is, analyze historical data for company XYZ. In a single-threaded runtime environment, these actions execute one after another. The next action can happen only when the previous one is finished. If a historical analysis takes half an hour, and the user selects to perform a download and check afterward, the warning may come too late to, say, buy or sell stock as a result. We just imagined the sort of application that cries out for multithreading. Ideally, the download should happen in the background (that is, in another thread). That way, other processes could happen at the same time so that, for example, a warning could be communicated instantly. All the while, the user is interacting with other parts of the application. The analysis, too, could happen in a separate thread, so the user can work in the rest of the application while the results are being calculated. So what exactly is a thread? In Java, thread means two different things: An instance of class java.lang.Thread A thread of execution An instance of Thread is justan object. Like any other object in Java, it has variables and methods, and lives and dies on the heap. But a thread of execution is an individual process (a lightweight process) that has its own call stack. In Java, there is one thread per call stack or, to think of it in reverse, one call stack per thread . Even if you dont create any new threads in your program, threads are back there running. The main() method that starts the whole ball rolling runs in one thread, called (surprisingly) the main thread. If you looked at the main call stack (and you can, anytime you get a stack trace from something that happens after main begins, but not within another thread) youd see that main() is the first method on the stackthe method at the bottom. But as soon as you create a new thread, a new stack materializes and methods called from that thread run in a call stack thats separate from the main() call stack. That second new call stack is said to run concurrently with the main thread, but well refine that notion as we go through this chapter....
View Full Document

Page1 / 65

ch09 - 9 Threads CERTIFICATION OBJECTIVES Defining,...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online