JAVA THREAD OUTLINE What is a thread Create

  • Slides: 41
Download presentation
JAVA THREAD

JAVA THREAD

OUTLINE What is a thread? Create JAVA threads Start a thread Thread life cycle

OUTLINE What is a thread? Create JAVA threads Start a thread Thread life cycle Control threads thread synchronization

SINGLE-THREAD V. S. MULTITHREAD Single-thread multithread main thread sub main sub

SINGLE-THREAD V. S. MULTITHREAD Single-thread multithread main thread sub main sub

THREADS Started by Java from main(String[]) Started by main thread Start by B thread

THREADS Started by Java from main(String[]) Started by main thread Start by B thread ←start ←end C A 同一時間只有一thread執行 Main program thread B

CREATE JAVA THREADS Two ways: 1. Extend the Thread class java. lang. Thread 2.

CREATE JAVA THREADS Two ways: 1. Extend the Thread class java. lang. Thread 2. 實作Runnable介面 package java. lang; public interface Runnable{ public void run() }

1. EXTEND THE THREAD CLASS Java. lang. Thread已實作Runnable介面 Step: 1. 製作一個subclass繼承Thread 2. override run()

1. EXTEND THE THREAD CLASS Java. lang. Thread已實作Runnable介面 Step: 1. 製作一個subclass繼承Thread 2. override run() method run()是一個thread的進入點

1. EXTEND THE THREAD CLASS 用start()開始這個 thread,而非直接 呼叫run()

1. EXTEND THE THREAD CLASS 用start()開始這個 thread,而非直接 呼叫run()

1. EXTEND THE THREAD CLASS 每次執行結果可能 會不一樣

1. EXTEND THE THREAD CLASS 每次執行結果可能 會不一樣

2. 實作RUNNABLE介面

2. 實作RUNNABLE介面

THREAD LIFE CYCLE Runnable start() Not running (ready) New thread notify. All interrupt Blocked/waiting

THREAD LIFE CYCLE Runnable start() Not running (ready) New thread notify. All interrupt Blocked/waiting CPU排程 running Dead run()執行結束 sleep wait for I/O

SETPRIORITY(INT) 可以設定優先度 優先度越高,會越先被選中而執行 public static final int MIN_PRIORITY public static final int NORM_PRIORITY(預設) public

SETPRIORITY(INT) 可以設定優先度 優先度越高,會越先被選中而執行 public static final int MIN_PRIORITY public static final int NORM_PRIORITY(預設) public static final int MAX_PRIORITY 或輸入 1~10

BLOCKED/WAITING狀態 讓thread進入Blocked/Waiting狀態: 1. sleep() Runnable 2. wait() start() New thread 3. 等待I/O 4. join()

BLOCKED/WAITING狀態 讓thread進入Blocked/Waiting狀態: 1. sleep() Runnable 2. wait() start() New thread 3. 等待I/O 4. join() Not running (ready) notify. All interrupt Blocked/waiting CPU排程 Dead running run()執行結束 sleep wait for I/O

SLEEP() public static void sleep(long millis) throws Interrupted. Exception 給予一個時間,此thread就會休息一段時間,進入 blocked狀態,等到時間到了以後,便會回到 runnable(ready)狀態

SLEEP() public static void sleep(long millis) throws Interrupted. Exception 給予一個時間,此thread就會休息一段時間,進入 blocked狀態,等到時間到了以後,便會回到 runnable(ready)狀態

