經驗不在于年限,在于積累---專注互聯網軟件開發

          把工作當事業做,把項目當作品做!

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            55 Posts :: 0 Stories :: 66 Comments :: 0 Trackbacks

          最近看到一道面試題,比較有意思:


          有三個線程
          ID分別是A、B、C,請有多線編程實現,在屏幕上循環打印10ABCABC…

          由于線程執行的不確定性,要保證這樣有序的輸出,必須控制好多線程的同步。

          線程同步有兩種基本方法:

          (1)    synchronized

          (2)    wait,notify,notifyAll

          現在分別采用這兩種方法來解答這道題目。

          /**

          @author陳新漢 http://www.aygfsteel.com/hankchen

           * 2009-12-28 下午01:57:04

           */

          /**

           *采用多線程技術打印10“ABC”,即ABCABC...”

           * 實現方式(一)利用synchronized關鍵字實現

           */

          public class XunleiInterviewMultithread {

                 /**

                  * @param args

                  */

                 public static void main(String[] args) {

                        XunleiLock lock = new XunleiLock();

                        new Thread(new XunleiPrinter("A", lock)).start();

                        new Thread(new XunleiPrinter("B", lock)).start();

                        new Thread(new XunleiPrinter("C", lock)).start();

                 }

          }

          class XunleiPrinter implements Runnable {

                 private String name = "";

                 private XunleiLock lock = null;

                 private int count=10;

                 public XunleiPrinter(String name, XunleiLock lock) {

                        this.name = name;

                        this.lock = lock;

                 }

                 @Override

                 public void run() {

                        while(count>0) {

                               synchronized (lock) {

                                      if (lock.getName().equalsIgnoreCase(this.name)) {

                                             System.out.print(name);

                                             count--;

                                             if (this.name.equals("A")) {

                                                    lock.setName("B");

                                             } elseif (this.name.equals("B")) {

                                                    lock.setName("C");

                                             } elseif (this.name.equals("C")) {

                                                    lock.setName("A");

                                             }

                                      }

                               }

                        }

                 }

          }

          class XunleiLock

          {

                 public String name = "A";

                 public String getName() {

                        return name;

                 }

                 public void setName(String name) {

                        this.name = name;

                 }

          }

          方法(二)線程類修改如下,其他類一樣:

          class XunleiPrinter2 implements Runnable {

                 private String name = "";

                 private XunleiLock lock = null;

                 private int count=10;

                 public XunleiPrinter2(String name, XunleiLock lock) {

                        this.name = name;

                        this.lock = lock;

                 }

                 @Override

                 public void run() {

                        while(count>0) {

                               synchronized (lock) {

                                      while(!lock.getName().equalsIgnoreCase(this.name)) {

                                             try{

                                                    lock.wait();

                                             }catch(InterruptedException e){

                                                    e.printStackTrace();

                                             }

                                      }

                                      System.out.print(name);

                                      count--;

                                      if (this.name.equals("A")) {

                                             lock.setName("B");

                                      } elseif (this.name.equals("B")) {

                                             lock.setName("C");

                                      } elseif (this.name.equals("C")) {

                                             lock.setName("A");

                                      }

                                      lock.notifyAll();

                               }

                        }

                 }

          }

          (友情提示:本博文章歡迎轉載,但請注明出處:陳新漢,http://www.aygfsteel.com/hankchen
          posted on 2009-12-29 20:58 hankchen 閱讀(3691) 評論(0)  編輯  收藏 所屬分類: Java基礎
          主站蜘蛛池模板: 阜阳市| 南皮县| 喀喇| 华池县| 酉阳| 庆阳市| 慈溪市| 同江市| 永善县| 卢龙县| 张家界市| 隆昌县| 襄城县| 突泉县| 山阳县| 渑池县| 枣强县| 钟山县| 宁蒗| 许昌县| 四川省| 朝阳区| 札达县| 玉环县| 青海省| 本溪| 太仆寺旗| 平罗县| 铁力市| 揭东县| 永年县| 灯塔市| 呈贡县| 临城县| 阿瓦提县| 财经| 历史| 衡阳市| 缙云县| 尼勒克县| 桑日县|