Tải bản đầy đủ (.ppt) (13 trang)

Giới thiệu về Thread trong java

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (995.24 KB, 13 trang )

Slide 1/12
A Guide to Advanced Java - Module 1 - Thread
30/07/2008
Module 1
Introduction to Thread

Objecve
Slide 2/12
A Guide to Advanced Java - Module 1 - Thread
30/07/2008
Dene 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
Creang Threads

Sub classing Thread:

Creang subclass

Overriding the run()
method

Starng the thread

Using the Runnable
interface:


Implemenng the
Runnable interface

Implemenng the run()
method

Starng 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 instrucons, 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
Slide 11/12
A Guide to Advanced Java - Module 1 - Thread
30/07/2008
Needs for Daemon Thread
Slide 12/12
A Guide to Advanced Java - Module 1 - Thread
30/07/2008
Summary
Slide 13/12
A Guide to Advanced Java - Module 1 - Thread
30/07/2008
LAB & Exercises

LAB
1) Extending the Thread class
2) Implemenng the Runnable
3) Creang the Thread objects
4) Using sleep() and getName() methods
5) Displaying Thread class method descripons
6) Se<ng and ge<ng Thread priories


Exercises

Write a thread displaying an electronic clock

×