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 掌握利用锁对象和条件对象实现的多线程同步技术。
package synch; import java.util.*; import java.util.concurrent.locks.*; /** * A bank with a number of bank accounts that uses locks for serializing access. * @version 1.30 2004-08-01 * @author Cay Horstmann */ public class Bank { private final double[] accounts; private Lock bankLock; private Condition sufficientFunds; /** * Constructs the bank. * @param n the number of accounts * @param initialBalance the initial balance for each account */ public Bank(int n, double initialBalance) { accounts = new double[n]; Arrays.fill(accounts, initialBalance); bankLock = new ReentrantLock();//锁对象初始化 sufficientFunds = bankLock.newCondition();//newCondition方法生成锁对象的条件对象 } /** * Transfers money from one account to another. * @param from the account to transfer from * @param to the account to transfer to * @param amount the amount to transfer */ public void transfer(int from, int to, double amount) throws InterruptedException {//加锁 bankLock.lock(); try { while (accounts[from] < amount) sufficientFunds.await();//将线程放到条件的等待集中 System.out.print(Thread.currentThread()); accounts[from] -= amount; System.out.printf(" %10.2f from %d to %d", amount, from, to); accounts[to] += amount; System.out.printf(" Total Balance: %10.2f%n", getTotalBalance()); sufficientFunds.signalAll();//解除该条件的等待集中随机的所有线程的阻塞状态 } finally { bankLock.unlock();//释放这个锁 } } /** * Gets the sum of all account balances. * @return the total balance */ public double getTotalBalance() { bankLock.lock(); try { double sum = 0; for (double a : accounts) sum += a; return sum; } finally { bankLock.unlock(); } } /** * Gets the number of accounts in the bank. * @return the number of accounts */ public int size() { return accounts.length; } }