• 解决线程安全的方式


    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 }

     

  • 相关阅读:
    Unity 游戏性能优化 学习
    常用网站
    Unity3d插件开发与SDK对接实战 学习
    Unity3D常用网络框架与实战解析 学习
    问题:grid卸载后重新安装时ASM磁盘识别不到了
    问题:oracle 12c rac数据库服务器的home目录丢失问题解决2018-06-25 18:30
    Linux启动报:UNEXPECTED INCONSISTENCY: RUN fsck MANUALLY问题解决
    linux下网卡bonding配置(转)
    热扩容LVM形式的/(根)分区(无损增大、缩小LVM分区)
    LVM逻辑卷管理
  • 原文地址:https://www.cnblogs.com/nnnnbbbb1/p/12973631.html
Copyright © 2020-2023  润新知