Java同步代碼塊和同步方法原理與應(yīng)用案例詳解
本文實(shí)例講述了Java同步代碼塊和同步方法。分享給大家供大家參考,具體如下:
一 點(diǎn)睛
所謂原子性:一段代碼要么執(zhí)行,要么不執(zhí)行,不存在執(zhí)行一部分被中斷的情況。言外之意是這段代碼就像原子一樣,不可拆分。
同步的含義:多線程在代碼執(zhí)行的關(guān)鍵點(diǎn)上,互通消息,相互協(xié)作,共同把任務(wù)正確的完成。
同步代碼塊語(yǔ)法:
synchronized(對(duì)象) { 需要同步的代碼塊; }
同步方法語(yǔ)法:
訪問(wèn)控制符 synchronized 返回值類型方法名稱(參數(shù)) { 需要同步的代碼; }
二 同步代碼塊完成賣票功能
1 代碼
public class threadSynchronization { public static void main( String[] args ) { TestThread t = new TestThread(); // 啟動(dòng)了四個(gè)線程,實(shí)現(xiàn)資源共享 new Thread( t ).start(); new Thread( t ).start(); new Thread( t ).start(); new Thread( t ).start(); } } class TestThread implements Runnable { private int tickets = 5; @Override public void run() { while( true ) { synchronized( this ) { if( tickets <= 0 ) break; try { Thread.sleep( 100 ); } catch( Exception e ) { e.printStackTrace(); } System.out.println( Thread.currentThread().getName() + "出售票" + tickets ); tickets -= 1; } } } }
2 運(yùn)行
Thread-0出售票5
Thread-3出售票4
Thread-3出售票3
Thread-2出售票2
Thread-2出售票1
三 同步方法完成買票功能
1 代碼
public class threadSynchronization { public static void main( String[] args ) { TestThread t = new TestThread(); // 啟動(dòng)了四個(gè)線程,實(shí)現(xiàn)資源共享的目的 new Thread( t ).start(); new Thread( t ).start(); new Thread( t ).start(); new Thread( t ).start(); } } class TestThread implements Runnable { private int tickets = 5; public void run() { while( tickets > 0 ) { sale(); } } public synchronized void sale() { if( tickets > 0 ) { try { Thread.sleep( 100 ); } catch( Exception e ) { e.printStackTrace(); } System.out.println( Thread.currentThread().getName() + "出售票" + tickets ); tickets -= 1; } } }
2 運(yùn)行
Thread-0出售票5
Thread-0出售票4
Thread-3出售票3
Thread-2出售票2
Thread-1出售票1
更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java進(jìn)程與線程操作技巧總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
詳解springboot?springsecuroty中的注銷和權(quán)限控制問(wèn)題
這篇文章主要介紹了springboot-springsecuroty?注銷和權(quán)限控制,賬戶注銷需要在SecurityConfig中加入開(kāi)啟注銷功能的代碼,權(quán)限控制要導(dǎo)入springsecurity和thymeleaf的整合依賴,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-03-03Java?AQS?線程安全同步隊(duì)列的實(shí)現(xiàn)
AQS 同步隊(duì)列是很多的 Java 線程安全對(duì)象的實(shí)現(xiàn),例如 ReentrantLock, Semaphore, CountDownLatch, ReentrantReadWriteLock 等等,本文就介紹了Java?AQS?線程安全同步隊(duì)列的實(shí)現(xiàn),感興趣的可以了解一下2023-08-08Java生成遞增流水號(hào)(編號(hào)+時(shí)間+流水號(hào))簡(jiǎn)單示例
這篇文章主要給大家介紹了關(guān)于Java生成遞增流水號(hào)(編號(hào)+時(shí)間+流水號(hào))的相關(guān)資料,在開(kāi)發(fā)項(xiàng)目漫長(zhǎng)的過(guò)程中常常會(huì)遇到流水號(hào)需要自動(dòng)生成的問(wèn)題存在,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下2023-07-07搭建maven私有倉(cāng)庫(kù)的方法實(shí)現(xiàn)
Maven是一個(gè)流行的Java項(xiàng)目管理工具,它可以幫助我們管理項(xiàng)目的構(gòu)建、報(bào)告和文檔,本文主要介紹了搭建maven私有倉(cāng)庫(kù)的方法實(shí)現(xiàn),感興趣的可以了解一下2023-05-05MybatisPlus的MetaObjectHandler與@TableLogic使用
這篇文章主要介紹了MybatisPlus的MetaObjectHandler與@TableLogic使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04Spring事務(wù)傳播中嵌套調(diào)用實(shí)現(xiàn)方法詳細(xì)介紹
Spring事務(wù)的本質(zhì)就是對(duì)數(shù)據(jù)庫(kù)事務(wù)的支持,沒(méi)有數(shù)據(jù)庫(kù)事務(wù),Spring是無(wú)法提供事務(wù)功能的。Spring只提供統(tǒng)一的事務(wù)管理接口,具體實(shí)現(xiàn)都是由數(shù)據(jù)庫(kù)自己實(shí)現(xiàn)的,Spring會(huì)在事務(wù)開(kāi)始時(shí),根據(jù)當(dāng)前設(shè)置的隔離級(jí)別,調(diào)整數(shù)據(jù)庫(kù)的隔離級(jí)別,由此保持一致2022-11-11