关键词:
1、实验目的与要求
(1) 掌握线程同步的概念及实现技术;
(2) 线程综合编程练习
2、实验内容和步骤
实验1:测试程序并进行代码注释。
测试程序1:
l 在Elipse环境下调试教材651页程序14-7,结合程序运行结果理解程序;
l 掌握利用锁对象和条件对象实现的多线程同步技术。
1 package synch; 2 3 import java.util.*; 4 import java.util.concurrent.locks.*; 5 6 /** 7 * A bank with a number of bank accounts that uses locks for serializing access. 8 * @version 1.30 2004-08-01 9 * @author Cay Horstmann 10 */ 11 public class Bank 12 13 private final double[] accounts;//银行的基本数据 14 private Lock bankLock;//锁对象 15 private Condition sufficientFunds; 16 17 /** 18 * Constructs the bank. 19 * @param n the number of accounts 20 * @param initialBalance the initial balance for each account 21 */ 22 public Bank(int n, double initialBalance) 23 24 accounts = new double[n]; 25 Arrays.fill(accounts, initialBalance); 26 bankLock = new ReentrantLock(); 27 sufficientFunds = bankLock.newCondition(); 28 29 30 /** 31 * Transfers money from one account to another. 32 * @param from the account to transfer from 33 * @param to the account to transfer to 34 * @param amount the amount to transfer 35 */ 36 public void transfer(int from, int to, double amount) throws InterruptedException 37 38 bankLock.lock(); 39 try 40 //锁对象的引用条件对象 41 while (accounts[from] < amount) 42 sufficientFunds.await(); 43 System.out.print(Thread.currentThread());//打印出线程号 44 accounts[from] -= amount; 45 System.out.printf(" %10.2f from %d to %d", amount, from, to); 46 accounts[to] += amount; 47 System.out.printf(" Total Balance: %10.2f%n", getTotalBalance()); 48 sufficientFunds.signalAll(); 49 50 finally 51 52 bankLock.unlock(); 53 54 55 56 /** 57 * Gets the sum of all account balances. 58 * @return the total balance 59 */ 60 public double getTotalBalance() 61 62 bankLock.lock();//加锁 63 try 64 65 double sum = 0; 66 67 for (double a : accounts) 68 sum += a; 69 70 return sum; 71 72 finally 73 74 bankLock.unlock();//解锁 75 76 77 78 /** 79 * Gets the number of accounts in the bank. 80 * @return the number of accounts 81 */ 82 public int size() 83 84 return accounts.length; 85 86
1 package synch; 2 3 /** 4 * This program shows how multiple threads can safely access a data structure. 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:
l 在Elipse环境下调试教材655页程序14-8,结合程序运行结果理解程序;
l 掌握synchronized在多线程同步中的应用。
1 package synch2; 2 3 /** 4 * 这个程序显示了多个线程如何安全地访问数据结构,使用同步方法。 5 * @version 1.31 2015-06-21 6 * @author Cay Horstmann 7 */ 8 public class SynchBankTest2 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
1 package synch2; 2 3 import java.util.*; 4 5 /** 6 * 具有多个使用同步原语的银行账户的银行。 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 * 建设银行。 16 * @param n the number of accounts 17 * @param initialBalance the initial balance for each account 18 */ 19 public Bank(int n, double initialBalance) 20 21 accounts = new double[n]; 22 Arrays.fill(accounts, initialBalance); 23 24 25 /** 26 * 把钱从一个账户转到另一个账户。 27 * @param from the account to transfer from 28 * @param to the account to transfer to 29 * @param amount the amount to transfer 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();//解除那些在该对象上调用wait方法的线程阻塞状态。该方法只能在同步方法或同步块内部调用。 41 42 43 /** 44 * 获取所有帐户余额的总和。 45 * @return the total balance 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 * 获取银行中的帐户数量。 59 * @return the number of accounts 60 */ 61 public int size() 62 63 return accounts.length; 64 65
测试程序3:
l 在Elipse环境下运行以下程序,结合程序运行结果分析程序存在问题;
l 尝试解决程序中存在问题。
class Cbank
private static int s=2000; public static void sub(int m)
int temp=s; temp=temp-m; try Thread.sleep((int)(1000*Math.random()));
catch (InterruptedException e) s=temp; System.out.println("s="+s);
class Customer extends Thread
public void run()
for( int i=1; i<=4; i++) Cbank.sub(100);
public class Thread3
public static void main(String args[])
Customer customer1 = new Customer(); Customer customer2 = new Customer(); customer1.start(); customer2.start();
|
改进后:
1 class Cbank 2 private static int s = 2000; 3 4 public synchronized static void sub(int m) 5 int temp = s; 6 temp = temp - m; 7 try 8 Thread.sleep((int) (1000 * Math.random())); 9 catch (InterruptedException e) 10 11 s = temp; 12 System.out.println("s=" + s); 13 14 15 16 class Customer extends Thread 17 public void run() 18 for (int i = 1; i <= 4; i++) 19 Cbank.sub(100); 20 21 22 23 public class Thread3 24 public static void main(String args[]) 25 Customer customer1 = new Customer(); 26 Customer customer2 = new Customer(); 27 customer1.start(); 28 customer2.start(); 29 30
实验2 编程练习
利用多线程及同步方法,编写一个程序模拟火车票售票系统,共3个窗口,卖10张票,程序输出结果类似(程序输出不唯一,可以是其他类似结果)。
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 Main 2 public static void main(String[] args) 3 Mythread mythread=new Mythread(); 4 Thread t1=new Thread(mythread); 5 Thread t2=new Thread(mythread); 6 Thread t3=new Thread(mythread); 7 t1.start(); 8 t2.start(); 9 t3.start(); 10 11 12 class Mythread implements Runnable 13 int t=1; 14 boolean flag=true; 15 public void run() 16 while(flag) 17 try 18 Thread.sleep(300); 19 catch(InterruptedException e) 20 e.printStackTrace(); 21 22 synchronized(this) 23 if(t<=10) 24 System.out.println(Thread.currentThread().getName() + "窗口售:第" + t + "张票"); 25 t++; 26 27 if(t>10) 28 flag=false; 29 30 31 32 33
实验总结:本周实验学会了如何使用线程同步机制来解决多线程并发导致的不确定性。
李清华201772020113《面向对象程序设计(java)》第十三周学习总结(代码片段)
1、实验目的与要求(1)掌握事件处理的基本原理,理解其用途;(2)掌握AWT事件模型的工作机制;(3)掌握事件处理的基本编程模型;(4)了解GUI界面组件观感设置方法;(5)掌握WindowAdapter类、AbstractAction类的用法;(6)掌握GUI程序中鼠标... 查看详情
李清华201772020113《面向对象程序设计(java)》第十四周学习总结(代码片段)
1、实验目的与要求(1)掌握GUI布局管理器用法;(2)掌握各类JavaSwing组件用途及常用API;2、实验内容和步骤实验1:导入第12章示例程序,测试程序并进行组内讨论。测试程序1l 在elipseIDE中运行教材479页程序12-1,结合运行结果理... 查看详情
201772020113李清华《面向对象程序设计(java)》第17周学习总结(代码片段)
1、实验目的与要求(1)掌握线程同步的概念及实现技术;(2)线程综合编程练习2、实验内容和步骤实验1:测试程序并进行代码注释。测试程序1:l 在Elipse环境下调试教材651页程序14-7,结合程序运行结果理解程序;l 掌... 查看详情
李清华201772020113《面向对象程序设计(java)》第六周学习总结(代码片段)
第一部分理论知识1.继承用已有类来构建新类的一种机制。新类可以继承父类的方法和域,同时可以在新类中添加新的方法和域。已有类称为:超类、基类或父类。新类称作:子类、派生类或孩子类。子类的构造器不能直接访问... 查看详情
201772020113李清华《面向对象程序设计(java)》第18周学习总结(代码片段)
1、实验目的与要求(1)综合掌握java基本程序结构;(2)综合掌握java面向对象程序设计特点;(3)综合掌握javaGUI程序设计结构;(4)综合掌握java多线程编程模型;(5)综合编程练习。2、实验内容和步骤任务1:填写课程课后调查问卷,网... 查看详情
esayxc++面向对象程序设计实践-汉诺塔
记一次课设GitHub:https://github.com/tao355667/HanoiGame面向对象程序设计实践-汉诺塔IDE:VisualStudio2022依赖库:EsayX效果预览:参考资料:[1]童晶,丁海军,金永霞,周小芹编著.面向“工程教育认证”计算机系列课程规划教材C语言课程... 查看详情
uml系统建模基础教程(清华大学出版社)课后题答案
参考技术AUML习题答案第一章 面向对象设计与UML1. 填空题(1) 基本构造块UML规则公共机制(2) 名字属性操作(3) 封装继承多态(4) 继承(5) 瀑布模型喷泉模型基于组件的开发模型XP开发模型2.选择题(1)C(2)ABCD(3)ABCD... 查看详情
面向对象程序设计介绍以及面向对象的基本特征
面向对象的程序设计(ObjectOrientedProgramming,OOP)方法是目前比较流行的程序设计方法,和面向过程的程序设计比,它更符合人类的自然思维方式。在面向过程程序设计中,程序=数据+算法,数据和对数据的操作是分离的,如... 查看详情
《面向对象程序设计概述》牛咏梅
面向对象程序设计概述牛咏梅(南阳理工学院河南南阳473000)摘要:分析传统程序设计方法与面向对象程序设计方法之间的差别,重点分析了面向对象程序设计方法的特点,介绍了面向对象程序设计方法的步骤及其优点。关键词:面向对... 查看详情
java面向对象大致梳理
...概述:Java设计语言面向对象:Java语言是一种面向对象的程序设计语言,而面向对象思想是一种程序设计思想,我们在面向对象思想的指引下,使用Java语言去设计、开发计算机程序。这里的对象泛指现实中一切事物,每种事物都... 查看详情
面向对象的程序设计
阅读目录一面向对象的程序设计的由来二什么是面向对象的程序设计及为什么要有它三类与对象四属性查找五绑定到对象的方法的特殊之处六对象之间的交互七练习八继承与派生九多态与多态性十封装十一绑定方法与非绑定方法... 查看详情
面向对象思想
...0年代,位于美国加州的Xerox研究中心推出smalltalk语言及其程序设计环境,使得面向对象程序设计方法得到比较完善的实现,掀起了面向对象研究的高潮。到80年代中后期,面向对象的软件设计和程序设计方法 查看详情
面向对象设计----软件设计师
上午12分下午两大答题30分面向对象的基本概念❤❤❤❤❤采用面向对象的软件开发,通常由面向对象分析,面向对象设计,面向对象实现1面向对象分析OOA:获取对应用问题的理解,主要任务是抽取和整理用户需求并建立问题域精确模... 查看详情
面向对象分析与设计面向对象设计包括哪些内容
一、总述面向对象分析的输入是用户的功能需求,输出是简单的、理性化的分析模型,此阶段的工作更多侧重于如何理解软件的功能需求;面向对象设计的输入是面向对象分析的结果,蔬菜水果最终的、细化后的设计模型,此阶... 查看详情
面向对象思想初识
面向对象思想概述Java语言是一种面向对象的程序设计语言,而面向对象思想是一种程序设计思想,我们在面向对象思想的指引下,使用Java语言去设计、开发计算机程序。这里的对象泛指现实中一切事物,每种事物都具备自己的... 查看详情
面向过程程序设计,面向对象程序设计,可视化程序设计的异同
...开-闭原则。也使代码更易阅读。相对而言,面向过程的程序设计是面向对象程序设计的基础。面向对象的程序里面一定会有面向过程的程序片断的!可视化程序设计主要是一种技术 参考技术B这些是阶梯型向上发展的,一个比一... 查看详情
面向对象程序设计的总结
自学习面向对象程序设计语言以来,深深体会到这种语言的魅力。与面向过程设计语言相比,随着学习的深入,两者的风格不一更加凸显。面向过程程序设计语言,典型的有C语言、C++,面向过程是一种以过程... 查看详情
面向对象的程序设计(代码片段)
一、什么是面向对象 面向对象编程是一种编程方式,使用“类”和“对象”来实现,所以,面向对象编程就是对“类”和“对象”的使用。 面向对象编程核心就是“对象”二字,“对象”是特征与技能的结合体。“类”... 查看详情