解决线程安全的方式

1. 同步代码块

 1 package cn.itcast.day_05.test_02;
 2 
 3 public class RunnableImpl implements Runnable{
 4 
 5     private int tickets = 100;
 6 
 7     //创建对象
 8     Object obj = new Object();
 9 
10     //设置线程任务
11     @Override
12     public void run() {
13         //票存在,卖票
14         while (true){
15             synchronized (obj){  //同步代码块
16                 if (tickets > 0){
17                     //提高安全性
18                     try {
19                         Thread.sleep(1000);
20                     } catch (InterruptedException e) {
21                         e.printStackTrace();
22                     }
23                     System.out.println(Thread.currentThread().getName()+"正在卖第"+tickets+"张票");
24                     tickets--;
25                 }
26             }
27         }
28     }
29 }

2. 同步方法

 1 package cn.itcast.day_05.test_02;
 2 
 3 public class RunnableImpl2 implements Runnable{
 4 
 5     private int tickets = 100;
 6 
 7     @Override
 8     public void run() {
 9         while (true){
10             payTickets();
11         }
12     }
13 
14     //静态同步方法
15     public synchronized void payTickets(){
16         if (tickets > 0){
17             try {
18                 Thread.sleep(1000);
19             } catch (InterruptedException e) {
20                 e.printStackTrace();
21             }
22             System.out.println(Thread.currentThread().getName()+"正在卖第"+tickets+"张票");
23             tickets--;
24         }
25     }
26 }

3. 静态同步方法

 1 package cn.itcast.day_05.test_02;
 2 
 3 public class RunnableImpl2 implements Runnable{
 4 
 5     private static int tickets = 100;
 6 
 7     @Override
 8     public void run() {
 9         while (true){
10             payTickets();
11         }
12     }
13 
14     //静态同步方法
15     public static synchronized void payTickets(){
16         if (tickets > 0){
17             try {
18                 Thread.sleep(1000);
19             } catch (InterruptedException e) {
20                 e.printStackTrace();
21             }
22             System.out.println(Thread.currentThread().getName()+"正在卖第"+tickets+"张票");
23             tickets--;
24         }
25     }
26 }

 4. Lock锁

 1 package cn.itcast.day_05.test_02;
 2 
 3 import java.util.concurrent.locks.Lock;
 4 import java.util.concurrent.locks.ReentrantLock;
 5 
 6 public class RunnableImpl3 implements Runnable{
 7 
 8     private int tickets = 100;
 9 
10     //创建ReentrantLock对象
11     Lock i = new ReentrantLock();
12 
13     @Override
14     public void run() {
15         while (true){
16 
17             //加锁
18             i.lock();
19 
20             if (tickets > 0){
21                 try {
22                     Thread.sleep(1000);
23                     System.out.println(Thread.currentThread().getName()+"正在卖第"+tickets+"张票");
24                     tickets--;
25                 } catch (InterruptedException e) {
26                     e.printStackTrace();
27                 }finally {   //无论程序是否异常,都会释放锁
28                     //解锁
29                     i.unlock();
30                 }
31             }
32         }
33     }
34 }

 

 

 

posted @ 2020-05-27 15:56  不溶解的糖  阅读(157)  评论(0编辑  收藏  举报