Java多线程的临界资源问题解决方案

所属分类: 软件编程 / java 阅读数: 41
收藏 0 赞 0 分享

这篇文章主要介绍了Java多线程的临界资源问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

临界资源问题的原因:某一个线程在对临界资源进行访问时,还没来得及完全修改临界资源的值,临界资源就被其他线程拿去访问,导致多个线程访问同一资源。直观表现为打印结果顺序混乱。

解决方法:加锁

静态方法中用类锁,非静态方法中用对象锁。

1.同步代码段:synchronized(){...}

2.同步方法:使用关键字synchronized修饰的方法

3.使用显式同步锁ReentrantLock

锁池描述的即为锁外等待的状态

方法一:同步代码段:synchronized(){...}

public class SourceConflict {
  public static void main(String[] args) {
    //实例化4个售票员,用4个线程模拟4个售票员
    
    Runnable r = () -> {
      while (TicketCenter.restCount > 0) {
        synchronized(" ") {
          if (TicketCenter.restCount <= 0) {
            return;
          }
          System.out.println(Thread.currentThread().getName() + "卖出一张票,剩余" + --TicketCenter.restCount + "张票");
        }
      }
    };
    
    //用4个线程模拟4个售票员
    Thread thread1 = new Thread(r, "thread-1");
    Thread thread2 = new Thread(r, "thread-2");
    Thread thread3 = new Thread(r, "thread-3");
    Thread thread4 = new Thread(r, "thread-4");
    
    //开启线程
    thread1.start();
    thread2.start();
    thread3.start();
    thread4.start();
    
  }  
}

//实现四名售票员共同售票,资源共享,非独立
//Lambda表达式或匿名内部类内部捕获的局部变量必须显式的声明为 final 或实际效果的的 final 类型,而捕获实例或静态变量是没有限制的
class TicketCenter{
  public static int restCount = 100; 
}

方法二:同步方法,即使用关键字synchronized修饰的方法

public class SourceConflict2 {
  public static void main(String[] args) {
    //实例化4个售票员,用4个线程模拟4个售票员
    
    Runnable r = () -> {
      while (TicketCenter.restCount > 0) {
        sellTicket();
      }
    };
    
    //用4个线程模拟4个售票员
    Thread thread1 = new Thread(r, "thread-1");
    Thread thread2 = new Thread(r, "thread-2");
    Thread thread3 = new Thread(r, "thread-3");
    Thread thread4 = new Thread(r, "thread-4");
    
    //开启线程
    thread1.start();
    thread2.start();
    thread3.start();
    thread4.start();
    
  }
  
  private synchronized static void sellTicket() {  
    if (TicketCenter.restCount <= 0) {
      return;
    }
    System.out.println(Thread.currentThread().getName() + "卖出一张票,剩余" + --TicketCenter.restCount + "张票");
  }
}

class TicketCenter{
  public static int restCount = 100; 
}

方法三:使用显式同步锁ReentrantLock

import java.util.concurrent.locks.ReentrantLock;

public class SourceConflict3 {
  public static void main(String[] args) {
    //实例化4个售票员,用4个线程模拟4个售票员
    
    //显式锁
    ReentrantLock lock = new ReentrantLock();
    Runnable r = () -> {
      while (TicketCenter.restCount > 0) {
        lock.lock();
        if (TicketCenter.restCount <= 0) {
          return;
        }
        System.out.println(Thread.currentThread().getName() + "卖出一张票,剩余" + --TicketCenter.restCount + "张票");
        lock.unlock();
      }
    };
    
    //用4个线程模拟4个售票员
    Thread thread1 = new Thread(r, "thread-1");
    Thread thread2 = new Thread(r, "thread-2");
    Thread thread3 = new Thread(r, "thread-3");
    Thread thread4 = new Thread(r, "thread-4");
    
    //开启线程
    thread1.start();
    thread2.start();
    thread3.start();
    thread4.start();
    
  }  
}
class TicketCenter{
  public static int restCount = 100; 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

SpringBoot中使用Ehcache的详细教程

EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
收藏 0 赞 0 分享

在idea 中添加和删除模块Module操作

这篇文章主要介绍了在idea 中添加和删除模块Module操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java spring整合junit操作(有详细的分析过程)

这篇文章主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解JAVA 弱引用

这篇文章主要介绍了 JAVA 弱引用的相关资料,帮助大家更好的理解和学习java引用对象,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

深入了解JAVA 虚引用

这篇文章主要介绍了JAVA 虚引用的相关资料,帮助大家更好的理解和学习JAVA,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解JAVA 强引用

这篇文章主要介绍了JAVA 强引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

java中的按位与(&)用法说明

这篇文章主要介绍了java中的按位与(&)用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

深入了解JAVA 软引用

这篇文章主要介绍了JAVA 软引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

利用MyBatis实现条件查询的方法汇总

这篇文章主要给大家介绍了关于利用MyBatis实现条件查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MyBatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound

这篇文章主要介绍了Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.Pa
收藏 0 赞 0 分享
查看更多