1、实验目的与要求

(1) 掌握线程同步的概念及实现技术;

(2) 线程综合编程练习

一、理论知识

⚫ 线程同步

(1)多线程并发运行不确定性问题解决方案:引入线 程同步机制,使得另一线程要使用该方法,就只 能等待

(2)在Java中解决多线程同步问题的方法有两种:

解决方案一:锁对象与条件对象

用ReentrantLock保护代码块的基本结构如下: myLock.lock();

try { critical section }

finally{ myLock.unlock(); }

(3)解决方案二: synchronized关键字

synchronized关键字作用: ➢ 某个类内方法用synchronized 修饰后,该方 法被称为同步方法;

➢ 只要某个线程正在访问同步方法,其他线程欲要访问同步方法就被阻塞,直至线程从同步方法返回前唤醒被阻塞线程,其他线程方可能进入同步方法。

(4)在同步方法中使用wait()、notify 和notifyAll()方法

一个线程在使用的同步方法中时,可能根据问题 的需要,必须使用wait()方法使本线程等待,暂 时让出CPU的使用权,并允许其它线程使用这个 同步方法。

线程如果用完同步方法,应当执行notifyAll()方 法通知所有由于使用这个同步方法而处于等待的 线程结束等待。

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();//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. }

bank

  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. }

SynchBank

测试程序2:

l 在Elipse环境下调试教材655页程序14-8,结合程序运行结果理解程序;

l 掌握synchronized在多线程同步中的应用。

  1. package synch2;
  2.  
  3. import java.util.*;
  4.  
  5. /**
  6. * A bank with a number of bank accounts that uses synchronization primitives.
  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. * Constructs the bank.
  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. * Transfers money from one account to another.
  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. * Gets the sum of all account balances.
  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. * Gets the number of accounts in the bank.
  59. * @return the number of accounts
  60. */
  61. public int size()
  62. {
  63. return accounts.length;
  64. }
  65. }

