A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

圣诞老人在睡觉,他只能被以下的两种情况之一唤醒:(1)所有的九头驯鹿全部回来了,(2)某些孩子做玩具的时候遇到了困难,为了让圣诞老人多睡一会,只有三个以上的孩子遇到困难以后才会唤醒。当这三个孩子的问题得到解决时,其它的孩子必须等到那些孩子返回,才可以继续提问。如果圣诞老人醒来以后发现有三个孩子在等待,但最后一头驯鹿也回来了,这时圣诞老人准备让小孩子等待到他为所有的驯鹿都帮好雪撬以后,因为他认为准备雪撬比小孩子做玩具要重要一点。(用信号量编程完成这个程序)
  1. public class Christmas {

  2.         public static void main(String[] args) {
  3.                 new Christmas();
  4.         }

  5.        
  6.         // 圣诞老人的信号量
  7.         OldMan[] ooMans = new OldMan[1];
  8.         private Semaphore        oldman                = new Semaphore(0);
  9.         Semaphore oldworkSemaphore = new Semaphore(1);
  10.        
  11.        
  12.         // 小孩的信号量
  13.         private Semaphore        child                = new Semaphore(3);
  14.         Semaphore chSemaphore = new Semaphore(1);
  15.        
  16.         // 鹿的信号量
  17.         private Semaphore        deer                = new Semaphore(9);
  18.         Semaphore deSemaphore = new Semaphore(1);

  19.         // 小孩的计数器
  20.         private int                        countchild        = 0;
  21.         // 鹿的计数器
  22.         private int                        countdeer        = 0;

  23.         // 小孩子list
  24.         List<Child>                        listC                = new ArrayList<Child>();
  25.         // 鹿数组
  26.         Deer[]                                deers                = new Deer[]{new Deer(1),new Deer(1),new Deer(2),new Deer(3),new Deer(4),
  27.                
  28.                                                     new Deer(5),new Deer(6),new Deer(7),new Deer(8),new Deer(9)};

  29.         public Christmas() {
  30.                
  31.        
  32.                
  33.                 for (int i = 0; i < ooMans.length; i++) {
  34.                           ooMans[i] = new OldMan(i);
  35.                           ooMans[i].start();
  36.                 }
  37.                
  38.                
  39.                 Timer timer = new Timer();
  40.                 timer.schedule(new Mtimer(), 0, 5000);
  41.                
  42.                 for (int i = 1; i<1000; i++) {
  43.                         try {
  44.                                 Thread.sleep(1000);
  45.                         } catch (InterruptedException e) {
  46.                         }
  47.                         Child c = new Child(i);
  48.                         // listC.add(c);
  49.                         c.start();
  50.                 }
  51.                
  52.         }

  53.         class Mtimer extends TimerTask{
  54.                 public void run() {
  55.                         for (int i = 1; i < deers.length; i++) {
  56.                                 try {
  57.                                         Thread.sleep(200);
  58.                                 } catch (InterruptedException e) {
  59.                                 }
  60.                                 deers[i] = new Deer(i);
  61.                                 deers[i].start();
  62.                         }
  63.                 }}
  64.        
  65.        
  66.         // 圣诞老人
  67.         class OldMan extends Thread {
  68.                
  69.                 Child childcChild = null;
  70.                 Deer deerdDeer = null;
  71.                
  72.                 int oldmanId = 0 ;
  73.                
  74.                 public volatile String        state = "空闲";

  75.                 public OldMan(int i) {
  76.                         this.oldmanId = i;
  77.                 }
  78.                
  79.                 public void run() {
  80.                         while (true) {
  81.                                 try {
  82.                                         //圣诞老人在睡觉 等待被 唤醒
  83.                                         oldman.acquire();
  84.                                         checkstate();
  85.                                         oldworkSemaphore.acquire();
  86.                                         System.out.println("圣诞老人被叫醒了,看看是谁叫醒了他!");
  87.                                         oldWork();
  88.                                         oldworkSemaphore.release();
  89.                                        
  90.                                 } catch (InterruptedException e) {
  91.                                         // TODO Auto-generated catch block
  92.                                         e.printStackTrace();
  93.                                 }
  94.                         }
  95.                 }
  96.                
  97.                 //圣诞老人的工作方法
  98.                 public void oldWork() {
  99.                                 //先判读鹿 在判读孩子 鹿比孩子重要
  100.                                 if (countdeer == 9) {
  101.                                         System.out.println("9头驯鹿叫醒了圣诞老人!");
  102.                                         System.out.println("鹿都回来了,绑雪橇!");
  103.                                         countdeer = 0 ;
  104.                                         deer.release(9);
  105.                                         System.out.println("绑好雪橇去休息了!");
  106.                                         state = "空闲";
  107.                                         deerdDeer.state = "出去";
  108.                                 } else if (countchild == 3){
  109.                                         System.out.println("3个孩子叫醒了圣诞老人!");
  110.                                         System.out.println("有3个孩子有困难,帮孩子解决问题");
  111.                                         countchild = 0 ;
  112.                                         child.release(3);
  113.                                         System.out.println("解决完孩子们的困难去休息了!");
  114.                                         state = "空闲";
  115.                                         childcChild.state = "有困难,找圣诞老人!";
  116.                                 }
  117.                 }
  118.                
  119.                 //判断圣诞老人的状态   
  120.                 private void checkstate(){
  121.                         while(!"繁忙".equals(state)){
  122.                                 //如果是满的状态就卧倒,让出执行权 ,线程并没有销毁 ,先进栈的会先出栈
  123.                                 yield();
  124.                         }
  125.                 }

  126.                 public void setchild(Child child) {
  127.                         this.childcChild = child;
  128.                 }

  129.                 public void setdeer(Deer deer) {
  130.                         this.deerdDeer = deer;
  131.                 }
  132.                
  133.         }

  134.         // 孩子
  135.         class Child extends Thread {
  136.                
  137.                 OldMan ooMan;
  138.                
  139.                 int childID ;
  140.                
  141.                 public volatile String        state = "玩玩具";
  142. //                public volatile String        state = "有困难,找圣诞老人!";
  143.                
  144.                 public Child(int id) {
  145.                         this.childID = id;
  146.                 }
  147.                
  148.                 public void run() {
  149.                         try {
  150.                                 //检查孩子自身的初始状态
  151.                                 checkstate2();
  152.                                 //来了一个孩子  孩子的信号量就减一
  153.                                 child.acquire();
  154.                                 //同时孩子的计数器就加一
  155.                 countchild++;
  156.                 System.out.println(childID+"号孩子有困难!");
  157.                 //如果有3个孩子   就叫醒圣诞老人
  158.                 chSemaphore.acquire();
  159.                 if (countchild == 3){
  160.                         checkstate();
  161.                         oldman.release();
  162.                 }
  163.                 chSemaphore.release();
  164.                                
  165.                         } catch (InterruptedException e) {
  166.                                 // TODO Auto-generated catch block
  167.                                 e.printStackTrace();
  168.                         }
  169.                 }
  170.                
  171.                 //判断圣诞老人处于什么状态    空闲了就取得圣诞老人
  172.                 private void checkstate() {
  173.                
  174.                 OldMan oldMan = null;
  175.                
  176.                 //凑齐3个孩子    去取得圣诞老人   
  177.                 AA: while (true) {
  178.                         for (int i = 0; i < ooMans.length; i++) {
  179.                                   if (ooMans[i].state == "空闲") {
  180.                                           oldMan = ooMans[i] ;
  181.                                           break AA;
  182.                         }
  183.                         }
  184.                 }
  185.                
  186.                 oldMan.setchild(this);
  187.                 this.setold(oldMan);
  188.                 oldMan.state = "繁忙";
  189.                 }

  190.                 private void setold(OldMan oldMan) {
  191.                this.ooMan = oldMan;                       
  192.                 }
  193.                
  194.                 //孩子的初始状态
  195.                 private void checkstate2(){
  196.                         try {
  197.                                 System.out.println(childID+"号玩玩具!");
  198.                                 sleep(500);
  199.                         } catch (InterruptedException e) {
  200.                                 // TODO Auto-generated catch block
  201.                                 e.printStackTrace();
  202.                         }
  203.                        
  204.                         state = "有困难,找圣诞老人!";
  205.                        
  206.                         while(!"有困难,找圣诞老人!".equals(state)){
  207.                                 //如果是满的状态就卧倒,让出执行权 ,线程并没有销毁 ,先进栈的会先出栈
  208.                                 yield();
  209.                         }
  210.                 }
  211.                
  212.         }

  213.         // 驯鹿
  214.         class Deer extends Thread {

  215.                 OldMan oooMan;
  216.                
  217.                 public int        deerid;

  218.                 public volatile String        state = "出去";
  219.                
  220.                 public Deer(int id) {
  221.                         this.deerid = id;
  222.                 }

  223.                 public void run() {
  224.                        
  225. //                        while (true) {
  226.                                 try {
  227.                                         //回来一只鹿   鹿的信号量就减一
  228.                                         deer.acquire();
  229.                                        
  230.                                         //鹿的计数器就加一
  231.                                         deSemaphore.acquire();
  232.                                         countdeer++;
  233.                                         System.out.println(deerid+"号鹿"+"回来了");
  234.                                        
  235.                                         //如果9只鹿都回来了就 叫醒圣诞老人
  236.                                         if (countdeer == 9 ){
  237.                                                 state = "都回来了";
  238.                                                 checkstate();
  239.                                                 oldman.release();
  240.                                         }
  241. //                                        else {
  242. //                                                yield();
  243. //                                        }
  244.                                         deSemaphore.release();
  245.                                        
  246.                                 } catch (InterruptedException e) {
  247.                                         // TODO Auto-generated catch block
  248.                                         e.printStackTrace();
  249.                                 }
  250.                                
  251. //                        }
  252.                 }
  253.                
  254.                 //判断圣诞老人处于什么状态    空闲了就取得圣诞老人
  255.                 private void checkstate() {
  256.                
  257.                 OldMan oldMan = null;
  258.                
  259. //                //判断所有的鹿都回来了没有
  260. //                if("出去".equals(state)){
  261. //                        //如果是满的状态就卧倒,让出执行权 ,线程并没有销毁 ,先进栈的会先出栈
  262. //                        yield();
  263. //                }
  264.                
  265.                 //凑齐9只鹿    判断圣诞老人是不是空闲的状态   
  266.                 AA: while (true) {
  267.                         for (int i = 0; i < ooMans.length; i++) {
  268.                                   if (ooMans[i].state == "空闲") {
  269.                                           oldMan = ooMans[i] ;
  270.                                           break AA;
  271.                         }
  272.                         }
  273.                 }
  274.                
  275.                 oldMan.setdeer(this);
  276.                 this.setold(oldMan);
  277.                 oldMan.state = "繁忙";
  278.                 }
  279.                
  280.                 private void setold(OldMan oldMan) {
  281.                         this.oooMan = oldMan;
  282.                 }
  283.         }

  284. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马