National Taiwan University Department of Computer Science and

  • Slides: 24
Download presentation
National Taiwan University Department of Computer Science and Information Engineering Multithread 多執行緒 以GUI為例了解物件以及Event Lecturer:楊昌樺

National Taiwan University Department of Computer Science and Information Engineering Multithread 多執行緒 以GUI為例了解物件以及Event Lecturer:楊昌樺

National Taiwan University Department of Computer Science and Information Engineering Important Features of Java

National Taiwan University Department of Computer Science and Information Engineering Important Features of Java 何謂多 (Multi-tasking) 例如,撰寫網路程式,模擬程式。 Java 利用 “執行緒” (Thread) 來實作多 可將一個Process分成數份, 讓各部份能同時被執行。

National Taiwan University Department of Computer Science and Information Engineering 執行緒的生命週期 start() notify(), I/O

National Taiwan University Department of Computer Science and Information Engineering 執行緒的生命週期 start() notify(), I/O unblock Blocked Runnable sleep(), wait(), , I/O block Running yield()

National Taiwan University Department of Computer Science and Information Engineering Thread類別 java. lang. Thread

National Taiwan University Department of Computer Science and Information Engineering Thread類別 java. lang. Thread public class Thread extends Object implements Runnable 提供的基本方法 static yield() 讓目前running的暫停, 讓runnable的擇一跑 static sleep() 讓目前running的睡一個設定的時間 start() 啟動, 之後JVM可啟動該thread的run() set. Priority() • set. Priority(MAX_PRIORITY):給最大優先權 • set. Priority(MIN_PRIORITY):給最小優先權 • set. Priority(NORM_PRIORITY):預設的優先權

National Taiwan University Department of Computer Science and Information Engineering 簡單程式範例 (My. Thread. java)

National Taiwan University Department of Computer Science and Information Engineering 簡單程式範例 (My. Thread. java) public class My. Thread extends Thread{ class Test. Thread My. Thread(String n) { { public static void main(String[] args) super(n); { } My. Thread t 1 = new My. Thread("t 1"); public void run() My. Thread t 2 = new My. Thread("t 2"); { t 1. start(); String tname = super. get. Name(); t 2. start(); try } { } for(int i = 1; i<=5000; i++) { System. out. println(tname+" "+i+" "+Math. random()); } } thread t 1, t 2 各執行5000次迴圈,在各個 catch(Exception e) 電腦產生的執行順序也會因為各個電腦 { 當時的CPU效能而有所不同 System. out. println("error"); } } }

