日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

王之泰201771010131《面向对象程序设计(java)》第十七周学习总结

發(fā)布時(shí)間:2023/12/18 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 王之泰201771010131《面向对象程序设计(java)》第十七周学习总结 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

第一部分:理論知識學(xué)習(xí)部分

第14章 并發(fā)

線程同步

多線程并發(fā)運(yùn)行不確定性問題解決方案:引入線 程同步機(jī)制,使得另一線程要使用該方法,就只 能等待。

? 在Java中解決多線程同步問題的方法有兩種:

1.- Java SE 5.0中引入ReentrantLock類(P648頁)。

2.- 在共享內(nèi)存的類方法前加synchronized修飾符。

……

public synchronized static void sub(int m)

……

解決方案一:鎖對象與條件對象

用ReentrantLock保護(hù)代碼塊的基本結(jié)構(gòu)如下:

myLock.lock();

try {

   critical section

} finally{

myLock.unlock(); }

有關(guān)鎖對象和條件對象的關(guān)鍵要點(diǎn):

? 鎖用來保護(hù)代碼片段,保證任何時(shí)刻只能有一 個(gè)線程執(zhí)行被保護(hù)的代碼。

? 鎖管理試圖進(jìn)入被保護(hù)代碼段的線程。

? 鎖可擁有一個(gè)或多個(gè)相關(guān)條件對象。

? 每個(gè)條件對象管理那些已經(jīng)進(jìn)入被保護(hù)的代碼 段但還不能運(yùn)行的線程。

解決方案二: synchronized關(guān)鍵字

synchronized關(guān)鍵字作用:

? 某個(gè)類內(nèi)方法用synchronized 修飾后,該方法被稱為同步方法;

? 只要某個(gè)線程正在訪問同步方法,其他線程欲要訪問同步方法就被阻塞,直至線程從同步方法返回前喚醒被阻塞線程,其他線程方可能進(jìn)入同步方法。

? 一個(gè)線程在使用的同步方法中時(shí),可能根據(jù)問題的需要,必須使用wait()方法使本線程等待,暫時(shí)讓出CPU的使用權(quán),并允許其它線程使用這個(gè)同步方法。

? 線程如果用完同步方法,應(yīng)當(dāng)執(zhí)行notifyAll()方 法通知所有由于使用這個(gè)同步方法而處于等待的 線程結(jié)束等待。

第二部分:實(shí)驗(yàn)部分——線程同步控制

實(shí)驗(yàn)時(shí)間?2018-12-10

1、實(shí)驗(yàn)?zāi)康呐c要求

(1)?掌握線程同步的概念及實(shí)現(xiàn)技術(shù);?

(2)?線程綜合編程練習(xí)

2、實(shí)驗(yàn)內(nèi)容和步驟

實(shí)驗(yàn)1:測試程序并進(jìn)行代碼注釋。

測試程序1:

1.在Elipse環(huán)境下調(diào)試教材651頁程序14-7,結(jié)合程序運(yùn)行結(jié)果理解程序;

2.掌握利用鎖對象和條件對象實(shí)現(xiàn)的多線程同步技術(shù)。

1 package synch; 2 3 import java.util.*; 4 import java.util.concurrent.locks.*; 5 6 /** 7 一個(gè)銀行有許多銀行帳戶,使用鎖序列化訪問 * @version 1.30 2004-08-01 8 * @author Cay Horstmann 9 */ 10 public class Bank 11 { 12 private final double[] accounts; 13 private Lock bankLock; 14 private Condition sufficientFunds; 15 16 /** 17 * 建設(shè)銀行。 18 * @param n 賬號 19 * @param initialBalance 每個(gè)賬戶的初始余額 20 */ 21 public Bank(int n, double initialBalance) 22 { 23 accounts = new double[n]; 24 Arrays.fill(accounts, initialBalance); 25 bankLock = new ReentrantLock(); 26 sufficientFunds = bankLock.newCondition(); 27 } 28 29 /** 30 * 把錢從一個(gè)賬戶轉(zhuǎn)到另一個(gè)賬戶。 31 * @param 從賬戶轉(zhuǎn)賬 32 * @param 轉(zhuǎn)到要轉(zhuǎn)賬的賬戶 33 * @param 請?jiān)试S我向你轉(zhuǎn)達(dá) 34 */ 35 public void transfer(int from, int to, double amount) throws InterruptedException 36 { 37 bankLock.lock(); 38 try 39 { 40 while (accounts[from] < amount) 41 sufficientFunds.await(); 42 System.out.print(Thread.currentThread()); 43 accounts[from] -= amount; 44 System.out.printf(" %10.2f from %d to %d", amount, from, to); 45 accounts[to] += amount; 46 System.out.printf(" Total Balance: %10.2f%n", getTotalBalance()); 47 sufficientFunds.signalAll(); 48 } 49 finally 50 { 51 bankLock.unlock(); 52 } 53 } 54 55 /** 56 * 獲取所有帳戶余額的總和。 57 * @return 總余額 58 */ 59 public double getTotalBalance() 60 { 61 bankLock.lock(); 62 try 63 { 64 double sum = 0; 65 66 for (double a : accounts) 67 sum += a; 68 69 return sum; 70 } 71 finally 72 { 73 bankLock.unlock(); 74 } 75 } 76 77 /** 78 * 獲取銀行中的帳戶數(shù)量。 79 * @return 賬號 80 */ 81 public int size() 82 { 83 return accounts.length; 84 } 85 }

