• java多线程笔试题:设计4个线程,其中两个线程每次对j增加1,另外2个线程每次对j减少1.写出程序


    分析:线程之间要共享变量(j)

    代码:

     1 /**
     2  * 
     3  */
     4 package bsms.base;
     5 
     6 /**
     7  * @author sunny
     8  *
     9  */
    10 public class Base55 {
    11   private int j;
    12 
    13   /**
    14    * @param args
    15    */
    16   public static void main(String[] args) {
    17     Base55 t = new Base55();
    18     for (int i = 0; i < 2; i++) {
    19       Runnable at = t.new Add();
    20       Runnable st = t.new Sub();
    21       new Thread(at, "Add-" + i).start();
    22       new Thread(st, "Sub-" + i).start();
    23     }
    24   }
    25 
    26   private synchronized void add() {
    27     System.out.println(Thread.currentThread().getName() + ",j=" + ++j);
    28     try {
    29       Thread.sleep(100);
    30     } catch (InterruptedException e) {
    31       // TODO Auto-generated catch block
    32       e.printStackTrace();
    33     }
    34   }
    35 
    36   private synchronized void sub() {
    37     System.out.println(Thread.currentThread().getName() + ",j=" + --j);
    38     try {
    39       Thread.sleep(100);
    40     } catch (InterruptedException e) {
    41       // TODO Auto-generated catch block
    42       e.printStackTrace();
    43     }
    44   }
    45 
    46   private class Add implements Runnable {
    47     @Override
    48     public void run() {      
    49 //      for (int i = 0; i < 100; i++) {
    50 //        add();
    51 //      }
    52       while(true){
    53         add();       
    54       }
    55     }
    56   }
    57 
    58   private class Sub implements Runnable {
    59     @Override
    60     public void run() {
    61 //      for (int i = 0; i < 100; i++) {
    62 //        sub();
    63 //      }
    64       while(true){
    65         sub();
    66       }
    67     }
    68   }
    69 
    70 }

    效果:

      1 Add-0,j=1
      2 Sub-1,j=0
      3 Sub-1,j=-1
      4 Sub-1,j=-2
      5 Sub-1,j=-3
      6 Add-1,j=-2
      7 Add-1,j=-1
      8 Add-1,j=0
      9 Sub-0,j=-1
     10 Add-1,j=0
     11 Add-1,j=1
     12 Sub-1,j=0
     13 Sub-1,j=-1
     14 Sub-1,j=-2
     15 Sub-1,j=-3
     16 Sub-1,j=-4
     17 Sub-1,j=-5
     18 Sub-1,j=-6
     19 Add-0,j=-5
     20 Sub-1,j=-6
     21 Sub-1,j=-7
     22 Sub-1,j=-8
     23 Sub-1,j=-9
     24 Sub-1,j=-10
     25 Sub-1,j=-11
     26 Add-1,j=-10
     27 Add-1,j=-9
     28 Add-1,j=-8
     29 Add-1,j=-7
     30 Sub-0,j=-8
     31 Add-1,j=-7
     32 Add-1,j=-6
     33 Add-1,j=-5
     34 Add-1,j=-4
     35 Add-1,j=-3
     36 Add-1,j=-2
     37 Add-1,j=-1
     38 Add-1,j=0
     39 Add-1,j=1
     40 Add-1,j=2
     41 Add-1,j=3
     42 Add-1,j=4
     43 Add-1,j=5
     44 Add-1,j=6
     45 Add-1,j=7
     46 Add-1,j=8
     47 Add-1,j=9
     48 Add-1,j=10
     49 Add-1,j=11
     50 Add-1,j=12
     51 Add-1,j=13
     52 Add-1,j=14
     53 Add-1,j=15
     54 Add-1,j=16
     55 Add-1,j=17
     56 Add-1,j=18
     57 Add-1,j=19
     58 Sub-1,j=18
     59 Sub-1,j=17
     60 Sub-1,j=16
     61 Sub-1,j=15
     62 Sub-1,j=14
     63 Sub-1,j=13
     64 Sub-1,j=12
     65 Sub-1,j=11
     66 Sub-1,j=10
     67 Sub-1,j=9
     68 Add-0,j=10
     69 Add-0,j=11
     70 Add-0,j=12
     71 Add-0,j=13
     72 Add-0,j=14
     73 Add-0,j=15
     74 Add-0,j=16
     75 Add-0,j=17
     76 Sub-1,j=16
     77 Sub-1,j=15
     78 Sub-1,j=14
     79 Sub-1,j=13
     80 Sub-1,j=12
     81 Sub-1,j=11
     82 Sub-1,j=10
     83 Add-1,j=11
     84 Add-1,j=12
     85 Sub-0,j=11
     86 Add-1,j=12
     87 Add-1,j=13
     88 Add-1,j=14
     89 Add-1,j=15
     90 Sub-1,j=14
     91 Sub-1,j=13
     92 Sub-1,j=12
     93 Sub-1,j=11
     94 Add-0,j=12
     95 Add-0,j=13
     96 Add-0,j=14
     97 Add-0,j=15
     98 Sub-1,j=14
     99 Add-1,j=15
    100 Sub-0,j=14
    101 Sub-0,j=13
    102 Sub-0,j=12
    103 Sub-0,j=11
    104 Sub-0,j=10
    105 Add-1,j=11
    106 Add-1,j=12
    107 Sub-1,j=11
    108 Add-0,j=12
    109 Add-0,j=13
    110 Add-0,j=14
    111 Add-0,j=15
    112 Sub-1,j=14
    113 Sub-1,j=13
    114 Add-1,j=14
    115 Add-1,j=15
    116 Sub-0,j=14
    117 Add-1,j=15
    118 Add-1,j=16
    119 Add-1,j=17
    120 Add-1,j=18
    121 Add-1,j=19
    122 Add-1,j=20
    123 Add-1,j=21
    124 Sub-1,j=20
    125 Sub-1,j=19
    126 Sub-1,j=18
    127 Sub-1,j=17
    128 Sub-1,j=16
    129 Sub-1,j=15
    130 Sub-1,j=14
    131 Sub-1,j=13
    132 Sub-1,j=12
    133 Sub-1,j=11
    134 Sub-1,j=10
    135 Add-0,j=11
    136 Add-0,j=12
    137 Add-0,j=13
    138 Add-0,j=14
    139 Add-0,j=15
    140 Add-0,j=16
    141 Add-0,j=17
    142 Add-0,j=18
    143 Sub-1,j=17
    144 Sub-1,j=16
    145 Sub-1,j=15
    146 Sub-1,j=14
    147 Sub-1,j=13
    148 Sub-1,j=12
    149 Sub-1,j=11
    150 Sub-1,j=10
    151 Sub-1,j=9
    152 Sub-1,j=8
    153 Sub-1,j=7
    154 Sub-1,j=6
    155 Add-1,j=7
    156 Add-1,j=8
    157 Add-1,j=9
    158 Sub-0,j=8
    159 Add-1,j=9
    160 Add-1,j=10
    161 Add-1,j=11
    162 Add-1,j=12
    163 Add-1,j=13
    164 Add-1,j=14
    165 Sub-1,j=13
    166 Sub-1,j=12
    167 Sub-1,j=11
    168 Sub-1,j=10
    169 Sub-1,j=9
    170 Sub-1,j=8
    171 Sub-1,j=7
    172 Sub-1,j=6
    173 Sub-1,j=5
    174 Sub-1,j=4
    175 Add-0,j=5
    176 Add-0,j=6
    177 Add-0,j=7
    178 Add-0,j=8
    179 Add-0,j=9
    180 Add-0,j=10
    181 Add-0,j=11
    182 Sub-1,j=10
    183 Sub-1,j=9
    184 Sub-1,j=8
    185 Sub-1,j=7
    186 Sub-1,j=6
    187 Sub-1,j=5
    188 Sub-1,j=4
    189 Sub-1,j=3
    190 Sub-1,j=2
    191 Sub-1,j=1
    192 Add-1,j=2
    193 Add-1,j=3
    194 Sub-0,j=2
    .......只展示部分
  • 相关阅读:
    8.25
    8.24
    8.23
    8.22
    8.21
    8.20
    8.19
    8.18
    8.17
    8.16
  • 原文地址:https://www.cnblogs.com/sunny08/p/6929558.html
Copyright © 2020-2023  润新知