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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© guowei 中级黑马   /  2016-7-9 05:02  /  3114 人查看  /  1 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

8黑马币
定义4个线程,其中两个线程完成对一个数加1的操作,各加20次, 另外两个线程完成对同一个数减1的操作 各减20次,把每次执行的结果打印(注意同步问题)求代码.....

最佳答案

查看完整内容

随便写写的 0.0

1 个回复

倒序浏览
本帖最后由 cat73 于 2016-7-9 13:22 编辑

随便写写的 0.0
  1. public final class Main {
  2.     public static void main(final String args[]) throws Exception {
  3.         new Main()._main(args);
  4.     }

  5.     private void _main(final String args[]) throws Exception {
  6.         final Int num = new Int();
  7.         new Thread(new IincThread(num, 1, 20, System.out), "+1 Thread 1").start();
  8.         new Thread(new IincThread(num, 1, 20, System.out), "+1 Thread 2").start();
  9.         new Thread(new IincThread(num, -1, 20, System.out), "-1 Thread 1").start();
  10.         new Thread(new IincThread(num, -1, 20, System.out), "-1 Thread 2").start();
  11.     }

  12.     /**
  13.      * 存储一个可变整数的类
  14.      *
  15.      * @author cat73
  16.      */
  17.     class Int {
  18.         public int num = 0;
  19.     }

  20.     /**
  21.      * 对数字执行加减操作的线程
  22.      *
  23.      * @author cat73
  24.      */
  25.     class IincThread implements Runnable {
  26.         private final Int num;
  27.         private final int n;
  28.         private int count;
  29.         private final PrintStream out;

  30.         /**
  31.          * 实例化一个工作线程
  32.          * @param num 要被加减的数字的包装类
  33.          * @param n 每次加减的量
  34.          * @param count 加减的次数
  35.          * @param out 输出详细信息的输出流,如不需要输出可设置为 null
  36.          */
  37.         public IincThread(final Int num, final int n, final int count, final PrintStream out) {
  38.             this.num = num;
  39.             this.n = n;
  40.             this.count = count;
  41.             this.out = out;
  42.         }

  43.         @Override
  44.         public void run() {
  45.             int numBefor, numAfter;
  46.             while (this.count-- > 0) {
  47.                 synchronized (this.num) {
  48.                     numBefor = this.num.num;
  49.                     
  50.                     this.num.num += this.n;
  51.                     
  52.                     numAfter = this.num.num;
  53.                     
  54.                     if(this.out != null) {
  55.                         this.out.println(Thread.currentThread().getName() + ": " + numBefor + " -> " + numAfter);
  56.                     }
  57.                 }
  58.                
  59.                 // 执行完一次后等一会再执行下一次
  60.                 try {
  61.                     Thread.sleep(50);
  62.                 } catch (InterruptedException e) {
  63.                 }
  64.             }
  65.         }
  66.     }
  67. }
复制代码

  1. +1 Thread 1: 0 -> 1
  2. -1 Thread 2: 1 -> 0
  3. +1 Thread 2: 0 -> 1
  4. -1 Thread 1: 1 -> 0
  5. -1 Thread 1: 0 -> -1
  6. +1 Thread 1: -1 -> 0
  7. +1 Thread 2: 0 -> 1
  8. -1 Thread 2: 1 -> 0
  9. -1 Thread 1: 0 -> -1
  10. -1 Thread 2: -1 -> -2
  11. +1 Thread 1: -2 -> -1
  12. +1 Thread 2: -1 -> 0
  13. -1 Thread 1: 0 -> -1
  14. +1 Thread 2: -1 -> 0
  15. -1 Thread 2: 0 -> -1
  16. +1 Thread 1: -1 -> 0
  17. -1 Thread 1: 0 -> -1
  18. -1 Thread 2: -1 -> -2
  19. +1 Thread 2: -2 -> -1
  20. +1 Thread 1: -1 -> 0
  21. -1 Thread 1: 0 -> -1
  22. +1 Thread 1: -1 -> 0
  23. +1 Thread 2: 0 -> 1
  24. -1 Thread 2: 1 -> 0
  25. -1 Thread 1: 0 -> -1
  26. +1 Thread 2: -1 -> 0
  27. -1 Thread 2: 0 -> -1
  28. +1 Thread 1: -1 -> 0
  29. -1 Thread 1: 0 -> -1
  30. +1 Thread 1: -1 -> 0
  31. +1 Thread 2: 0 -> 1
  32. -1 Thread 2: 1 -> 0
  33. -1 Thread 1: 0 -> -1
  34. -1 Thread 2: -1 -> -2
  35. +1 Thread 2: -2 -> -1
  36. +1 Thread 1: -1 -> 0
  37. -1 Thread 1: 0 -> -1
  38. -1 Thread 2: -1 -> -2
  39. +1 Thread 2: -2 -> -1
  40. +1 Thread 1: -1 -> 0
  41. -1 Thread 1: 0 -> -1
  42. +1 Thread 2: -1 -> 0
  43. -1 Thread 2: 0 -> -1
  44. +1 Thread 1: -1 -> 0
  45. -1 Thread 1: 0 -> -1
  46. +1 Thread 1: -1 -> 0
  47. +1 Thread 2: 0 -> 1
  48. -1 Thread 2: 1 -> 0
  49. -1 Thread 1: 0 -> -1
  50. -1 Thread 2: -1 -> -2
  51. +1 Thread 1: -2 -> -1
  52. +1 Thread 2: -1 -> 0
  53. -1 Thread 1: 0 -> -1
  54. +1 Thread 2: -1 -> 0
  55. +1 Thread 1: 0 -> 1
  56. -1 Thread 2: 1 -> 0
  57. -1 Thread 1: 0 -> -1
  58. +1 Thread 1: -1 -> 0
  59. -1 Thread 2: 0 -> -1
  60. +1 Thread 2: -1 -> 0
  61. -1 Thread 1: 0 -> -1
  62. +1 Thread 2: -1 -> 0
  63. +1 Thread 1: 0 -> 1
  64. -1 Thread 2: 1 -> 0
  65. -1 Thread 1: 0 -> -1
  66. +1 Thread 1: -1 -> 0
  67. +1 Thread 2: 0 -> 1
  68. -1 Thread 2: 1 -> 0
  69. -1 Thread 1: 0 -> -1
  70. +1 Thread 2: -1 -> 0
  71. +1 Thread 1: 0 -> 1
  72. -1 Thread 2: 1 -> 0
  73. -1 Thread 1: 0 -> -1
  74. +1 Thread 1: -1 -> 0
  75. +1 Thread 2: 0 -> 1
  76. -1 Thread 2: 1 -> 0
  77. -1 Thread 1: 0 -> -1
  78. +1 Thread 2: -1 -> 0
  79. +1 Thread 1: 0 -> 1
  80. -1 Thread 2: 1 -> 0
