Tải bản đầy đủ (.pdf) (21 trang)

welcome to nguyenhuuthe sites

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 (968.44 KB, 21 trang )

<span class='text_page_counter'>(1)</span><div class='page_container' data-page=1>

<b>THREAD</b>



<b>Nguyễn Hữu Thể</b>


</div>
<span class='text_page_counter'>(2)</span><div class='page_container' data-page=2>

<b>THREAD </b>



<b>Hệ điều hành đa nhiệm cổ điển:</b>


 Đơn vị cơ bản sử dụng CPU là quá trình.


 Mỗi quá trình có một khơng gian địa chỉ và một khơng gian trạng
thái riêng


<b>Hệ điều hành đa nhiệm hiện đại, hỗ trợ Thread:</b>


 Đơn vị cơ bản sử dụng CPU là Thread.


 Mỗi q trình có một khơng gian địa chỉ và nhiều Thread điều khiển.
 Mỗi Thread có bộ đếm chương trình, trạng thái các thanh ghi và


ngăn xếp riêng.


 Thread của một q trình có thể chia sẻ nhau khơng gian địa chỉ :
Biến tồn cục, tập tin, chương trình con, hiệu báo, . . .


</div>
<span class='text_page_counter'>(3)</span><div class='page_container' data-page=3>

<b>Multi-Thread </b>



</div>
<span class='text_page_counter'>(4)</span><div class='page_container' data-page=4>

<b>Multi-Thread (2)</b>


<b>Ví dụ: </b>


</div>
<span class='text_page_counter'>(5)</span><div class='page_container' data-page=5>

<b>Multi-Thread (3)</b>




<b>Thread trong Java là một đối tượng của lớp java.lang.Thread</b>


Một chương trình cài đặt Thread bằng cách tạo ra các lớp con của lớp
Thread.


Lớp Thread có 3 phương thức cơ bản:


public static synchronized void

<b>start</b>

() :



• Chuẩn bị mọi thứ cần thiết để thực hiện Thread.


public void

<b>run</b>

():



• Chứa mã lệnh thực hiện cơng việc thực sự của Thread.
• run() được gọi một cách tự động bởi start().


public void

<b>stop</b>

()

: kết thúc một Thread.


Thread kết thúc khi:


 Hoặc tất cả các lệnh trong run() đã được thực thi.


</div>
<span class='text_page_counter'>(6)</span><div class='page_container' data-page=6>

<b>Multi-Thread (4)</b>



Để lập trình đa tiến trình, lớp dẫn xuất từ lớp Thread được


<b>cài đặt sau đó override phương thức run(). </b>



</div>
<span class='text_page_counter'>(7)</span><div class='page_container' data-page=7>

<b>Quản lý Thread</b>




Quản lý thread gồm: tạo thread, chỉnh độ ưu tiên, chạy thread, và


dừng thread.



Tạo Thread: tạo class kế thừa từ class Thread có sẵn rồi override


<b>phương thức run(). </b>



Khi thread được kích hoạt, nó sẽ chạy các lệnh trong phương


<b>thức run() này. </b>



<b>Do đó, ta dùng các constructor của lớp dẫn xuất chứ không </b>


dùng các constructor của thread một cách trực tiếp.



</div>
<span class='text_page_counter'>(8)</span><div class='page_container' data-page=8>

<b>Thread - constructor </b>



Các constructor:



Thread() Tạo Thread với giá trị mặc định
Thread(String name) Tạo Thread với tên cho trước


Thread(Runnable target) Tạo thread liên kết với đối tượng Runnable
Thread(Runnable target, String


name)


</div>
<span class='text_page_counter'>(9)</span><div class='page_container' data-page=9>

<b>Thread – Cấu trúc chương trình</b>



class MyThread extends Thread // Kế thừa lớp Thread


{



// ...


public void run() // Override phương thức run


{


// ...


}


// ...


}


Tạo đối tượng Thread và gọi thực thi:



MyThread t = new MyThread(); // Tạo tiểu trình


