李敏  
          日歷
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345
          統(tǒng)計(jì)
          • 隨筆 - 1
          • 文章 - 40
          • 評(píng)論 - 4
          • 引用 - 0

          導(dǎo)航

          常用鏈接

          留言簿(1)

          文章分類

          文章檔案

          相冊

          收藏夾

          它山之石

          聚賢莊

          搜索

          •  

          最新評(píng)論

           

           

          /**
           * 有三個(gè)線程ID分別是A、B、C,請(qǐng)用多線編程實(shí)現(xiàn),在屏幕上循環(huán)打印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;
              }

          }


          迅雷面試題以上為我的其中一道題的個(gè)人實(shí)現(xiàn)
          http://www.javaeye.com/topic/534019
          posted on 2009-12-02 06:34 李敏 閱讀(381) 評(píng)論(0)  編輯  收藏 所屬分類: 算法
           
          Copyright © 李敏 Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 博爱县| 冕宁县| 邵武市| 江口县| 兴安盟| 集安市| 乐亭县| 南乐县| 普兰县| 顺义区| 嫩江县| 界首市| 定南县| 南乐县| 德安县| 凤凰县| 东山县| 枝江市| 延津县| 江陵县| 阿城市| 新河县| 色达县| 土默特右旗| 鄂州市| 墨江| 监利县| 全南县| 太白县| 兰溪市| 深泽县| 绿春县| 伊金霍洛旗| 阿坝县| 屏东县| 舟山市| 肥西县| 南靖县| 乐安县| 全椒县| 沂水县|