复制代码

  1. +1 Thread 1: 0 -> 1
  2. -1 Thread 2: 1 -> 0
  3. -1 Thread 1: 0 -> -1
  4. +1 Thread 2: -1 -> 0
  5. +1 Thread 2: 0 -> 1
  6. +1 Thread 1: 1 -> 2
  7. -1 Thread 2: 2 -> 1
  8. -1 Thread 1: 1 -> 0
  9. +1 Thread 1: 0 -> 1
  10. +1 Thread 2: 1 -> 2
  11. -1 Thread 2: 2 -> 1
  12. -1 Thread 1: 1 -> 0
  13. +1 Thread 2: 0 -> 1
  14. +1 Thread 1: 1 -> 2
  15. -1 Thread 2: 2 -> 1
  16. -1 Thread 1: 1 -> 0
  17. +1 Thread 1: 0 -> 1
  18. +1 Thread 2: 1 -> 2
  19. -1 Thread 2: 2 -> 1
  20. -1 Thread 1: 1 -> 0
  21. +1 Thread 2: 0 -> 1
  22. +1 Thread 1: 1 -> 2
  23. -1 Thread 2: 2 -> 1
  24. -1 Thread 1: 1 -> 0
  25. +1 Thread 1: 0 -> 1
  26. +1 Thread 2: 1 -> 2
  27. -1 Thread 2: 2 -> 1
  28. -1 Thread 1: 1 -> 0
  29. +1 Thread 2: 0 -> 1
  30. -1 Thread 1: 1 -> 0
  31. +1 Thread 1: 0 -> 1
  32. -1 Thread 2: 1 -> 0
  33. +1 Thread 2: 0 -> 1
  34. -1 Thread 2: 1 -> 0
  35. -1 Thread 1: 0 -> -1
  36. +1 Thread 1: -1 -> 0
  37. +1 Thread 2: 0 -> 1
  38. +1 Thread 1: 1 -> 2
  39. -1 Thread 1: 2 -> 1
  40. -1 Thread 2: 1 -> 0
  41. +1 Thread 2: 0 -> 1
  42. -1 Thread 2: 1 -> 0
  43. +1 Thread 1: 0 -> 1
  44. -1 Thread 1: 1 -> 0
  45. +1 Thread 2: 0 -> 1
  46. +1 Thread 1: 1 -> 2
  47. -1 Thread 2: 2 -> 1
  48. -1 Thread 1: 1 -> 0
  49. +1 Thread 2: 0 -> 1
  50. +1 Thread 1: 1 -> 2
  51. -1 Thread 2: 2 -> 1
  52. -1 Thread 1: 1 -> 0
  53. +1 Thread 2: 0 -> 1
  54. +1 Thread 1: 1 -> 2
  55. -1 Thread 2: 2 -> 1
  56. -1 Thread 1: 1 -> 0
  57. +1 Thread 2: 0 -> 1
  58. -1 Thread 2: 1 -> 0
  59. +1 Thread 1: 0 -> 1
  60. -1 Thread 1: 1 -> 0
  61. +1 Thread 2: 0 -> 1
  62. -1 Thread 2: 1 -> 0
  63. +1 Thread 1: 0 -> 1
  64. -1 Thread 1: 1 -> 0
  65. +1 Thread 2: 0 -> 1
  66. -1 Thread 2: 1 -> 0
  67. +1 Thread 1: 0 -> 1
  68. -1 Thread 1: 1 -> 0
  69. +1 Thread 2: 0 -> 1
  70. -1 Thread 2: 1 -> 0
  71. +1 Thread 1: 0 -> 1
  72. -1 Thread 1: 1 -> 0
  73. +1 Thread 2: 0 -> 1
  74. -1 Thread 2: 1 -> 0
  75. +1 Thread 1: 0 -> 1
  76. -1 Thread 1: 1 -> 0
  77. +1 Thread 2: 0 -> 1
  78. -1 Thread 2: 1 -> 0
  79. -1 Thread 1: 0 -> -1
  80. +1 Thread 1: -1 -> 0
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马