這篇“java中有什么特殊monitor”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“java中有什么特殊monitor”文章吧。
說明
1、this monitor:在成員方法上的synchronized,就是this monitor,等價于在方法中使用synchronized(this)
2、class monitor:在靜態方法上的synchronized,就是class monitor,等價于在靜態方法中使用synchronized(XXX.class)
實例
public class Main { public synchronized void method1(){ System.out.println(Thread.currentThread().getName()+" method1"); try{ TimeUnit.MINUTES.sleep(5); }catch (InterruptedException e){ e.printStackTrace(); } } public synchronized void method2(){ System.out.println(Thread.currentThread().getName()+" method2"); try{ TimeUnit.MINUTES.sleep(5); }catch (InterruptedException e){ e.printStackTrace(); } } public static void main(String[] args) throws InterruptedException { Main m = new Main(); new Thread(m::method1).start(); new Thread(m::method2).start(); } }
以上就是關于“java中有什么特殊monitor”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。