Java中的线程安全问题

给出一个问题,如下:

解决方案如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class Demo_5 {
 
  public static void main(String[] args) {
   //创建一个窗口
   TicketWindow tw1=new TicketWindow();
 
   //使用三个线程同时启动
   Thread t1=new Thread(tw1);
   Thread t2=new Thread(tw1);
   Thread t3=new Thread(tw1);
   
   t1.start();
   t2.start();
   t3.start();
  }
 
}
 
//售票窗口类
class TicketWindow implements Runnable{
  private int nums=2000;       //一共2000张票
 
  @Override
  public void run() {
   while(true){ 
   
     if(nums>0){      //先判断是否还有票
      //Thread.currentThread().getName()得到当前线程的名字
      System.out.println(Thread.currentThread().getName()+"在售出第"+nums+"张票"); //显示售票信息
     
      //出票的速度是一秒出一张
      try {
       Thread.sleep(1000);
      } catch (InterruptedException e) {
       e.printStackTrace();
      }
     
      nums--;
     }else{
      break;       //售票结束
     }
     
  
  }
}

执行这段代码发现问题,就是同一张票号可能被多个售票窗口出售,惹祸的代码就是if else语句块。

解决方法就是在需要同步的代码段用synchronized(Object){你要同步的代码}即可。

修改后代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class Demo_5 {
 
  public static void main(String[] args) {
   //创建一个窗口
   TicketWindow tw1=new TicketWindow();
 
   //使用三个线程同时启动
   Thread t1=new Thread(tw1);
   Thread t2=new Thread(tw1);
   Thread t3=new Thread(tw1);
   
   t1.start();
   t2.start();
   t3.start();
  }
 
}
 
//售票窗口类
class TicketWindow implements Runnable{
  private int nums=2000;       //一共2000张票
 
  @Override
  public void run() {
   while(true){ 
    //认为if else这段代码要保证其原子性(同步代码块)
    synchronized (this) {
   
     if(nums>0){        //先判断是否还有票
      //Thread.currentThread().getName()得到当前线程的名字
      System.out.println(Thread.currentThread().getName()+"在售出第"+nums+"张票"); //显示售票信息
     
      //出票的速度是一秒出一张
      try {
       Thread.sleep(1000);
      } catch (InterruptedException e) {
       e.printStackTrace();
      }
     
      nums--;
     }else{
      break;       //售票结束
     }
     
    }
   }
  }
}

执行这段代码发现出票正常了。

线程1正执行需要做同步处理的代码,线程2,3,4……blocked,被放入了线程等待池,就好像某人上厕所前先把门关上(上锁),完事之后再出来(解锁),然后别人就可以继续使用了。

猜你喜欢

转载自www.cnblogs.com/wlx6/p/12382500.html