Java多線程下解決數(shù)據(jù)安全問(wèn)題
同步代碼塊
基本語(yǔ)句
synchronized (任意對(duì)象) {
操作共享代碼
}
代碼示例
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();
}
}
優(yōu)缺點(diǎn):
- 解決了多線程的數(shù)據(jù)安全問(wèn)題
- 多線程時(shí),每個(gè)線程都會(huì)判斷同步上的鎖,耗費(fèi)資源,降低了程序的運(yùn)行效率
同步方法
同步方法:將synchronized關(guān)鍵字加到方法上
- 格式: 修飾符 synchronized 返回值類型 方法名(){ }
- 同步方法的鎖對(duì)象是this
同步靜態(tài)方法,就是把synchronized關(guān)鍵字加到靜態(tài)方法上
- 格式: 修飾符 static synchronized 返回值類型 方法名(){ }
- 同步靜態(tài)方法的鎖對(duì)象是 類名.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實(shí)現(xiàn)提供比使用synchronized方法和語(yǔ)句可獲得更廣泛的操作
- void lock()獲得鎖
- void unlock()釋放
lock是接口不能直接實(shí)例化,采用實(shí)現(xiàn)類實(shí)例化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();
}
到此這篇關(guān)于Java多線程下解決數(shù)據(jù)安全問(wèn)題的文章就介紹到這了,更多相關(guān)java多線程數(shù)據(jù)安全內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot統(tǒng)一返回json數(shù)據(jù)格式并配置系統(tǒng)異常攔截方式
這篇文章主要介紹了springboot統(tǒng)一返回json數(shù)據(jù)格式并配置系統(tǒng)異常攔截方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Java實(shí)現(xiàn)批量化操作Excel文件的示例代碼
在操作Excel的場(chǎng)景中,通常會(huì)有一些針對(duì)Excel的批量操作,這篇文章主要為大家詳細(xì)介紹了如何使用GcExcel實(shí)現(xiàn)批量化操作Excel,感興趣的可以了解一下2024-12-12
java鎖機(jī)制ReentrantLock源碼實(shí)例分析
這篇文章主要為大家介紹了java鎖機(jī)制ReentrantLock源碼實(shí)例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
解決springboot 2.x集成log4j2調(diào)試日志無(wú)法關(guān)閉的問(wèn)題
這篇文章主要介紹了解決springboot 2.x集成log4j2調(diào)試日志無(wú)法關(guān)閉的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Java中定時(shí)器Timer致命缺點(diǎn)案例詳解
這篇文章主要介紹了Java中定時(shí)器Timer致命缺點(diǎn),以Java中定時(shí)器Time為案例整理下我的學(xué)習(xí)方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02
Mybatis-plus配置分頁(yè)插件返回統(tǒng)一結(jié)果集
本文主要介紹了Mybatis-plus配置分頁(yè)插件返回統(tǒng)一結(jié)果集,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06

