java资源访问的错误方法2007-05-29 yycnet.yeah.net yyc译现在考虑换成另一种方式来使用本章频繁见到的计数器。在下面的例子中,每个线程都包含了两个计数器,它们在run()里增值以及显示。除此以外,我们使用了Watcher类的另一个线程。它的作用是监视计数器,检查它们是否保持相等。这表面是一项无意义的行动,因为如果查看代码,就会发现计数器肯定是相同的。但实际情况却不一定如此。下面是程序的第一个版本:
//: Sharing1.java// Problems with resource sharing while threadingimport java.awt.*;import java.awt.event.*;import java.applet.*;class TwoCounter extends Thread {private boolean started = false;private TextField t1 = new TextField(5),t2 = new TextField(5);private Label l = new Label("count1 == count2");private int count1 = 0, count2 = 0;// Add the display components as a panel// to the given container:public TwoCounter(Container c) {Panel p = new Panel();p.add(t1);p.add(t2);p.add(l);c.add(p);}public void start() {if(!started) {started = true;super.start();}}public void run() {while (true) {t1.setText(Integer.toString(count1++));t2.setText(Integer.toString(count2++));try {sleep(500);} catch (InterruptedException e){}}}public void synchTest() {Sharing1.incrementAccess();if(count1 != count2)l.setText("Unsynched");}}class Watcher extends Thread {private Sharing1 p;public Watcher(Sharing1 p) { this.p = p;start();}public void run() {while(true) {for(int i = 0; i < p.s.length; i++)p.s[i].synchTest();try {sleep(500);} catch (InterruptedException e){}}}}public class Sharing1 extends Applet {TwoCounter[] s;private static int accessCount = 0;private static TextField aCount = new TextField("0", 10);public static void incrementAccess() {accessCount++;aCount.setText(Integer.toString(accessCount));}private Button start = new Button("Start"),observer = new Button("Observe");private boolean isApplet = true;private int numCounters = 0;private int numObservers = 0;public void init() {if(isApplet) {numCounters = Integer.parseInt(getParameter("size"));numObservers = Integer.parseInt(getParameter("observers"));}s = new TwoCounter[numCounters];for(int i = 0; i < s.length; i++)s[i] = new TwoCounter(this);Panel p = new Panel();start.addActionListener(new StartL());p.add(start);observer.addActionListener(new ObserverL());p.add(observer);p.add(new Label("Access Count"));p.add(aCount);add(p);}class StartL implements ActionListener {public void actionPerformed(ActionEvent e) {for(int i = 0; i < s.length; i++)s[i].start();}}class ObserverL implements ActionListener {public void actionPerformed(ActionEvent e) {for(int i = 0; i < numObservers; i++)new Watcher(Sharing1.this);}}public static void main(String[] args) {Sharing1 applet = new Sharing1();// This isn"t an applet, so set the flag and// produce the parameter values from args:applet.isApplet = false;applet.numCounters = (args.length == 0 ? 5 :Integer.parseInt(args[0]));applet.numObservers =(args.length < 2 ? 5 :Integer.parseInt(args[1]));Frame aFrame = new Frame("Sharing1");aFrame.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e){System.exit(0);}});aFrame.add(applet, BorderLayout.CENTER);aFrame.setSize(350, applet.numCounters *100);applet.init();applet.start();aFrame.setVisible(true);}} ///:~
和往常一样,每个计数器都包含了自己的显示组件:两个文本字段以及一个标签。根据它们的初始值,可知道计数是相同的。这些组件在TwoCounter构建器加入Container。由于这个线程是通过用户的一个“按下按钮”操作启动的,所以start()可能被多次调用。但对一个线程来说,对Thread.start()的多次调用是非法的(会产生违例)。在started标记和过载的start()方法中,大家可看到针对这一情况采取的防范措施。
在run()中,count1和count2的增值与显示方式表面上似乎能保持它们完全一致。随后会调用sleep();若没有这个调用,程序便会出错,因为那会造成CPU难于交换任务。
synchTest()方法采取的似乎是没有意义的行动,它检查count1是否等于count2;如果不等,就把标签设为“Unsynched”(不同步)。但是首先,它调用的是类Sharing1的一个静态成员,以便增值和显示一个访问计数器,指出这种检查已成功进行了多少次(这样做的理由会在本例的其他版本中变得非常明显)。
Watcher类是一个线程,它的作用是为处于活动状态的所有TwoCounter对象都调用synchTest()。其间,它会对Sharing1对象中容纳的数组进行遍历。可将Watcher想象成它掠过TwoCounter对象的肩膀不断地“偷看”。
Sharing1包含了TwoCounter对象的一个数组,它通过init()进行初始化,并在我们按下“start”按钮后作为线程启动。以后若按下“Observe”(观察)按钮,就会创建一个或者多个观察器,并对毫不设防的TwoCounter进行调查。
注意为了让它作为一个程序片在浏览器中运行,Web页需要包含下面这几行:
<applet code=Sharing1 width=650 height=500><param name=size value="20"><param name=observers value="1"></applet>
可自行改变宽度、高度以及参数,根据自己的意愿进行试验。若改变了size和observers,程序的行为也会发生变化。我们也注意到,通过从命令行接受参数(或者使用默认值),它被设计成作为一个独立的应用程序运行。
下面才是最让人“不可思议”的。在TwoCounter.run()中,无限循环只是不断地重复相邻的行:
t1.setText(Integer.toString(count1++));
t2.setText(Integer.toString(count2++));
(和“睡眠”一样,不过在这里并不重要)。但在程序运行的时候,你会发现count1和count2被“观察”(用Watcher观察)的次数是不相等的!这是由线程的本质造成的——它们可在任何时候挂起(暂停)。所以在上述两行的执行时刻之间,有时会出现执行暂停现象。同时,Watcher线程也正好跟随着进来,并正好在这个时候进行比较,造成计数器出现不相等的情况。
本例揭示了使用线程时一个非常基本的问题。我们跟无从知道一个线程什么时候运行。想象自己坐在一张桌子前面,桌上放有一把叉子,准备叉起自己的最后一块食物。当叉子要碰到食物时,食物却突然消失了(因为这个线程已被挂起,同时另一个线程进来“偷”走了食物)。这便是我们要解决的问题。
有的时候,我们并不介意一个资源在尝试使用它的时候是否正被访问(食物在另一些盘子里)。但为了让多线程机制能够正常运转,需要采取一些措施来防止两个线程访问相同的资源——至少在关键的时期。
为防止出现这样的冲突,只需在线程使用一个资源时为其加锁即可。访问资源的第一个线程会其加上锁以后,其他线程便不能再使用那个资源,除非被解锁。如果车子的前座是有限的资源,高喊“这是我的!”的孩子会主张把它锁起来。