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

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

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

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


          有三個線程
          ID分別是ABC,請有多線編程實現,在屏幕上循環打印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基礎
          主站蜘蛛池模板: 腾冲县| 天柱县| 天长市| 盐城市| 德庆县| 策勒县| 莱阳市| 湖口县| 漳平市| 高雄县| 错那县| 安仁县| 嘉兴市| 乌鲁木齐市| 修武县| 新乐市| 天峨县| 吴江市| 会理县| 大丰市| 酒泉市| 额尔古纳市| 洪洞县| 福建省| 三江| 邮箱| 大安市| 明溪县| 上犹县| 炎陵县| 黄梅县| 东兴市| 沅陵县| 临沂市| 东乡县| 龙胜| 钟山县| 吉水县| 疏附县| 徐闻县| 杭锦旗|