freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內(nèi)容

java語言程序設(shè)計(異常處理、線程、集合操作)ppt(編輯修改稿)

2024-11-12 16:18 本頁面
 

【文章內(nèi)容簡介】 final String getName() public static void sleep(long millis) throws InterruptedException 37 ? 線程創(chuàng)建的兩種方式 1. “ Subclassing Thread and Overriding run” ?繼承 Thread類 , 重寫 run()方法 2. “ Implementing the Runnable Interface” ?實現(xiàn) Runnable接口 ? 應(yīng)用場合 ? 當(dāng)所定義的類為一個子類時,須利用 Runnable接口 線程的創(chuàng)建 38 第七章 線程 1. 概述 2. 線程的創(chuàng)建 ? 兩種方式 3. 線程的同步 1. synchronized 2. wait()/notifyAll()/notify() 4. 線程的生命周期 39 ? 獨立的 (independent)、異步的(asynchronous) 線程 ? 共享資源的訪問 ? 多個線程對同一資源進(jìn)行操作 (讀 /寫 ) ? 當(dāng)多個線程訪問同一數(shù)據(jù)項(如靜態(tài)字段、可全局訪問對象的實例字段或共享集合)時,需要確保它們協(xié)調(diào)了對數(shù)據(jù)的訪問,這樣它們都可以看到數(shù)據(jù)的一致視圖,而且相互不會干擾另一方的更改 ? synchronized 關(guān)鍵詞 ? wait() / notify() / notifyAll() 方法 線程的同步 40 ? 線程同步 實例 線程的同步 public class CubbyHole { private int contents。 public int get() { return contents。 } public void put(int value) { contents = value。 } } public class Producer extends Thread { private CubbyHole cubbyhole。 public Producer(CubbyHole c) { cubbyhole = c。 } public void run() { for (int i = 0。 i 10。 i++) { (i)。 (Producer + put: + i)。 try { sleep((int)(() * 100))。 } catch (InterruptedException e) { } } } } public class Consumer extends Thread { private CubbyHole cubbyhole。 public Consumer(CubbyHole c) { cubbyhole = c。 } public void run() { int value = 0。 for (int i = 0。 i 10。 i++) { value = ()。 (Consumer “ + got: + value)。 } } } public class ProducerConsumerTest { public static void main(String[] args) { CubbyHole h = new CubbyHole()。 Producer p = new Producer(h)。 Consumer c = new Consumer(h)。 ()。 ()。 } } . . . Consumer got: 3 Producer put: 4 Producer put: 5 Consumer got: 5 . . . . . . Producer put: 4 Consumer got: 4 Consumer got: 4 Producer put: 5 . . . 41 1. 給關(guān)鍵部分 (Critical Section)加鎖 (lock) ? CubbyHole對象 ? synchronized 關(guān)鍵詞 ?the two threads must not simultaneously access the CubbyHole. ? The Java platform then associates a lock with every object that has synchronized code 線程的同步 public class CubbyHole { private int contents。 public int get() { return contents。 } public void put(int value) { contents = value。 } } public class CubbyHole { private int contents。 public synchronized int get() { return contents。 } public synchronized void put(int value) { contents = value。 } } 42 2. 線程的協(xié)調(diào) ? the two threads must do some simple coordination. ? Producer通過某種方式告訴 Consumer在CubbyHole中有值,而 Consumer必須通過某種方式表示出 CubbyHole中的值已被取走 ? CubbyHole對象 (Critical Section) ? ? wait()、 notify()、 notifyAll() 線程的同步 43 線程的同步 public class CubbyHole { private int contents。 public int get() { return contents。 } public void put(int value) { contents = value。 } } public class CubbyHole { private int contents。 private boolean available = false。 public synchronized int get() { while (available == false) { try { wait()。 //打開鎖,等候 Producer填值 } catch (InterruptedException e) { } } available = false。 notifyAll()。 return contents。 } public synchronized void put(int value) { while (available == true) { try { wait()。 //打開鎖,等候 Consumer取值 } catch (InterruptedException e) { } } contents = value。 available = true。 notifyAll()。 } } public final void wait() throws InterruptedException public final void wait(long timeout) throws InterruptedException public final void notifyAll() //喚醒所有等待的線程 public final void notify() //隨機(jī)喚醒一個等待的線程 The wait method relinquishes the lock held by the Consumer on the CubbyHole (thereby allowing the Producer to get the lock and update the CubbyHole) and then waits for notification from the Producer. 44 線程的同步 public class CubbyHole { private int contents。 private boolean available = false。 public synchronized int get() { while (available == false) { try { wait()。 //打開鎖,等候 Producer填值 } catch (InterruptedException e) { } } available = false。 notifyAll()。 return contents。 } public synchronized void put(int value) { while (available == true) { try { wait()。 //打開鎖,等候 Consumer取值 } catch (InterruptedException e) { } } contents = value。 available = true。 notifyAll()。 } } 線程 consumer get() 判斷當(dāng)前是否存有值 有 沒有 設(shè)置為空 喚醒 producer 取值 釋放鎖定 等待新值 線程 producer put() 判斷當(dāng)前是否存有值 有 沒有 釋放鎖定 等待取值 放值 設(shè)置為有 喚醒 consumer 45 第七章 線程 1. 概述 2. 線程的創(chuàng)建 ? 兩種方式 3. 線程的同步 1. synchronized 2. wait()/notifyAll()/notify() 4. 線程的生命周期 46 ? 線程啟動 1. new SimpleThread1(Jamaica).start()。 class SimpleThread1 extends Thread { … … } 2. SimpleThread2 a = new SimpleThread2(Jamaica)。 Thread thread = new Thread(a)。 ()。 class SimpleThread2 implements Runnable { … … } ? 線程停止 ? A thread dies naturally when the run method exits. 線程的生命周期 public void run() { int i = 0。 while (i 100) { i++。 (i = + i)。 } } 47 ? 進(jìn)一步的學(xué)習(xí) ? Starvation (資源缺乏 ) ? Deadlock (死鎖 ) ? ThreadGroup (線程組 ) –同時對一組線程進(jìn)行操作 ? 優(yōu)先權(quán) (Priorities) ? Daemon線程 線程 48 第七章 結(jié)束! 49 ? 對數(shù)組對象的操作 (Arrays) ? 對象集合 (Set) ? 對象列表 (List) ? 對象映射 (Map) ? 對對象數(shù)組的操作 (Collections) ? 枚舉 (Enumeration)和迭代 (Ite
點擊復(fù)制文檔內(nèi)容
教學(xué)課件相關(guān)推薦
文庫吧 www.dybbs8.com
備案圖片鄂ICP備17016276號-1