Download presentation
Presentation is loading. Please wait.
Published byFelix Brown Modified over 9 years ago
1
111 © 2002, Cisco Systems, Inc. All rights reserved.
2
2 Threads Chapter 15
3
333 © 2002, Cisco Systems, Inc. All rights reserved. Learning Objectives Threads and multithreading The Thread class and Runnable interface Creating and running threads The lifecycle of a thread Managing threads
4
444 © 2002, Cisco Systems, Inc. All rights reserved. Overview This chapter explores the usage of multithreading to improve program efficiency. Threads are created from the Thread class and Runnable interface. The run() method is executed concurrently after threads have started. The five states of the thread lifecycle are born, ready, running, blocked, and dead. Multiple threads can share code and data, so careful thread management must be practiced to conserve the integrity of data.
5
555 © 2002, Cisco Systems, Inc. All rights reserved. Overview (continued) Threads are scheduled by the CPU with time slicing or preemption. The sleep method and yield methods allow other threads access to the CPU. The wait and notify methods are used to facilitate thread communication. Synchronizing threads protects objects from becoming invalid. Deadlock occurs when all active threads are in the blocked state.
6
666 © 2002, Cisco Systems, Inc. All rights reserved. Key Terms Thread runnable Low-priority thread High-priority thread Multiprocessing computers Virtual CPU Time slicing Preemption run(), start()
7
777 © 2002, Cisco Systems, Inc. All rights reserved. Key Terms (continued) IllegalThreadStateException Born state, ready state, blocked state, wait state, dead state sleep(), join(), yield(), wait(), notify(), stop(), suspend(), resume() Wait pool Synchronized Dead locks
8
888 © 2002, Cisco Systems, Inc. All rights reserved. Understanding Threads A thread is a path of execution. Multithreading allows more than one path of execution through a program. Processes can occur concurrently in multithreaded programs.
9
999 © 2002, Cisco Systems, Inc. All rights reserved. Understanding Threads (continued) A thread is the encapsulation of a virtual CPU and its own program code and data. The java.lang.Thread class is used to create multithreaded programs. Threads share code when they are operating on instances of the same class. Threads share data when they are operating on references to the same object.
10
10 © 2002, Cisco Systems, Inc. All rights reserved. Executing Threads Concurrently The operating system manages thread scheduling with time slicing or preemption. Thread scheduling varies among operating systems. In time slicing, each thread is given an amount of time to process before control is transferred to another thread. In preemption, threads of higher priority have access to the CPU and lower-priority threads must wait.
11
11 © 2002, Cisco Systems, Inc. All rights reserved. The Thread Class There are several constructors for a Thread object. All Thread objects implement the Runnable interface.
12
12 © 2002, Cisco Systems, Inc. All rights reserved. Runnable Interface Any object that is executed as a thread must implement Runnable. Runnable objects must define a run() method with no arguments. The run() method contains the statements to be executed concurrently with other threads. A Thread class can be created by implementing Runnable and passing an instance of itself to a Thread constructor.
13
13 © 2002, Cisco Systems, Inc. All rights reserved. Creating and Running Threads There are several ways to create a thread: Create a Thread object and pass a Runnable target through the constructor. Extend the Thread class and override the run method. Extend the Thread class and pass a Runnable target through the constructor.
14
14 © 2002, Cisco Systems, Inc. All rights reserved. Running a Thread To run a thread, the thread’s start() method is called. The start() method performs several tasks and calls the thread’s run() method. The thread becomes ready for execution when the start() method is called. The start() method returns control to the method in which it was called, and the caller runs concurrently with the thread.
15
15 © 2002, Cisco Systems, Inc. All rights reserved. Thread States
16
16 © 2002, Cisco Systems, Inc. All rights reserved. Thread States Born state – The thread has just been created. Ready state – The thread’s start() method has been called but it is not currently running. Running state – The thread is using processor time to execute statements. Blocked state – A thread is waiting to complete the run() method. Dead state – A thread’s run() method has completed or terminated.
17
17 © 2002, Cisco Systems, Inc. All rights reserved. Thread Methods – sleep() Thread.sleep(1000); The sleep method is a static method of the Thread class. The sleep method causes the current thread to enter the blocked state. The thread remains in the blocked state for the number of milliseconds specified in the sleep method. An InterruptedExeption is thrown by the sleep method if the sleeping thread is interrupted by another thread.
18
18 © 2002, Cisco Systems, Inc. All rights reserved. Thread Methods – join() The join method causes the current thread to wait until the thread on which the join method is called terminates. The join method can be called with a specified timeout. The caller will terminate at the end of the specified time if the other thread has not terminated. A timeout of zero signifies that the thread will not time out.
19
19 © 2002, Cisco Systems, Inc. All rights reserved. Thread Methods – yield() The yield method is a static method of the Thread class. The yield method causes the current thread to pause so that other threads can access the CPU. Only threads of the same priority are given access to the CPU.
20
20 © 2002, Cisco Systems, Inc. All rights reserved. Thread Priorities A programmer cannot control thread scheduling but can assign thread priorities. Thread priorities are static fields of the Thread class and affect the scheduling of threads.
21
21 © 2002, Cisco Systems, Inc. All rights reserved. Stopping a Thread The Thread methods stop and suspend are deprecated because they are inherently unsafe. The following steps should be used to safely stop a thread.
22
22 © 2002, Cisco Systems, Inc. All rights reserved. Synchronizing Threads A programmer must ensure that multiple threads do not access the same data simultaneously. Threads simultaneously changing object data can produce an unsafe object. To prevent this problem, the keyword synchronized is used. Both methods and code blocks can be declared synchronized. The thread has exclusive access to an object by obtaining an object lock.
23
23 © 2002, Cisco Systems, Inc. All rights reserved. Keyword Synchronized The synchronized keyword allows interaction with the object lock.
24
24 © 2002, Cisco Systems, Inc. All rights reserved. Keyword Synchronized (continued) When a thread is executing a synchronized block of code, no other thread has access to the object data referenced in the block. A programmer must ensure that all references to the object data are enclosed in synchronized blocks. The object lock is automatically released when the synchronized block terminates. If an object lock is unavailable, the requesting thread is blocked until the lock is released.
25
25 © 2002, Cisco Systems, Inc. All rights reserved. Deadlock Deadlock occurs when all running threads enter the blocked state. Deadlock can occur if multiple threads are accessing multiple objects and each is waiting for the other to release a lock. Java has no way to detect or prevent deadlock. Deadlock occurs due to a logic error.
26
26 © 2002, Cisco Systems, Inc. All rights reserved. Communication Between Threads The wait and notify methods of the Object class are used for thread communication. The wait method is called by a thread on a certain object when it must wait to proceed until another thread performs some action. The notify method is called by a thread to let the other thread know that it can now proceed.
27
27 © 2002, Cisco Systems, Inc. All rights reserved. The wait and notify Methods The wait and notify methods can only be called when the thread obtains an object lock. They can only be called from within a synchronized block of code. After a wait or notify method is called, the object lock is released. The thread that has called a wait method is placed in the wait pool.
28
28 © 2002, Cisco Systems, Inc. All rights reserved. The Wait Pool The notify method moves a thread from the wait pool to the lock pool. The notifyAll method moves all threads from the wait pool to the lock pool.
29
29 © 2002, Cisco Systems, Inc. All rights reserved. Summary Threads are used to create multiple paths of execution through a program. A thread is the encapsulation of a virtual CPU, code, and data. There are several ways to create threads, including extension of Thread and implementation of Runnable. In all threads, the run() method is overridden.
30
30 © 2002, Cisco Systems, Inc. All rights reserved. Summary (continued) Thread methods include sleep, join, and yield. The stop method should not be used to terminate a thread because it can leave objects in an inconsistent state. The wait and notify methods of the Object class are used for Thread communication when sharing data. The keyword synchronized is used to obtain a lock on an object and secure exclusive access.
31
31 © 2002, Cisco Systems, Inc. All rights reserved.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.