SLEEP() try { Thread. sleep(1000); //睡 1000毫秒 } catch (Interrupted. Exception e) { //sleep被打斷

SLEEP() try { Thread. sleep(1000); //睡 1000毫秒 } catch (Interrupted. Exception e) { //sleep被打斷 }

JOIN() Public final void join() throws Interrupted Exception 當有另一個thread使用join()時,現正執行的thread會 進入blocked狀態,必須等到加入的thread執行完,才 會繼續執行

JOIN() Public final void join() throws Interrupted Exception 當有另一個thread使用join()時,現正執行的thread會 進入blocked狀態,必須等到加入的thread執行完,才 會繼續執行

JOIN()

JOIN()

YEILD() public static void yield() 使執行中的thread暫時回到ready狀態,暫時讓出執行 權,以免一個thread佔用太多時間 Runnable start() New thread Not running (ready)

YEILD() public static void yield() 使執行中的thread暫時回到ready狀態,暫時讓出執行 權,以免一個thread佔用太多時間 Runnable start() New thread Not running (ready) notify. All interrupt Blocked/waiting CPU排程 Dead running run()執行結束 sleep wait for I/O

RUNNABLE 讓thread回到runnable(ready)狀態: 1. notify() Runnable 2. notify. All() start() New thread 3. interrupt() Not

RUNNABLE 讓thread回到runnable(ready)狀態: 1. notify() Runnable 2. notify. All() start() New thread 3. interrupt() Not running (ready) notify. All interrupt Blocked/waiting CPU排程 Dead running run()執行結束 sleep wait for I/O

NOTIFY(), NOTIFYALL() 當notify()被呼叫時,他會從blocked/waiting區中的所 有thread中隨機挑選出一個回到runnable的狀態,等待 被執行 notify. All()則可以讓所有在blocked/waiting區塊中等待 的所有thread都回到runnable去等待執行

NOTIFY(), NOTIFYALL() 當notify()被呼叫時,他會從blocked/waiting區中的所 有thread中隨機挑選出一個回到runnable的狀態,等待 被執行 notify. All()則可以讓所有在blocked/waiting區塊中等待 的所有thread都回到runnable去等待執行

INTERRUPT() try { Thread. sleep(1000); //睡 1000毫秒 } catch (Interrupted. Exception e) { //sleep被打斷

INTERRUPT() try { Thread. sleep(1000); //睡 1000毫秒 } catch (Interrupted. Exception e) { //sleep被打斷 } 原先進入sleep狀態的thread 執行Thread. interrupt()後便會被中斷,並且丟出例外

THREAD SYNCHRONIZATION 兩人同時要存 500元進入同一個原本 0元的帳戶 void add(int amount) { int x = account. get

THREAD SYNCHRONIZATION 兩人同時要存 500元進入同一個原本 0元的帳戶 void add(int amount) { int x = account. get (); x += amount; account. set (x); } add(500) : // A存入 500 x = account. get() //1 x += 50; //2 account. set(x) //3 add(500) : // B存入 500 x = account. get() //4 x += 50; //5 account. set(x) //6 若由cpu排程執行順序: 1 4 2 5 3 6 應該最後帳戶裡要有1000元 然而因為沒有同步,造成帳戶裡只有500元

SYNCHRONIZED 1. 在方法前面加修飾字Synchronized public void synchronized add(int amount){ int x = account. get ();

SYNCHRONIZED 1. 在方法前面加修飾字Synchronized public void synchronized add(int amount){ int x = account. get (); x += amount; account. set (x); } 一次只有一個thread可以執行這個物件的方法,取得lock的 thread得以執行完整個方法而不被別的thread打擾

SYNCHRONIZED 2. 在程式區塊前加sychronized(this) public void add(int amount){ synchronized(this){ int x = account. get ();

SYNCHRONIZED 2. 在程式區塊前加sychronized(this) public void add(int amount){ synchronized(this){ int x = account. get (); x += amount; account. set (x); } } 取得lock的thread在執行被synchronized(this){}包住的區塊時, 其他thread暫時無法取得lock,須等到此區塊執行完

WAIT() , NOTIFY() public final void wait() throws Interrupted. Exception 讓正在執行的thread進入wait set,並釋放物件的lock, 直到被notify()叫回到runnable狀態 public

WAIT() , NOTIFY() public final void wait() throws Interrupted. Exception 讓正在執行的thread進入wait set,並釋放物件的lock, 直到被notify()叫回到runnable狀態 public final void notify() 隨機選取一個在wait set的thread,被選到的thread可 以與其他thread爭取lock

PRODUCER

PRODUCER

CONSUMER

CONSUMER

CLERK

CLERK

CLERK

CLERK

PRODUCER&CONSUMER

PRODUCER&CONSUMER

THREAD GROUP 建立thread group Thread. Group thread. Group 1 = new Thread. Group("group 1");

THREAD GROUP 建立thread group Thread. Group thread. Group 1 = new Thread. Group("group 1"); 將thread指定為某個thread group Thread thread 1 = new Thread(thread. Group 1, "group 1's member"); 用處: 某些方法可以一次對group內所有threads產生影響 (ex. interrupt()一次interrupt group內所有threads set. Max. Priority()將group內所有threads的優先權設成 最大)