1. Trang chủ
  2. » Giáo án - Bài giảng

Giới thiệu về Thread trong java

13 373 1

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

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

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Creating Threads• Sub classing Thread: – Creating subclass – Overriding the run method – Starting the thread • Using the Runnable interface: – Implementing the Runnable interface – Impl

Trang 1

Slide 1/12

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

Module 1 Introduction to Thread

• Objective

Trang 2

Define Threads

Trang 3

Slide 3/12

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

Trang 4

Creating Threads

• Sub classing Thread:

– Creating subclass

– Overriding the run()

method – Starting the thread

• Using the Runnable interface:

– Implementing the Runnable interface – Implementing the run() method

– Starting the thread

Trang 5

Slide 5/12

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

Thread states

Trang 6

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 instructions, which are

executed when the start() method is invoked

Trang 7

Slide 7/12

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

Methods of the Thread class …

Trang 8

Methods of the Thread class …

Trang 9

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

Trang 10

Daemon Threads

• setDaemon() method

• isDaemon() method

Trang 11

Slide 11/12

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

Needs for Daemon Thread

Trang 12

Summary

Trang 13

Slide 13/12

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

LAB & Exercises

LAB

1) Extending the Thread class 2) Implementing the Runnable 3) Creating the Thread objects 4) Using sleep() and getName() methods 5) Displaying Thread class method descriptions 6) Setting and getting Thread priorities

Exercises

 Write a thread displaying an electronic clock

Ngày đăng: 30/04/2015, 07:00

TỪ KHÓA LIÊN QUAN

w