Java多线程下解决数据安全问题
作者:季沐 时间:2022-05-16 04:48:45
同步代码块
基本语句
synchronized (任意对象) {
操作共享代码
}
代码示例
public class SellTicket implements Runnable {
private int tickets = 100;
private Object object = new Object();
@Override
public void run() {
while (true) {
synchronized (object) {
if (tickets > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "正在出售第" + tickets + "张票");
tickets--;
}
}
}
}
public static void main(String[] args) {
SellTicket sellTicket = new SellTicket();
Thread thread1 = new Thread(sellTicket, "窗口1");
Thread thread2 = new Thread(sellTicket, "窗口2");
Thread thread3 = new Thread(sellTicket, "窗口3");
thread1.start();
thread2.start();
thread3.start();
}
}
优缺点:
解决了多线程的数据安全问题
多线程时,每个线程都会判断同步上的锁,耗费资源,降低了程序的运行效率
同步方法
同步方法:将synchronized关键字加到方法上
格式: 修饰符 synchronized 返回值类型 方法名(){ }
同步方法的锁对象是this
同步静态方法,就是把synchronized关键字加到静态方法上
格式: 修饰符 static synchronized 返回值类型 方法名(){ }
同步静态方法的锁对象是 类名.class
代码示例
public class SellTicket implements Runnable {
// private int tickets = 100;
private static int tickets = 100;
private Object object = new Object();
private int x = 0;
@Override
public void run() {
while (true) {
if (x % 2 == 0) {
// synchronized (object) {
// synchronized (this) {
synchronized (SellTicket.class) {
if (tickets > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
} System.out.println(Thread.currentThread().getName() + "正在出售第" + tickets + "张票");
tickets--;
}
}
} else {
// synchronized (object) {
// if (tickets > 0) {
// try {
// Thread.sleep(100);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// System.out.println(Thread.currentThread().getName() + "正在出售第" + tickets + "张票");
// tickets--;
// }
// }
sellTicket();
}
x++;
}
}
// private void sellTicket(){
// synchronized (object) {
// if (tickets > 0) {
// try {
// Thread.sleep(100);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// System.out.println(Thread.currentThread().getName() + "正在出售第" + tickets + "张票");
// tickets--;
// }
// }
// }
// private synchronized void sellTicket(){
// if (tickets > 0) {
// try {
// Thread.sleep(100);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// System.out.println(Thread.currentThread().getName() + "正在出售第" + tickets + "张票");
// tickets--;
// }
private static synchronized void sellTicket(){
if (tickets > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "正在出售第" + tickets + "张票");
tickets--;
}
}
public static void main(String[] args) {
SellTicket sellTicket = new SellTicket();
Thread thread1 = new Thread(sellTicket, "窗口1");
Thread thread2 = new Thread(sellTicket, "窗口2");
Thread thread3 = new Thread(sellTicket, "窗口3");
thread1.start();
thread2.start();
thread3.start();
}
}
lock锁
lock实现提供比使用synchronized方法和语句可获得更广泛的操作
void lock()获得锁
void unlock()释放
lock是接口不能直接实例化,采用实现类实例化ReentrantLock
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class SellTicket implements Runnable {
private int tickets = 100;
private Object object = new Object();
private Lock lock = new ReentrantLock();
@Override
public void run() {
while (true) {
try {
lock.lock();
if (tickets > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "正在出售第" + tickets + "张票");
tickets--;
}
} finally {
lock.unlock();
}
}
}
public static void main(String[] args) {
SellTicket sellTicket = new SellTicket();
Thread thread1 = new Thread(sellTicket, "窗口1");
Thread thread2 = new Thread(sellTicket, "窗口2");
Thread thread3 = new Thread(sellTicket, "窗口3");
thread1.start();
thread2.start();
thread3.start();
}
来源:https://www.cnblogs.com/testero/p/15135598.html
标签:Java,多线程,安全
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java实现Excel文件转PDF(无水印无限制)
2023-10-28 11:33:18
![](https://img.aspxhome.com/file/2023/5/58535_0s.png)
Java查找不重复无序数组中是否存在两个数字的和为某个值
2023-08-22 16:44:40
重写hashCode()和equals()方法详细介绍
2023-11-24 16:13:33
![](https://img.aspxhome.com/file/2023/8/59198_0s.png)
Android编程基础之获取手机屏幕大小(DisplayMetrics应用)示例
2023-09-26 17:57:43
![](https://img.aspxhome.com/file/2023/3/85983_0s.jpg)
SpringBoot JS-SDK自定义微信分享的实现
2023-11-10 05:08:28
![](https://img.aspxhome.com/file/2023/6/59256_0s.jpg)
Java Swing JTextArea文本区域的实现示例
2023-10-30 13:40:28
![](https://img.aspxhome.com/file/2023/6/58606_0s.png)
新手小白学JAVA IDEA下载使用手册全集
2023-11-23 14:33:23
![](https://img.aspxhome.com/file/2023/3/59773_0s.jpg)
如何将IDEA打成jar包并在windows后台运行
2022-02-27 01:36:57
![](https://img.aspxhome.com/file/2023/6/61546_0s.png)
浅谈Java之Map 按值排序 (Map sort by value)
2021-06-20 01:23:10
利用JavaMail发送HTML模板邮件
2023-01-21 02:25:32
详解Mybatis通用Mapper介绍与使用
2023-11-29 08:49:08
java实现航空用户管理系统
2023-11-24 02:42:37
![](https://img.aspxhome.com/file/2023/9/59589_0s.png)
浅谈Java 继承接口同名函数问题
2023-07-22 13:28:47
![](https://img.aspxhome.com/file/2023/0/57720_0s.jpg)
java数据结构之java实现栈
2023-11-25 05:32:36
Android实现登录注册功能
2023-07-31 09:35:44
![](https://img.aspxhome.com/file/2023/1/84741_0s.jpg)
Android WebView无法弹出软键盘的原因及解决办法
2023-07-07 18:30:00
MyBatis源码解析——获取SqlSessionFactory方式
2023-04-03 15:09:14
![](https://img.aspxhome.com/file/2023/2/62082_0s.png)
使用java技术抓取网站上彩票双色球信息详解
2022-01-04 14:47:51
![](https://img.aspxhome.com/file/2023/1/60111_0s.jpg)
Java程序图形用户界面设计之按钮与布局
2023-07-18 07:03:21
![](https://img.aspxhome.com/file/2023/2/61572_0s.png)
Spring Boot 2.5.0 重新设计的spring.sql.init 配置有啥用
2021-12-04 00:04:43