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 返回值類(lèi)型 方法名(){ }
- 同步方法的鎖對(duì)象是this
同步靜態(tài)方法,就是把synchronized關(guān)鍵字加到靜態(tài)方法上
- 格式: 修飾符 static synchronized 返回值類(lèi)型 方法名(){ }
- 同步靜態(tài)方法的鎖對(duì)象是 類(lèi)名.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)類(lèi)實(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)文章
JVM運(yùn)行時(shí)數(shù)據(jù)區(qū)劃分原理詳解
這篇文章主要介紹了JVM運(yùn)行時(shí)數(shù)據(jù)區(qū)劃分原理詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
聊聊SpringBoot使用Nacos進(jìn)行服務(wù)注冊(cè)發(fā)現(xiàn)與配置管理問(wèn)題
Nacos支持基于DNS和基于RPC的服務(wù)發(fā)現(xiàn)(可以作為springcloud的注冊(cè)中心)、動(dòng)態(tài)配置服務(wù)(可以做配置中心)、動(dòng)態(tài)?DNS?服務(wù)。本文重點(diǎn)給大家介紹SpringBoot使用Nacos進(jìn)行服務(wù)注冊(cè)發(fā)現(xiàn)與配置管理,感興趣的朋友一起看看吧2022-01-01
SpringBoot詳解如何實(shí)現(xiàn)讀寫(xiě)分離
當(dāng)響應(yīng)的瓶頸在數(shù)據(jù)庫(kù)的時(shí)候,就要考慮數(shù)據(jù)庫(kù)的讀寫(xiě)分離,當(dāng)然還可以分庫(kù)分表,那是單表數(shù)據(jù)量特別大,當(dāng)單表數(shù)據(jù)量不是特別大,但是請(qǐng)求量比較大的時(shí)候,就要考慮讀寫(xiě)分離了.具體的話,還是要看自己的業(yè)務(wù)...如果還是很慢,那就要分庫(kù)分表了...我們這篇就簡(jiǎn)單講一下讀寫(xiě)分離2022-05-05
SpringBoot配置Https入門(mén)實(shí)踐
本文主要介紹了SpringBoot配置Https入門(mén)實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
Spring初始化和銷(xiāo)毀的實(shí)現(xiàn)方法
這篇文章主要介紹了Spring初始化和銷(xiāo)毀的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
Spring實(shí)戰(zhàn)之使用Expression接口進(jìn)行表達(dá)式求值操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之使用Expression接口進(jìn)行表達(dá)式求值操作,結(jié)合實(shí)例形式分析了Spring操作Expression接口實(shí)現(xiàn)表達(dá)式運(yùn)算的操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2019-12-12
關(guān)于idea更新到2020.2.3無(wú)法創(chuàng)建web項(xiàng)目原因 library is not specified
這篇文章主要介紹了關(guān)于idea更新到2020.2.3無(wú)法創(chuàng)建web項(xiàng)目原因 library is not specified,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
解決Spring?Security集成knife4j訪問(wèn)接口文檔出現(xiàn)403的問(wèn)題
這篇文章主要給大家介紹了如何解決Spring?Security集成knife4j訪問(wèn)接口文檔出現(xiàn)403的問(wèn)題,文中有詳細(xì)的解決方案,有需要的朋友可以參考閱讀下2023-07-07

