1 / 26

Thread

Thread. boonrit@feu.ac.th. Thread. ส่วนของ process ที่ให้ CPU ประมวลผล. Creating and Running Threads in Java. สร้าง instance ของ Thread object เรียก start () method ในการสั่งให้ thread ทำงาน Thread myThread = new Thread(); myThread.start();. การสร้าง Thread โดยวิธีการ.

lecea
Download Presentation

Thread

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Thread boonrit@feu.ac.th

  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 ผลลัพธ์ 11111111112222222222333333333344444444445555555555

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

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

  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. Sleepy 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. wait() notify() notifyAll() • wait() เป็นการสั้งให้ thread หยุดทำงานไปอยู่ในสภาวะ block • notify() ในการให้ THREAD กลับเข้ามาทำงานอีกครั้ง

  18. 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; • }

  19. 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);

  20. Stop thread • // Create and start the thread • MyThread thread = new MyThread(); • thread.start(); • // Do work... • // Stop the thread • thread.allDone = true; • class MyThread extends Thread { • boolean allDone = false; • // This method is called when the thread runs • public void run() { • while (true) { • // Do work... • if (allDone) { • return; • } • // Do work... • } • } • }

  21. Waiting for a Thread to Finish • myThread.join(); class testJoninNew{ public static void main(String[] argv){ Socket connection = server.accept( ); ………. Thread input = new InputThread(connection.getInputStream( )); input.start( ); Thread output = new OutputThread(connection.getOutputStream( )); output.start( ); // wait for output and input to finish try { input.join( ); output.join( ); } catch (InterruptedException ex) { } } }

  22. Synchronization Why Synchronization public class Maze { 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 maze 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.

  23. 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!

  24. Synchronization • public class Maze { • 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; • } • }

  25. 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) { • playerX = x; • playerY = y; • } • }

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

More Related