?

1 package synch; 2 3 /** 4 * 這個(gè)程序顯示了多個(gè)線程如何安全地訪問數(shù)據(jù)結(jié)構(gòu)。 5 * @version 1.31 2015-06-21 6 * @author Cay Horstmann 7 */ 8 public class SynchBankTest 9 { 10 public static final int NACCOUNTS = 100; 11 public static final double INITIAL_BALANCE = 1000; 12 public static final double MAX_AMOUNT = 1000; 13 public static final int DELAY = 10; 14 15 public static void main(String[] args) 16 { 17 Bank bank = new Bank(NACCOUNTS, INITIAL_BALANCE); 18 for (int i = 0; i < NACCOUNTS; i++) 19 { 20 int fromAccount = i; 21 Runnable r = () -> { 22 try 23 { 24 while (true) 25 { 26 int toAccount = (int) (bank.size() * Math.random()); 27 double amount = MAX_AMOUNT * Math.random(); 28 bank.transfer(fromAccount, toAccount, amount); 29 Thread.sleep((int) (DELAY * Math.random())); 30 } 31 } 32 catch (InterruptedException e) 33 { 34 } 35 }; 36 Thread t = new Thread(r); 37 t.start(); 38 } 39 } 40 }

?

測試程序2:

1.在Elipse環(huán)境下調(diào)試教材655頁程序14-8,結(jié)合程序運(yùn)行結(jié)果理解程序;

2.掌握synchronized在多線程同步中的應(yīng)用。

?

1 package synch2; 2 3 import java.util.*; 4 5 /** 6 * 具有多個(gè)使用同步原語的銀行賬戶的銀行。 7 * @version 1.30 2004-08-01 8 * @author Cay Horstmann 9 */ 10 public class Bank 11 { 12 private final double[] accounts; 13 14 /** 15 * 建設(shè)銀行。 16 * @param n 賬號 17 * @param initialBalance 每個(gè)賬戶的初始余額 18 */ 19 public Bank(int n, double initialBalance) 20 { 21 accounts = new double[n]; 22 Arrays.fill(accounts, initialBalance); 23 } 24 25 /** 26 * 把錢從一個(gè)賬戶轉(zhuǎn)到另一個(gè)賬戶。 27 * @param 從賬戶轉(zhuǎn)賬 28 * @param 轉(zhuǎn)到要轉(zhuǎn)賬的賬戶 29 * @param 請?jiān)试S我向你轉(zhuǎn)達(dá) 30 */ 31 public synchronized void transfer(int from, int to, double amount) throws InterruptedException 32 { 33 while (accounts[from] < amount) 34 wait(); 35 System.out.print(Thread.currentThread()); 36 accounts[from] -= amount; 37 System.out.printf(" %10.2f from %d to %d", amount, from, to); 38 accounts[to] += amount; 39 System.out.printf(" Total Balance: %10.2f%n", getTotalBalance()); 40 notifyAll(); 41 } 42 43 /** 44 * 獲取所有帳戶余額的總和。 45 * @return 總余額 46 */ 47 public synchronized double getTotalBalance() 48 { 49 double sum = 0; 50 51 for (double a : accounts) 52 sum += a; 53 54 return sum; 55 } 56 57 /** 58 * 獲取銀行中的帳戶數(shù)量。 59 * @return 60 */ 61 public int size() 62 { 63 return accounts.length; 64 } 65 }

?

1 package synch2; 2 3 /** 4 * 5 * 這個(gè)程序展示了多個(gè)線程如何使用同步方法安全地訪問數(shù)據(jù)結(jié)構(gòu)。 6 * @version 1.31 2015-06-21 7 * @author Cay Horstmann 8 */ 9 public class SynchBankTest2 10 { 11 public static final int NACCOUNTS = 100; 12 public static final double INITIAL_BALANCE = 1000; 13 public static final double MAX_AMOUNT = 1000; 14 public static final int DELAY = 10; 15 16 public static void main(String[] args) 17 { 18 Bank bank = new Bank(NACCOUNTS, INITIAL_BALANCE); 19 for (int i = 0; i < NACCOUNTS; i++) 20 { 21 int fromAccount = i; 22 Runnable r = () -> { 23 try 24 { 25 while (true) 26 { 27 int toAccount = (int) (bank.size() * Math.random()); 28 double amount = MAX_AMOUNT * Math.random(); 29 bank.transfer(fromAccount, toAccount, amount); 30 Thread.sleep((int) (DELAY * Math.random())); 31 } 32 } 33 catch (InterruptedException e) 34 { 35 } 36 }; 37 Thread t = new Thread(r); 38 t.start(); 39 } 40 } 41 }

