[ 永远的UNIX::UNIX技术资料的宝库 ]

首页 > 编程技术 > Java > 正文
 

如何使用Java编写多线程程序(2)

作者:贾波 来源:Java中文站 (2006-05-24 10:35:32)

 

三、程序示例

例一、

让我们看看下面的例子。取钱的流程是输入密码,然后确定要取得金额,如果所取的金额小于或等于可以取出的金额,WITHDRAW则返回TRUE,然后ATM机出钱,然后打印清单;否则返回FALSE,然后打印清单。如下图:

public class AutomatedTellerMachine extends Teller {

public void withdraw(float amount) {

Account a = getAccount();

if (a.deduct(amount))

dispense(amount);

printReceipt();

}

}

public class Account {

private float total;

public boolean deduct(float t) {

if (t <= total) {

total -= t;

return true;

}

return false;

}

}

就这个例子而言,假设有这种情况,对同一个账号可以在不同的地方取钱,在同一时间,不同地点,妻子和丈夫取钱,妻子输入了账号上的最大金额,丈夫也是一样,假如妻子输入后已经得到true的返回值,但是丈夫的线程所得到的值还没有更新,这样丈夫也能够得到true的返回值,这样就出现了问题!这个问题怎么解决呢?在java里面提供了控制机制以保证deduct操作时的原子性,那就是关键字synchronized。

在Account的deduct方法加入synchronized就可以解决这个问题。

例二、

在这里我们用多线程中最典型的例子,生产者与消费者问题。在这个例子里面我们定义了生产者Producer,消费者Consumer和仓库Warehouse三个类,在整个程序的生命周期里,生产者随机地制造出产品放到仓库中,消费者也是随即地从仓库中取出产品。

import exception.ProducerConsumerException;

/**

* Consumer.java

* Consumer

* By: Jiabo

* Date: Mar 21, 2004

* Time: 2:47:58 PM

*/

public class Consumer extends Thread {

private Warehouse warehouse;

private String id;

public Consumer(Warehouse warehouse, String id) {

this.warehouse = warehouse;

this.id = id;

}

public void run() {

int tmp = (int) Math.random() * 10;

try {

warehouse.get(tmp);

System.out.println("Consumer # " + this.id + " get " + tmp);

} catch (ProducerConsumerException e) {

e.printStackTrace();

}

try {

sleep((int) (Math.random() * 100));

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

在这个类中,值得注意的一点是run方法中必须使用try-catch,因为,消费者从仓库中取东西时有可能诸如仓库中的储量不够得异常,在消费者里面也是一样,只不过异常变为仓库已满。

import exception.*;

/**

* Producer.java

* Producer

* By: Jiabo

* Date: Mar 21, 2004

* Time: 2:47:45 PM

*/

public class Producer extends Thread {

private Warehouse warehouse;

private String id;

public Producer(Warehouse warehouse, String id) {

this.warehouse = warehouse;

this.id = id;

}

public void run() {

int tmp = (int) Math.random() * 10;

if (tmp != 0) {

try {

warehouse.put(tmp);

System.out.println("Consumer # " + this.id + " put " + tmp);

} catch (ProducerConsumerException e) {

e.printStackTrace();

}

}

try {

sleep((int) (Math.random() * 100));

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

最重要的一部分在Warehouse类,如上所说为了保证get何set的原子性,在这里使用了synchronized关键字,并且在操作时抛出了可能跑出的异常。

import exception.*;

/**

* Warehouse

* By: Jiabo

* Date: Mar 21, 2004

* Time: 2:48:10 PM

*/

public class Warehouse {

// max capability of the warehouse

private int MAX;

private int contents;

// init with max capacity

public Warehouse(int max) {

this.MAX = max;

this.contents = 0;

}

public synchronized void get(int amount) throws ProducerConsumerException {

// the amount you want to get is bigger than the contends that the warehouse stores

if (amount > this.contents) {

throw new NotEnoughGoodsException();

}

amount -= contents;

}

public synchronized void put(int amount) throws ProducerConsumerException {

// the amount you want to put is out of the capability of the warehouse

if (amount > (this.MAX - this.contents)) {

throw new WarehouseFullException();

} else if (this.contents == 0) {

// warehouse is empty

throw new WarehouseEmptyException();

}

amount += contents;

}

}

致谢:

非常感谢挚友eflyer在病中为本文的部分程序提出了宝贵建议,在此表示诚挚的谢意。

参考:

§ http://www-900.ibm.com/developerWorks/cn/java/j-thread/index.shtml

§ Java Threads, 2nd edition Scott Oaks & Henry Wong 2nd Edition January 1999 ISBN: 1-56592-418-5, 332 pages


(http://www.fanqiang.com)



 
 相关文章
Solaris 8 新增功能一览--(1)IPv6,LDAP,Java增强 2001-06-13 10:08:01
JAVA入门教程: 第 一 章  Java概述 2001-04-16 15:07:00
JAVA入门教程: 第 二 章 数 据 类 型 2001-04-16 15:08:15
JAVA入门教程: 第 三 章 运算符和表达式 2001-04-16 15:09:16
JAVA入门教程: 第 四 章 流 控 制 2001-04-16 15:10:00
JAVA入门教程: 第 五 章 数 组 2001-04-16 15:10:37
JAVA入门教程: 第 六 章 对 象、类、包和接口 2001-04-16 15:11:52
JAVA初学常见问题 2001-04-16 15:13:54
Java及Web应用(一) 2001-04-21 17:19:33
Java及Web应用(二) 2001-04-21 17:22:26
基于java servlet技术的留言簿 2001-04-21 17:36:45
Javascript源码:计算器 2001-05-19 08:42:55
Javascript源码:时间按钮 2001-05-17 14:26:12
Javascript源码:只显示当前日期 2001-05-17 14:28:10
Javascript源码:显示当前日期与时间 2001-05-14 16:45:08
JAVA Servlets简介 2001-05-30 12:10:00
很棒的JavaScript!!(弹性) 2001-06-03 10:08:00
Forte for Java简介与基础安装 2001-06-07 14:08:00
Java编程技巧(信号量,管道) 2001-06-08 15:00:00
JAVA的网络功能与编程 2001-06-26 14:08:00
 

★  感谢所有的作者为我们学习技术知识提供了一条捷径  ★
www.fanqiang.com