李敏  
          日歷
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345
          統計
          • 隨筆 - 1
          • 文章 - 40
          • 評論 - 4
          • 引用 - 0

          導航

          常用鏈接

          留言簿(1)

          文章分類

          文章檔案

          相冊

          收藏夾

          它山之石

          聚賢莊

          搜索

          •  

          最新評論

           

           

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

          public class TestThread {

              
          private void service() {
                  
          int IDChartCount = ID.values().length - 1;

                  IDChart dpool 
          = new IDChart(IDChartCount);

                  
          for (ID id : ID.values()) {
                      Thread t 
          = new ThreadImpl(dpool, id, id.name());

                      t.start();
                  }

              }


              
          public static void main(String[] args) {
                  
          new TestThread().service();
              }

          }


          enum ID {
              a, b, c
          }


          class ThreadImpl extends Thread {
              
          private final ID id;

              
          private final IDChart chart;

              
          public ThreadImpl(IDChart chart, ID id, String name) {
                  
          super(name);

                  
          this.chart = chart;

                  
          this.id = id;
              }


              
          public void run() {
                  
          for (int i = 0; i < 10;) {
                      
          synchronized (chart) {
                          
          if (chart.check(id)) {
                              System.out.println(getId() 
          + " " + getName() + "  " + id);

                              i
          ++;
                          }

                      }

                      
          try {
                          sleep(
          1);
                      }
           catch (InterruptedException e) {
                          
          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }

                  }

              }

          }


          class IDChart {
              
          private int index = 0;

              
          private final int count;

              
          public IDChart(int count) {
                  
          this.count = count;
              }


              
          public boolean check(ID id) {
                  
          if (index == id.ordinal()) {

                      
          if (index != count)
                          index
          ++;
                      
          else
                          index 
          = 0;

                      
          return true;
                  }

                  
          return false;
              }

          }


          迅雷面試題以上為我的其中一道題的個人實現
          http://www.javaeye.com/topic/534019
          posted on 2009-12-02 06:34 李敏 閱讀(381) 評論(0)  編輯  收藏 所屬分類: 算法
           
          Copyright © 李敏 Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 康平县| 沈丘县| 宕昌县| 黄石市| 郎溪县| 琼结县| 阿荣旗| 清新县| 威信县| 阿城市| 石城县| 汕尾市| 赫章县| 多伦县| 南昌市| 昆山市| 锡林郭勒盟| 永顺县| 安义县| 资溪县| 翁牛特旗| 黄陵县| 兴业县| 舒城县| 巨野县| 沈阳市| 宕昌县| 来安县| 台江县| 永康市| 大新县| 兰坪| 石门县| 体育| 康马县| 乌苏市| 云和县| 固始县| 屏山县| 平安县| 军事|