งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Thread boonrit@feu.ac.th.

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Thread boonrit@feu.ac.th."— ใบสำเนางานนำเสนอ:

1 Thread

2 Thread ส่วนของ process ที่ให้ CPU ประมวลผล

3 Creating and Running Threads in Java
สร้าง instance ของ Thread object เรียก start() method ในการสั่งให้ thread ทำงาน Thread myThread = new Thread(); myThread.start();

4 การสร้าง Thread โดยวิธีการ
สืบทอดมาจาก Thread class ทำการ override run() method public class MyThread extends Thread { public void run() { System.out.println("Do something cool here."); } Thread myThread = new MyThread(); myThread.start();

5 การสร้าง Thread โดยวิธีการ สืบทอดมาจาก Thread class
public class NumberThread extends Thread { int num; public NumberThread(int n) { num = n; } public void run() { for (int k=0; k < 10; k++) { System.out.print(num); } // for } // run() } // NumberThread class

6 การสร้าง Thread โดยวิธีการ สืบทอดมาจาก Thread class
public class Numbers { public static void main(String args[]) { // 5 threads NumberThread number1, number2, number3, number4, number5; // Create and start each thread number1 = new NumberThread(1); number1.start(); number2 = new NumberThread(2); number2.start(); number3 = new NumberThread(3); number3.start(); number4 = new NumberThread(4); number4.start(); number5 = new NumberThread(5); number5.start(); } // main() } // Numbers class ผลลัพธ์

7 การสร้าง Thread โดยวิธีการ สืบทอดมาจาก Thread class

8 การสร้าง Thread โดยวิธีการ สืบทอดมาจาก Thread class

9 การสร้าง Thread โดยวิธีการ Implementing the Runnable Interface
public class MyClass extends SomeOtherClass implements Runnable { public MyClass() { Thread thread = new Thread(this); thread.start(); } public void run() { System.out.println("Do something cool here.");

10 การสร้าง Thread โดยวิธีการ Implementing the Runnable Interface

11 การสร้าง Thread โดยวิธีการ Implementing the Runnable Interface
public class NumberPrinter implements Runnable { int num; public NumberPrinter(int n) { num = n; } public void run() { for (int k=0; k < 10; k++) System.out.print(num); } // run() } // NumberPrinter class

12 การสร้าง Thread โดยวิธีการ Implementing the Runnable Interface
public class Numbers { public static void main(String args[]) { Thread number1, number2, number3, number4, number5; // Create and start each thread number1 = new Thread(new NumberPrinter(1)); number1.start(); number2 = new Thread(new NumberPrinter(2)); number2.start(); number3 = new Thread(new NumberPrinter(3)); number3.start(); number4 = new Thread(new NumberPrinter(4)); number4.start(); number5 = new Thread(new NumberPrinter(5)); number5.start(); } // main() } // Numbers class

13 Thread Priority ใช้ setPriority(n)
เป็นการกำหนดลำดับความสำคัญของ Thread โดยที่ Thread ที่มีการกำหนด Priority สูงจะมีโอกาสที่จะได้รับการประมวลผลก่อน และนานกว่า Thread ที่มี Priority ต่ำกว่า n มีค่าระหว่าง 0-10 public NumberThread(int n) { num = n; setPriority(n); }

14 Sleep Threads เป็นการให้ Thread หยุดรอในเวลาที่กำหนด โดยที่ หน่วยเป็น millisecond Thread.sleep(1000); //หยุด 1 วินาที try { sleep(100); } catch (InterruptedException e) { System.out.println(e.getMessage()); }

15 Sleepy Threads public void run() { for (int k=0; k < 10; k++) {
try { Thread.sleep((long)(Math.random() * 1000)); } catch (InterruptedException e) { System.out.println(e.getMessage()); } System.out.print(num); } // for } // run()

16 Thread States and Life Cycle

17 suspend() and resume()
suspend() สั่งให้ thread หยุดทำงานและเข้าสู่สถานะ blocked resume() ให้ thread เข้าสู่สถานะ ready

18 Suspend() and resume()

19 Suspend() and resume()
public void test() { MyThread_subpend t1=new MyThread_subpend("A"); MyThread_subpend t2=new MyThread_subpend("B"); t1.start(); t2.start(); try{ Thread.sleep(200); t1.suspend(); Thread.sleep(1000); t1.resume(); }catch(InterruptedException e){} } ABBABBBBBBBBBBABABABABABABABABAAAAAAAAAA

20 join() หยุดรอ Thread ลูกให้ทำงานจบก่อน ผลลัพธ์ public class Numbers {
public static void main(String args[]) { // 5 threads NumberThread number1, number2, number3, number4, number5; // Create and start each thread number1 = new NumberThread(1); number1.start(); number2 = new NumberThread(2); number2.start(); try{ number1.join();number2.join(); }catch(InteruptedException e{} System.out.print(“\nMain stop”); } // main() } // Numbers class ผลลัพธ์ Main stop

21 stop สั่งให้ Thread หยุดทำงาน public void run() { try{ while(true)
sleep(1000); System.out.println("running"); } }catch(ThreadDeath e){ System.out.print("Killed"); catch(InterruptedException ee){} สั่งให้ Thread หยุดทำงาน

22 stop public void test(){ MyThreadStop t=new MyThreadStop(); t.start();
try { Thread.sleep(4000); } catch (InterruptedException e) { } t.stop(); running Killed

23 Daemon เป็นการกำหนด Thread ลูกให้ถูกทำลายเมื่อ Thread แม่จบการทำงาน

24 Daemon

25 Daemon public static void main(String[] args) {
Main Stop public static void main(String[] args) { MyThreadDeamon t1=new MyThreadDeamon("A"); MyThreadDeamon t2=new MyThreadDeamon("B"); t1.setDaemon(true); t2.setDaemon(true); t1.start(); t2.start(); try { Thread.sleep(4000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.print("Main Stop");

26 Synchronization Why Synchronization public class Game {
private int playerX; private int playerY; public boolean isAtExit() { return (playerX == 0 && playerY == 0); } public void setPosition(int x, int y) { playerX = x; playerY = y; Let's say you're creating a xxxx game. Any thread can set the position of the player, and any thread can check to see if the player is at the exit. For simplicity, let's say the exit is at position x = 0, y = 0.

27 Synchronization Starting off, the object's variables are playerX = 1 and playerY = 0. Thread A calls setPosition(0,1). The line playerX = x; is executed. Now playerX = 0. Thread A is pre-empted by Thread B. Thread B calls isAtExit(). Currently, playerX = 0 and playerY = 0, so isAtExit() returns true!

28 Synchronization public class Game { private int playerX;
private int playerY; public synchronized boolean isAtExit() { return (playerX == 0 && playerY == 0); } public synchronized void setPosition(int x, int y) { playerX = x; playerY = y;

29 Synchronization public synchronized void setPosition(int x, int y) {
playerX = x; playerY = y; } is essentially the same as this: public void setPosition(int x, int y) { synchronized(this) {

30 Synchronization public void myMethod() { synchronized(this) {
// code that needs to be synchronized } // code that is already thread-safe

31 wait() notify() notifyAll()
wait() เป็นการสั้งให้ thread หยุดทำงานไปอยู่ในสภาวะ block notify() ในการให้ THREAD กลับเข้ามาทำงานอีกครั้ง

32 wait() notify() notifyAll()
//not use wait and notify // Thread A public void waitForMessage() { while (hasMessage == false) { Thread.sleep(100); } // Thread B public void setMessage(String message) { ... hasMessage = true;

33 wait() notify() notifyAll()
Here's the updated message code: // Thread A public synchronized void waitForMessage() { try { wait(); } catch (InterruptedException ex) { } // Thread B public synchronized void setMessage(String message) { ... notify(); You can also choose to wait for a maximum amount of time. The wait() method can take a maximum amount of time to wait as a parameter: wait(100);

34 public class DataIO { String data=""; public synchronized void setData(String data) { this.data=data; notify(); } public synchronized void sentData() throws InterruptedException if(data.equals("")) wait(); System.out.print(data); data="";

35 public class OutData extends Thread{
DataIO dataio; public OutData(DataIO dataio) { this.dataio=dataio; } public void run() while(true) try { dataio.sentData(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace();

36 public class MainClass {
/** args */ public static void main(String[] args) { // TODO Auto-generated method stub DataIO dataio=new DataIO(); Thread outdata=new OutData(dataio); outdata.setDaemon(true); outdata.start(); while(true) { String d=JOptionPane.showInputDialog("Data"); if(d.equals("exit")) break; dataio.setData(d); }


ดาวน์โหลด ppt Thread boonrit@feu.ac.th.

งานนำเสนอที่คล้ายกัน


Ads by Google