转载请注明出处
http://blog.csdn.net/pony_maggie/article/details/42587821
作者:小马
先看一个示例, 多个计数器线程实例(TwoCounter), 每个实例有两个整型计数器,线程运行时,两个计数器开始"同时"累加,还有若干个监控器线程实例(Watcher),每个实例都会利用自己的"线程时间"监控所有的计数器实例,当某个实例中的count1和count2不相等时,就会显示"unsynched"。
贴出一些关键的代码,
class TwoCounter extends Thread { private boolean started = false; private JTextField t1 = new JTextField(5); private JTextField t2 = new JTextField(5); private JLabel label = new JLabel("count1 == count2"); private int count1 = 0, count2 = 0; public TwoCounter() { JPanel p = new JPanel(); p.add(t1); p.add(t2); p.add(label); getContentPane().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) { System.err.println("Interrupted"); } } } //用来监视计算器的对外API public void synchTest() { Sharing1.incrementAccess();//追踪访问次数 if(count1 != count2) { label.setText("Unsynched"); } } }
class Watcher extends Thread { public Watcher() { start(); } public void run() { while(true) { for(int i = 0; i < s.length; i++) { s[i].synchTest(); } try { sleep(500); } catch (InterruptedException e) { System.err.println("interrupted"); } } } }
结果证明确实有不相等的情况出现,原因是什么呢?
再回头看一下TwoCounter中的run函数,线程的执行函数,下面两行:
t1.setText(Integer.toString(count1++)); t2.setText(Integer.toString(count2++));