Java多線(xiàn)程下解決數(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):
解決了多線(xiàn)程的數(shù)據(jù)安全問(wèn)題 多線(xiàn)程時(shí),每個(gè)線(xiàn)程都會(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多線(xiàn)程下解決數(shù)據(jù)安全問(wèn)題的文章就介紹到這了,更多相關(guān)java多線(xiàn)程數(shù)據(jù)安全內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP基礎(chǔ)入門(mén)第三篇(ASP腳本基礎(chǔ))2. PHP循環(huán)與分支知識(shí)點(diǎn)梳理3. 解析原生JS getComputedStyle4. 前端從瀏覽器的渲染到性能優(yōu)化5. 無(wú)線(xiàn)標(biāo)記語(yǔ)言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁(yè)6. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)7. ASP實(shí)現(xiàn)加法驗(yàn)證碼8. 讀大數(shù)據(jù)量的XML文件的讀取問(wèn)題9. css代碼優(yōu)化的12個(gè)技巧10. 利用CSS3新特性創(chuàng)建透明邊框三角
