一道java多線程筆試上機題

上個星期我到誠邁科技參加面試.面試完畢後面試官讓我把筆試卷上的一道多線程題在計算機上編程實現.題目如下:

四個線程a,b,c,d. 線程a,b對變量i加一. 線程c,d對變量i減去一.四個線程順序執行, 每個線程每次只執行一次.i的初始值爲0, 打印結果0 1 2 1 0 1 2 1 0 1 2...

   這道題還是有一定的難度的. 因爲要求順序執行. 不能簡單用同步.

   經考慮,我決定用一個隊列來對四個線程順序調度.代碼如下:

Java代碼
  1. 1.package org.jenfer.struts2demo.web.struts2.action;     
  2. 2.    
  3. 3.import java.util.concurrent.BlockingQueue;     
  4. 4.import java.util.concurrent.ExecutorService;     
  5. 5.import java.util.concurrent.Executors;     
  6. 6.import java.util.concurrent.LinkedBlockingQueue;     
  7. 7.    
  8. 8./**    
  9. 9. * 四個線程a,b,c,d. 線程a,b對變量i加一.    
  10. 10. * 線程c,d對變量i減去一.四個線程順序執行,    
  11. 11. * 每個線程每次只執行一次.i的初始值爲0,    
  12. 12. * 打印結果0 1 2 1 0 1 2 1 0 1 2...     
  13. 13. *     
  14. 14. * @author 武漢科技大學08級研究生周劍華    
  15. 15. *    
  16. 16. */    
  17. 17.public class MultiThreadTest {     
  18. 18.    //variable i      
  19. 19.    private int i=0;     
  20. 20.    //queue to control thread invoke     
  21. 21.    private BlockingQueue<Integer> queue=new LinkedBlockingQueue<Integer>();     
  22. 22.         
  23. 23.    public MultiThreadTest() {     
  24. 24.        queue.offer(1);     
  25. 25.        queue.offer(2);     
  26. 26.        queue.offer(3);     
  27. 27.        queue.offer(4);     
  28. 28.    }     
  29. 29.         
  30. 30.    public synchronized void inc(int con) throws InterruptedException{     
  31. 31.        while(true){     
  32. 32.            int c=queue.peek();     
  33. 33.            if(c==con){     
  34. 34.                break;     
  35. 35.            }else{     
  36. 36.                notifyAll();     
  37. 37.                wait();     
  38. 38.            }     
  39. 39.        }     
  40. 40.             
  41. 41.        queue.offer(queue.take());     
  42. 42.             
  43. 43.        i++;     
  44. 44.        System.out.println(Thread.currentThread().getName()+",i="+i);     
  45. 45.    }     
  46. 46.         
  47. 47.    public synchronized void dec(int con) throws InterruptedException{     
  48. 48.        while(true){     
  49. 49.            int c=queue.peek();     
  50. 50.            if(c==con){     
  51. 51.                break;     
  52. 52.            }else{     
  53. 53.                notifyAll();     
  54. 54.                wait();     
  55. 55.            }     
  56. 56.        }     
  57. 57.             
  58. 58.        queue.offer(queue.take());     
  59. 59.        i--;     
  60. 60.        System.out.println(Thread.currentThread().getName()+",i="+i);     
  61. 61.    }     
  62. 62.         
  63. 63.    private class IncThread implements Runnable{     
  64. 64.        private int condition;     
  65. 65.        public IncThread(int condition) {     
  66. 66.            this.condition=condition;     
  67. 67.        }     
  68. 68.        public void run() {     
  69. 69.            while(true){     
  70. 70.                try {     
  71. 71.                    inc(condition);     
  72. 72.                } catch (InterruptedException e) {     
  73. 73.                    System.err.println(Thread.currentThread().getName()+" exit now");     
  74. 74.                    break;     
  75. 75.                }     
  76. 76.            }     
  77. 77.        }     
  78. 78.    }     
  79. 79.         
  80. 80.    private class DecThread implements Runnable{     
  81. 81.        private int condition;     
  82. 82.        public DecThread(int condition) {     
  83. 83.            this.condition=condition;     
  84. 84.        }     
  85. 85.        public void run() {     
  86. 86.            while(true){     
  87. 87.                try {     
  88. 88.                    dec(condition);     
  89. 89.                } catch (InterruptedException e) {     
  90. 90.                    System.err.println(Thread.currentThread().getName()+" exit now");     
  91. 91.                    break;     
  92. 92.                }     
  93. 93.            }     
  94. 94.        }     
  95. 95.    }     
  96. 96.         
  97. 97.    public static void main(String[] args) {     
  98. 98.        MultiThreadTest test=new MultiThreadTest();     
  99. 99.        ExecutorService exec=Executors.newFixedThreadPool(4);     
  100. 100.        exec.submit(test.new IncThread(1));     
  101. 101.        exec.submit(test.new IncThread(2));     
  102. 102.        exec.submit(test.new DecThread(3));     
  103. 103.        exec.submit(test.new DecThread(4));     
  104. 104.             
  105. 105.        exec.shutdown();     
  106. 106.    }     
  107. 107.}   

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章