t.start(); // Gọi thực thi tiểu trình


</div>
<span class='text_page_counter'>(10)</span><div class='page_container' data-page=10>

<b>Ex 1: Lớp MyThread cơ bản</b>



<b>class</b> MyThread <b>extends</b> Thread { // Kế thừa lớp Thread


<b>public void</b> run() // Override phương thức run


{


<b>int</b> i = 0;



<b>while</b> (i < 10) {


System.<i>out</i>.println(getName() + " " + i);
i++;


}
}


<b>public static void</b> main(String[] args) {


MyThread t1 = <b>new</b> MyThread(); // Tạo Thread


</div>
<span class='text_page_counter'>(11)</span><div class='page_container' data-page=11></div>
<span class='text_page_counter'>(12)</span><div class='page_container' data-page=12>

<b>Ex 2: MyThread2</b>



Các thuộc tính



name: tên của thread



n: số lần thread xuất hiện ra màn hình



Các phương thức:



MyThread2(String name, int n):



• Là phương thức khởi tạo, có nhiệm vụ gán giá trị cho 2 thuộc tính và
gọi phương thức start() để cho thread hoạt động (start() tự động gọi
run())


run()




• In n lần dịng thơng báo ra màn hình rồi kết thúc thread.


</div>
<span class='text_page_counter'>(13)</span><div class='page_container' data-page=13>

<b>Ex 2: MyThread2 (2)</b>



<b>13</b>


<b>public class</b> MyThread2 <b>extends</b> Thread {
String name;


<b>int</b> n;


MyThread2(String name, <b>int</b> n) {


<b>this</b>.name = name;


<b>this</b>.n = n;


System.<i>out</i>.println("Thread " + name + " has been created.");


start();
}


<b>public void</b> run() {


<b>for</b> (<b>int</b> i = 0; i < n; i++) {


System.<i>out</i>.println("Hello, I'm " + name);


System.<i>out</i>.println(" I go to bed now, bye bye.");
}



}


<b>public static void</b> main(String args[]) {


<b>int</b> n = 10;


<b>int</b> numThread = 4;


<b>for</b> (<b>int</b> i = 0; i < numThread; i++) {


MyThread2 t = <b>new</b> MyThread2("Thread" + i, n);
}


</div>
<span class='text_page_counter'>(14)</span><div class='page_container' data-page=14>

<b>Ex 2: MyThread2 (3) – Kết quả</b>


Thread Thread0 has been created ....!
Thread Thread1 has been created ....!
Hello, I'm Thread0


I go to bed now, bye bye...


Thread Thread2 has been created ....!
Hello, I'm Thread1


I go to bed now, bye bye...
Hello, I'm Thread0


I go to bed now, bye bye...
Hello, I'm Thread1



I go to bed now, bye bye...


Thread Thread3 has been created ....!
Hello, I'm Thread1


I go to bed now, bye bye...
Hello, I'm Thread0


I go to bed now, bye bye...
Hello, I'm Thread2


I go to bed now, bye bye...


Hello, I'm Thread1


I go to bed now, bye bye...
Hello, I'm Thread3


I go to bed now, bye bye...
Hello, I'm Thread0


I go to bed now, bye bye...
Hello, I'm Thread3


I go to bed now, bye bye...
Hello, I'm Thread2


I go to bed now, bye bye...
Hello, I'm Thread3



I go to bed now, bye bye...
Hello, I'm Thread0


I go to bed now, bye bye...
Hello, I'm Thread1


I go to bed now, bye bye...
Hello, I'm Thread0


</div>
<span class='text_page_counter'>(15)</span><div class='page_container' data-page=15>

<b>Các phương thức khác</b>



o

<b>public static void sleep(long milliseconds) throws </b>


InterruptedException



o Làm cho Thread bị nghẽn (Blocked) một khoảng thời gian mili giây xác
định.


o

<b>public final void suspend() </b>



o Chuyển Thread từ trạng thái sẳn sàng sang trạng thái nghẽn.


o

<b>public final void resume()</b>



o Chuyển Thread từ trạng thái nghẽn sang trạng thái sẵn sàng.


o

<b>public final void yield()</b>



o Chuyển Thread từ trạng thái đang chạy sang trạng thái sẵn sàng.


</div>
<span class='text_page_counter'>(16)</span><div class='page_container' data-page=16>

<b>Độ ưu tiên</b>




o Trong Java, thread có 10 mức ưu tiên, đánh số từ 1 (độ ưu tiên


thấp nhất) tới 10 (độ ưu tiên cao nhất).



o Khi thread được tạo ra, nó sẽ có độ ưu tiên mặc định là 5. Có thể


chỉnh lại độ ưu tiên của thread bằng phương thức



void

<b>setPriority(</b>

int

newPriority)


 Ví

dụ:



t.setPriority(8);



Lưu ý:



</div>
<span class='text_page_counter'>(17)</span><div class='page_container' data-page=17>

<b>Thực thi thread</b>



<b>Để thực thi thread, ta gọi phương thức void start(). </b>



Khi được thực thi, thread sẽ chạy các lệnh trong phương thức


<b>run().</b>



Ví dụ:



t.start();



Lưu ý:



Nếu thread đã start() rồi, việc gọi start() lần nữa sẽ bị phát sinh


exception IllegalThreadStateException.




Để start() lại thread, ta cần tạo mới thread.



</div>
<span class='text_page_counter'>(18)</span><div class='page_container' data-page=18>

<b>Dừng thread</b>



Để dừng thread đang chạy, ta dùng phương thức void interrupt().


t.interrupt();



</div>
<span class='text_page_counter'>(19)</span><div class='page_container' data-page=19>

<b>Interface Runnable</b>



<b>Có thể tạo class implement interface Runnable. </b>



<b>Class phải hiện thực phương thức void run() của interface này.</b>


Tạo thread liên kết với Runnable thông qua constructor tương



ứng của lớp Thread.



Khi thread được start, nó sẽ gọi thực thi lệnh trong phương


<b>thức run() của Runnable liên kết với nó.</b>



</div>
<span class='text_page_counter'>(20)</span><div class='page_container' data-page=20>

<b>Runnable - EX 1</b>



public class HelloRunnable implements Runnable {
public void run() {


System.<i>out</i>.println("Hello from a thread!");
}


public static void main(String args[]) {


(new Thread(new HelloRunnable())).start();


}


</div>
<span class='text_page_counter'>(21)</span><div class='page_container' data-page=21>

<b>21</b>


<b>Runnable - EX 2</b>


<b>public class</b> <b>RunnableThread implements</b> <b>Runnable {</b>
<b>Thread runner;</b>


<b>public</b> <b>RunnableThread() {</b>
<b>}</b>


<b>public</b> <b>RunnableThread(String threadName) {</b>


<b>runner</b> <b>= new</b> <b>Thread(this, threadName);//(1)Create a new thread.</b>


<b>System.</b><i><b>out</b></i><b>.println(runner.getName());</b>


<b>runner.start(); // (2) Start the thread.</b>


<b>}</b>


<b>public void</b> <b>run() { // Display info about this particular thread</b>


<b>System.</b><i><b>out</b><b>.println(Thread.currentThread());</b></i>
<b>}</b>


<b>public static void</b> <b>main(String[] args) {</b>


<b>Thread thread1 = new</b> <b>Thread(new</b> <b>RunnableThread(), "thread1");</b>


<b>Thread thread2 = new</b> <b>Thread(new</b> <b>RunnableThread(), "thread2");</b>
<b>RunnableThread thread3 = new</b> <b>RunnableThread("thread3");</b>


<b>thread1.start(); </b>
<b>thread2.start();</b>


<b>try</b> <b>{</b> <b>// delay for one second</b>


<i><b>Thread.currentThread().sleep(1000);</b></i>
<b>} catch</b> <b>(InterruptedException e) {</b>


<b>}</b>


<b>// Display info about the main thread</b>


</div>

<!--links-->

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×