熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java中的同步——示例程序

2013-11-23 19:09:19  來源: Java核心技術 

  前面說到了Java中的同步問題下面通過一個小小的實例程序來演示Java中的同步方法其中對前文提到的Counter類做了稍微的修改

  public class Counter {

  private int c = ;

  public void increment() {

  Systemoutprintln(before increment c = + c);

  c++;

  try {

  Threadsleep();

  } catch (InterruptedException e) {

  eprintStackTrace();

  }

  Systemoutprintln(after increment c = + c);

  }

  public  void decrement() {

  Systemoutprintln(before decrement c = + c);

  c;

  try {

  Threadsleep();

  } catch (InterruptedException e) {

  eprintStackTrace();

  }

  Systemoutprintln(after decrement c = + c);

  }

  public int value() {

  return c;

  }

  }

  在上面的Counter類的實現中分別對increment和decrement方法中增加了sleep()的調用這樣做的目的是為了放大兩個線程對同一對象的方法調用時的交錯效果

  下面是兩個線程在ThreadA中調用了次increment()方法在ThreadB中調用了次decrement()方法

  Thread

  public class ThreadA implements Runnable {

  private Counter c;

  public ThreadA(Counter c) {

  thisc = c;

  }

  @Override

  public void run() {

  for (int i = ; i < ; i++) {

  thiscincrement();

  }

  }

  }

  ThreadB

  public class ThreadB implements Runnable {

  private Counter c;

  public ThreadB(Counter c) {

  thisc = c;

  }

  @Override

  public void run() {

  for (int i = ; i < ; i++) {

  thiscdecrement();

  }

  }

  }

  主程序如下其中生成了兩個線程threadA和ThreadB他們共享Counter c

  public class Main {

  public static void main(String[] args) {

  Counter c = new Counter();

  ThreadA a = new ThreadA(c);

  ThreadB b = new ThreadB(c);

  Thread threadA = new Thread(a);

  Thread threadB = new Thread(b);

  threadAstart();

  threadBstart();

  }

  }

  執行上面的代碼可能的結果如下

  before increment c =

  before decrement c =

  after increment c =

  before increment c =

  after decrement c =

  before decrement c =

  after increment c =

  before increment c =

  after decrement c =

  before decrement c =

  after increment c =

  before increment c =

  after decrement c =

  before decrement c =

  after increment c =

  before increment c =

  after decrement c =

  before decrement c =

  after increment c =

  before increment c =

  after decrement c =

  before decrement c =

  after increment c =

  before increment c =

  after increment c =

  before increment c =

  after decrement c =

  before decrement c =

  after decrement c =

  before decrement c =

  after increment c =

  before increment c =

  after increment c =

  before increment c =

  after decrement c =

  before decrement c =

  after increment c =

  after decrement c =

  before decrement c =

  after decrement c =

  從上面的輸出結果中我們不難看出出現了嚴重的交錯現象! 在increment或者是decrement方法中輸出before和after本應該是成對連續出現的但輸出結果卻不是如此

  將上面代碼的increment()和decrement()方法用synchronized 修飾後重新運行該程序輸出結果如下

  before increment c =

  after increment c =

  before increment c =

  after increment c =

  before decrement c =

  after decrement c =

  before increment c =

  after increment c =

  before decrement c =

  after decrement c =

  before decrement c =

  after decrement c =

  before decrement c =

  after decrement c =

  before decrement c =

  after decrement c =

  before increment c =

  after increment c =

  before decrement c =

  after decrement c =

  before decrement c =

  after decrement c =

  before decrement c =

  after decrement c =

  before decrement c =

  after decrement c =

  before increment c =

  after increment c =

  before decrement c =

  after decrement c =

  before increment c =

  after increment c =

  before increment c =

  after increment c =

  before increment c =

  after increment c =

  before increment c =

  after increment c =

  before increment c =

  after increment c =

  這樣輸出結果和沒有增加synchronized修飾符時的大不相同單獨一次的increment和decrement方法並沒有出現交錯的現象只是連續次的increment()和decrement ()有交錯(這個不是synchronized能解決的問題)

  至少我們從上面的實例程序中可以看到synchronized方法的作用了

  另外我們還可以使用同步語句(也就是使用Counter對象的同步鎖)來代替上面的同步方法其效果是一樣的有興趣的網友可以自己嘗試一下


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26369.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.