Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 13 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
13
Dung lượng
1,32 MB
Nội dung
Slide 1/12 A Guide to Advanced Java - Module 1 - Thread 30/07/2008 Module 1 Introduction to Thread • Objecve Slide 2/12 A Guide to Advanced Java - Module 1 - Thread 30/07/2008 Dene Threads Slide 3/12 A Guide to Advanced Java - Module 1 - Thread 30/07/2008 Slide 4/12 A Guide to Advanced Java - Module 1 - Thread 30/07/2008 Creang Threads • Sub classing Thread: – Creang subclass – Overriding the run() method – Starng the thread • Using the Runnable interface: – Implemenng the Runnable interface – Implemenng the run() method – Starng the thread Slide 5/12 A Guide to Advanced Java - Module 1 - Thread 30/07/2008 Thread states Slide 6/12 A Guide to Advanced Java - Module 1 - Thread 30/07/2008 Methods of the Thread class • The currentThread() method returns the thread running. • The getName() method helps to retrieve the name of the current thread. • The setName() method assigns a name to a thread. • The start() method allocates the system resources necessary to run the thread and executes the run() method of its target object. • The run() method contains instrucons, which are executed when the start() method is invoked. Slide 7/12 A Guide to Advanced Java - Module 1 - Thread 30/07/2008 Methods of the Thread class … Slide 8/12 A Guide to Advanced Java - Module 1 - Thread 30/07/2008 Methods of the Thread class … Slide 9/12 A Guide to Advanced Java - Module 1 - Thread 30/07/2008 Managing Thread • Thread priority – Thread.MAX_PRIORITY : constant value of 10 – Thread.NORM_PRIORITY : constant value of 5, default – Thread.MIN_PRIORITY : constant value of 1 • Methods: – setPriority() – getPriority() • Thread priority helps the thread scheduler to decide which thread to run • Priority also helps the OS to decide the amount of resource that has to be allocated to each thread Slide 10/12 A Guide to Advanced Java - Module 1 - Thread 30/07/2008 Daemon Threads • setDaemon() method • isDaemon() method [...]...Needs for Daemon Thread 30/07/2008 A Guide to Advanced Java - Module 1 - Thread Slide 11/12 Summary 30/07/2008 A Guide to Advanced Java - Module 1 - Thread Slide 12/12 LAB & Exercises LAB 1) 2) 3) 4) 5) 6) Extending the Thread class Implementing the Runnable Creating the Thread objects Using sleep() and getName() methods Displaying Thread class method descriptions Setting and getting Thread priorities... the Thread objects Using sleep() and getName() methods Displaying Thread class method descriptions Setting and getting Thread priorities Exercises Write a thread displaying an electronic clock 30/07/2008 A Guide to Advanced Java - Module 1 - Thread Slide 13/12 . to Advanced Java - Module 1 - Thread 30/07/2008 Managing Thread • Thread priority – Thread. MAX_PRIORITY : constant value of 10 – Thread. NORM_PRIORITY : constant value of 5, default – Thread. MIN_PRIORITY. Threads Slide 3/12 A Guide to Advanced Java - Module 1 - Thread 30/07/2008 Slide 4/12 A Guide to Advanced Java - Module 1 - Thread 30/07/2008 Creang Threads • Sub classing Thread: – Creang subclass – Overriding. - Thread 30/07/2008 Thread states Slide 6/12 A Guide to Advanced Java - Module 1 - Thread 30/07/2008 Methods of the Thread class • The currentThread() method returns the thread running. • The