National Taiwan University Department of Computer Science and Information Engineering 不能多重繼承時 (My. Thread 2.

National Taiwan University Department of Computer Science and Information Engineering 不能多重繼承時 (My. Thread 2. java) public class My. Thread 2 implements Runnable { int i; public void run() { i = 0; while (true) { System. out. println(Thread. current. Thread(). get. Name() + " " + (++i) + " " +Math. random()); class Test. Thread 2 if ( i == 5000 ) break; { } public static void main(String args[]) } { } My. Thread 2 r = new My. Thread 2(); Thread t 1 = new Thread(r); 先宣告一個空的 Thread t 2 = new Thread(r); t 1. set. Name(“t 1"); My. Thread 2類別物件r t 2. set. Name(“t 2"); 建立t 1, t 2 執行緒物件實 t 1. start(); t 2. start(); 體,其對象為 My. Thread 2中的run()方法 } }

National Taiwan University Department of Computer Science and Information Engineering 如果執行緒間彼此有同步的考量 (My. Thread 3.

National Taiwan University Department of Computer Science and Information Engineering 如果執行緒間彼此有同步的考量 (My. Thread 3. java) public class My. Thread 3 implements Runnable{ static int max=0; public void run() { for(int i = 1; i<=5000; i++) { int tmp=max; System. out. println( Thread. current. Thread(). get. Name() + " " + ++tmp ); max=tmp; } } } 各把max拿來加 5000次 最後卻不一定等於 10000 而且每次都不一樣, why? class Test. Thread 3 { public static void main(String args[]) { My. Thread 3 r = new My. Thread 3(); Thread t 1 = new Thread(r); Thread t 2 = new Thread(r); t 1. set. Name("t 1"); t 2. set. Name("t 2"); t 1. start(); t 2. start(); } }

National Taiwan University Department of Computer Science and Information Engineering 方法加 synchronized public class

National Taiwan University Department of Computer Science and Information Engineering 方法加 synchronized public class My. Thread 4 implements Runnable{ static int max=0; public void run() { for(int i = 1; i<=5000; i++) add. Max(); } private synchronized void add. Max() { int tmp=max; System. out. println(Thread. current. Thread(). get. Name() + " " + ++tmp); max=tmp; } 保證 } add. Max()這個方法 只有同時間只有一個 process可以執行

National Taiwan University Department of Computer Science and Information Engineering Events 以物件來表示 所有的事件都是 Event.

National Taiwan University Department of Computer Science and Information Engineering Events 以物件來表示 所有的事件都是 Event. Object的子類別 n所有的訊息都包含在java. awt. event類別庫內

National Taiwan University Department of Computer Science and Information Engineering 以GUI為例了解物件 Hello. Window. java

National Taiwan University Department of Computer Science and Information Engineering 以GUI為例了解物件 Hello. Window. java JFrame frame = new JFrame(“Hello. Window”); Container cp = frame. get. Content. Pane(); cp. set. Layout(new Flow. Layout(Flow. Layout. LEFT)); //建立視窗框架 //獲得內容面板 //設定版面配置 JText. Field text. A = new JText. Field(20); JButton button 1 = new JButton(“hi”); //宣告文字元件 //宣告按鈕元件 cp. add(text. A); cp. add(button 1); //將元件加入面板 frame. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); //設定關閉動作 frame. pack(); //調整視窗大小 frame. set. Visible(true); //顯示視窗

National Taiwan University Department of Computer Science and Information Engineering 以GUI範例了解物件以及Event 以My. GUI了解Event (My.

National Taiwan University Department of Computer Science and Information Engineering 以GUI範例了解物件以及Event 以My. GUI了解Event (My. GUI. java) 作加法 public My. GUI() //My. GUI. java { button. Plus. add. Action. Listener(new Action. Listener() { public void action. Performed(Action. Event e) { int var. A = Integer. parse. Int(text. A. get. Text()); int var. B = Integer. parse. Int(text. B. get. Text()); Integer var. C = new Integer(var. A+var. B); text. C. set. Text(var. C. to. String()); } }); }

National Taiwan University Department of Computer Science and Information Engineering 委派事件模型 1. 事先有註冊 按鈕

National Taiwan University Department of Computer Science and Information Engineering 委派事件模型 1. 事先有註冊 按鈕 button. Plus 2. 按下按鈕產生一個 Event物件傳給action. Listner action. Listener 3. 根據物件的種類 指派給事件處理者 action. Performed

National Taiwan University Department of Computer Science and Information Engineering 系統實際運作狀況 當事件發生時,會有一個事件ID產生 GUI元件使用這個ID碼,呼叫對應的事件方法 假如收到有Action.

National Taiwan University Department of Computer Science and Information Engineering 系統實際運作狀況 當事件發生時,會有一個事件ID產生 GUI元件使用這個ID碼,呼叫對應的事件方法 假如收到有Action. Event這種物件規格 從全部已註冊的Action. Listeners中,選出欲呼叫的 action. Performed() 方法

National Taiwan University Department of Computer Science and Information Engineering 另一個版本 class My. Listener

National Taiwan University Department of Computer Science and Information Engineering 另一個版本 class My. Listener implements Action. Listener //介面 { public void action. Performed(Action. Event e) //實現這個介面一定要 { //實作action. Performed int var. A = Integer. parse. Int(text. A. get. Text()); int var. B = Integer. parse. Int(text. B. get. Text()); Integer var. C = new Integer(var. A+var. B); text. C. set. Text(var. C. to. String()); } } public My. GUI 2() // My. GUI 2. java { My. Listener listener = new My. Listener(); // button. Plus. add. Action. Listener(listener); }

National Taiwan University Department of Computer Science and Information Engineering Event的註冊 Event 產生時,只會通知有註冊過的Listener。所以對必須要 先把Event『註冊』給要負責處理的Listner

National Taiwan University Department of Computer Science and Information Engineering Event的註冊 Event 產生時,只會通知有註冊過的Listener。所以對必須要 先把Event『註冊』給要負責處理的Listner 註冊所有想要擷取的事件,而當使用者啟動的事件並不 是我們所想要的事件時,就不加以理會 程式上以XX. add. XXListener 來完成註冊 button. add. Action. Listener(new Action. Listener() … 一個event source 可以被好幾個listener 所註冊,同樣地, 一個listener 也可以註冊好幾個event source 所有的Event Listener 都是一種interface,裡面只有定義這個 Listener所提供的抽象method 必須去實作出此listener interface 內所有的method

National Taiwan University Department of Computer Science and Information Engineering Action. Listener 都是Event. Listener的子類別

National Taiwan University Department of Computer Science and Information Engineering Action. Listener 都是Event. Listener的子類別 Action Type Action. Listener Component. Event Component. Listener Focus. Event Focus. Listener Key. Event Key. Listener Container. Event Container. Listener Window. Event Window. Listener Item. Event Item. Listener Adjust. Event Adjust. Listener Text. Event Text. Listener Action. Event Action. Listener

National Taiwan University Department of Computer Science and Information Engineering 處理的方法 Button Check. Box

National Taiwan University Department of Computer Science and Information Engineering 處理的方法 Button Check. Box Component

National Taiwan University Department of Computer Science and Information Engineering Your Turn 實作出My. Calc小算盤

National Taiwan University Department of Computer Science and Information Engineering Your Turn 實作出My. Calc小算盤 (利用My. Calc. java半成品改良) Form已經建好了 完成計算機功能 @代表 00 S取平方根 % 例如 50*10% 5