Bank

  1. package synch2;
  2.  
  3. /**
  4. * This program shows how multiple threads can safely access a data structure,
  5. * using synchronized methods.
  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. }

SynchBankTest2

结果如下:

测试程序3:

l 在Elipse环境下运行以下程序,结合程序运行结果分析程序存在问题;

尝试解决程序中存在问题。

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

Thread3

实验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 Demo {
  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(500);
  19. }
  20. catch(InterruptedException e)
  21. {
  22. e.printStackTrace();
  23. }
  24.  
  25. synchronized(this) {
  26. if(t<=10) {
  27. System.out.println(Thread.currentThread().getName()+"窗口售:第"+t+"张票");
  28. t++;
  29. }
  30. if(t>10) {
  31. flag=false;
  32. }
  33. }
  34.  
  35. }
  36. }
  37. }

Demo

结果如下:

 三、实验总结

本次作业我们学习了线程同步的知识,在测试程序中更深一步的理解了理论知识,发现了线程的多变性,希望在接下来额学习中可以更好的运用。

201771010135 杨蓉庆《面对对象程序设计(java)》第十七周学习总结的更多相关文章

  1. 201771010134杨其菊《面向对象程序设计java》第九周学习总结

                                                                      第九周学习总结 第一部分:理论知识 异常.断言和调试.日志 1.捕获 ...

  2. 201771010135杨蓉庆《面向对象程序设计(java)》第四周学习总结

    学习目标 1.掌握类与对象的基础概念,理解类与对象的关系: 2.掌握对象与对象变量的关系: 3.掌握预定义类的基本使用方法,熟悉Math类.String类.math类.Scanner类.LocalDa ...

  3. 201771010135杨蓉庆 《面向对象程序设计(java)》第三周学习总结

    一:第1-3章学习内容: 第一章:复习基本数据类型 整型 byte(1个字节 表示范围:-2^7 ~ (2^7)-1) short(2个字节 表示范围:-2^15~(2^15)-1) int(4个字节 ...

  4. 201771010135杨蓉庆《面向对象程序设计(java)》第六周学习总结

    实验六 继承定义与使用 1.实验目的与要求 (1) 理解继承的定义: (2) 掌握子类的定义要求 (3) 掌握多态性的概念及用法: (4) 掌握抽象类的定义及用途: (5) 掌握类中4个成员访问权限修 ...

  5. 201771010135杨蓉庆《面向对象程序设计(java)》第二周学习总结

    第一部分:理论知识学习部分 3.1 标识符:由字母.下划线.美元符号和数字组成, 且第一个符号不能为数字,可用作:类名.变量名.方法名.数组名.文件名等.有Hello.$1234.程序名.www_12 ...

  6. 201771010135 杨蓉庆/张燕《面对对象程序设计(java)》第十三周学习总结

    1.实验目的与要求 (1) 掌握事件处理的基本原理,理解其用途: (2) 掌握AWT事件模型的工作机制: (3) 掌握事件处理的基本编程模型: (4) 了解GUI界面组件观感设置方法: (5) 掌握W ...

  7. 201771010135 杨蓉庆《2018面向对象程序设计(java)课程学习进度条》

                                                                                                        ...

  8. 马凯军201771010116《面向对象与程序设计Java》第九周学习总结

    一.理论知识部分 异常.日志.断言和调试 1.异常:在程序的执行过程中所发生的异常事件,它中断指令的正常执行. 2.Java的异常处理机制可以控制程序从错误产生的位置转移到能够进行错误处理的位置. 3 ...

  9. 扎西平措 201571030332《面向对象程序设计 Java 》第一周学习总结

    <面向对象程序设计(java)>第一周学习总结 正文开头: 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 ...

  10. 201771010135 杨蓉庆《面对对象程序设计(java)》第十八周学习总结

    1.实验目的与要求 (1) 综合掌握java基本程序结构: (2) 综合掌握java面向对象程序设计特点: (3) 综合掌握java GUI 程序设计结构: (4) 综合掌握java多线程编程模型: ...

随机推荐

  1. day30 NFS服务器概述

    02. NFS存储服务概念介绍 NFS是Network File System的缩写,中文意思是网络文件共享系统, 它的主要功能是通过网络(一般是局域网)让不同的主机系统之间可以共享文件或目录 存储服 ...

  2. python接口自动化之发送post(四)

    1.ssl问题 目前很多的请求使用的都是较为安全的https请求,https请求相对于http安全级别更高,需要验证ssl证书 写代码的时候有两种可以忽略ssl验证的方法 (1)import urll ...

  3. oracle 锁表处理

    1.查询 select object_name,machine,s.sid,s.serial#from v$locked_object l,dba_objects o ,v$session swher ...

  4. Linux oracle中文乱码的问题解决

    乱码问题的根源是字符集的修改 1.查看linux的默认语言 2.查看客户端的语言编码设置 配置文件中的配置: cat  ~/.bash_profile 注意修改配置信息: export PATHexp ...

  5. Shell的 for 循环小例子

    <1> 上例子 for i in f1 f2 f3; do @echo $i; done 执行结果: f1 f2 f3 但是,请注意:如果是在makefile 中写,要写成这个样子: al ...

  6. 解决 genymotion 安装apk报错 app contains ARM native code and your Genymotion device cannot run ARM instructions

    1.某些APP安装在模拟器时提示“ this probably means that the app contains ARM native code and your Genymotion devi ...

  7. qml-main.cpp中的两种启动Qt Quick App模式

     现有两种启动Qt Quick App 模式: QQmlApplicationEngine搭配Window. QQuickView搭配Item.  qt默认使用第一种方法. QQmlApplicati ...

  8. django初步了解(一)

    安装django pip3 install django==版本号 创建一个djangp项目 django-admin startproject 项目名 目录介绍: 运行django项目: pytho ...

  9. OpenCV之Core组件进阶

    颜色空间缩减 利用C++类型转换时向下取整操作,实现定义域内颜色缩减.表达式如下 Inew = (Iold/10)*10 简单的颜色空间缩减算法可由以下两步组成: (1)遍历图像矩阵的每个元素 (2) ...

  10. Java爬虫学习(1)之爬取新浪微博博文

    本次学习采用了webmagic框架,完成的是一个简单的小demo package com.mieba.spiader; import us.codecraft.webmagic.Page; impor ...