Java多线程的临界资源问题解决方案
作者:戈德里克山谷 发布时间:2021-12-29 07:44:35
标签:Java,多,线程,临界,资源
这篇文章主要介绍了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;
}
来源:https://www.cnblogs.com/ggrrbb/p/12289687.html


猜你喜欢
- 构建可重复读取inputStream的request我们知道,request的inputStream只能被读取一次,多次读取将报错,那么如何
- Feign动态设置header和原理项目中用到了Feign做远程调用, 有部分场景需要动态配置header开始的做法是通过 @Request
- 前言新进阶的程序员可能对async、await用得比较多,却对之前的异步了解甚少。本人就是此类,因此打算回顾学习下异步的进化史。本文主要是回
- RocketMQ生产者发送消息分为三种模式RocketMQ生产者发送消息分为三种模式,分别是同步发送,异步发送和单向发送。单向发送,这个就是
- private InetAddress getBroadcastAddress() throws IOException { WifiMan
- springboot Jpa通用接口,公共方法de 简单使用 pom文件加入jpa这是我的例子使用的依赖。jpa必须当
- 通过代码操作防火墙的方式有两种:一是代码操作修改注册表启用或关闭防火墙;二是直接操作防火墙对象来启用或关闭防火墙。不论哪一种方式,都需要使用
- 本文实例为大家分享了Android实现层叠卡片式banner的具体代码,供大家参考,具体内容如下效果图如下:背景由于公司VIP模块项目需要,
- Android中的Adapter在自定义显示列表时非常有用,比如SimpleAdapter,它的构造函数是:public SimpleAda
- 本文实例讲述了C#使用Ado.net读取Excel表的方法。分享给大家供大家参考。具体分析如下:微软NET提供了一个交互的方法,通过使用AD
- 本文介绍了详解WMI RPC 服务器不可用的解决方案,分享给大家,具体如下:ConnectionOptions connectionOpti
- 昨天写了一篇Redis布隆过滤器相关的命令的文章,今天来说一说springboot中如何简单在代码中使用布隆过滤器吧。目前市面上也有好几种实
- /** * @param h *
- 本文实例为大家分享了Android实现ListView下拉刷新上拉加载更多的具体代码,供大家参考,具体内容如下其实谷歌官方目前已经推出Lis
- 本文为大家分享了Unity制作签名功能的具体代码,供大家参考,具体内容如下前言:项目中需要做一个签名的功能,同时需要两个两个屏幕进行显示,但
- 一.前言这一篇来看看 SpringIOC 里面的一个细节点 , 来简单看看 BeanDefinition 这个对象 , 以及有没有办法对其进
- 每次写批量的时候,都要在网上搜索一下,虽然都做过多次了,但具体的自己还是记不住(汗颜),所以索性今天就记录下来。前期说明:foreach的主
- 本文实例形式展示了C#中异步调用的实现方法,并对其原理进行了较为深入的分析,现以教程的方式分享给大家供大家参考之用。具体如下:首先我们来看一
- 由于经常用到文件处理,便自己封装了下 分享给大家。 包含写入文本 批量删除文件 下载文件 。--可直接使用/// <summary&g
- springMVC是spring的一个模块,专门做web的。SpringMVC请求处理过程:请求发送,根据url-pattern,转发发送给