測試程序3:

1.在Elipse環(huán)境下運(yùn)行以下程序,結(jié)合程序運(yùn)行結(jié)果分析程序存在問題;

2.嘗試解決程序中存在問題。

1 package sdfsd; 2 3 class Cbank 4 { 5 private static int s=2000; 6 public static void sub(int m) 7 { 8 int temp=s; 9 temp=temp-m; 10 try { 11 Thread.sleep((int)(1000*Math.random())); 12 } 13 catch (InterruptedException e) { } 14 s=temp; 15 System.out.println("s="+s); 16 } 17 } 18 19 20 class Customer extends Thread 21 { 22 public void run() 23 { 24 for( int i=1; i<=4; i++) 25 Cbank.sub(100); 26 } 27 } 28 public class Thread3 29 { 30 public static void main(String args[]) 31 { 32 Customer customer1 = new Customer(); 33 34 Customer customer2 = new Customer(); 35 customer1.start(); 36 customer2.start(); 37 } 38 }

?

?改進(jìn)

1 package sdfsd; 2 3 class Cbank 4 { 5 private static int s=2000; 6 public synchronized static void sub(int m) 7 { 8 int temp=s; 9 temp=temp-m; 10 try { 11 Thread.sleep((int)(1000*Math.random())); 12 } 13 catch (InterruptedException e) { } 14 s=temp; 15 System.out.println("s="+s); 16 } 17 } 18 19 20 class Customer extends Thread 21 { 22 public void run() 23 { 24 for( int i=1; i<=4; i++) 25 Cbank.sub(100); 26 } 27 } 28 29 public class Thread3 30 { 31 public static void main(String args[]) 32 { 33 Customer customer1 = new Customer(); 34 35 Customer customer2 = new Customer(); 36 customer1.start(); 37 customer2.start(); 38 } 39 }

?

?

實(shí)驗(yàn)2?編程練習(xí)

利用多線程及同步方法,編寫一個(gè)程序模擬火車票售票系統(tǒng),共3個(gè)窗口,賣10張票,程序輸出結(jié)果類似(程序輸出不唯一,可以是其他類似結(jié)果)。

Thread-0窗口售:第1張票

Thread-0窗口售:第2張票

Thread-1窗口售:第3張票

Thread-2窗口售:第4張票

Thread-2窗口售:第5張票

Thread-1窗口售:第6張票

Thread-0窗口售:第7張票

Thread-2窗口售:第8張票

Thread-1窗口售:第9張票

Thread-0窗口售:第10張票

?

?

1 public class Demo { 2 public static void main(String[] args) { 3 Mythread mythread = new Mythread(); 4 Thread ticket1 = new Thread(mythread); 5 Thread ticket2 = new Thread(mythread); 6 Thread ticket3 = new Thread(mythread); 7 ticket1.start(); 8 ticket2.start(); 9 ticket3.start(); 10 } 11 } 12 13 class Mythread implements Runnable { 14 int ticket = 1; 15 boolean flag = true; 16 17 @Override 18 public void run() { 19 while (flag) { 20 try { 21 Thread.sleep(500); 22 } catch (InterruptedException e) { 23 // TODO Auto-generated catch block 24 e.printStackTrace(); 25 } 26 27 synchronized (this) { 28 if (ticket <= 10) { 29 System.out.println(Thread.currentThread().getName() + "窗口售:第" + ticket + "張票"); 30 ticket++; 31 } 32 if (ticket > 10) { 33 flag = false; 34 } 35 } 36 } 37 } 38 39 }

?

?

?第三部分:總結(jié)

  在本周的學(xué)習(xí)中,我學(xué)習(xí)了線程同步這一知識點(diǎn),我了解到這一知識點(diǎn)是用來解決多線程并發(fā)運(yùn)行不確定性問題。并且這周是最后一周學(xué)習(xí),助教學(xué)長為我們做了完整的演示來結(jié)束這學(xué)期的學(xué)習(xí),總之,這學(xué)期在老師和助教學(xué)長的幫助下我們的學(xué)習(xí)能力有了很大的提升。感謝老師,也感謝助教學(xué)長!

?

轉(zhuǎn)載于:https://www.cnblogs.com/hackerZT-7/p/10150786.html

總結(jié)

以上是生活随笔為你收集整理的王之泰201771010131《面向对象程序设计(java)》第十七周学习总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。