日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

监视器java_Java监视器绑定的超人

發布時間:2023/12/3 java 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 监视器java_Java监视器绑定的超人 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

監視器java

這是超人生活中的黑暗時期。 喬爾·艾爾(Jor-El)希望他繼續航行,為他的最終命運做好準備。 然而,地球面臨著世界末日,正義聯盟需要他們的鋼鐵俠行動來拯救世界。 但是,由于我們只有一個超人,您不能同時做這兩個事情。 同樣,他不能在沒有首先實現自己的命運并實現他的真正力量的情況下與厄運斗爭。 我們該如何呼吁超人,而不是讓這個人為所欲為。 這應該以一種有序的方式進行,其中必須等到航程完成。 我們將利用Java Monitors幫助SuperMan聆聽他的K星之父,并及時回來拯救世界免遭厄運。 首先,我們定義鋼鐵俠。

/*** The awesome kryptonian man is represented by this class* * @author Dinuka Arseculeratne**/ public class SuperMan {private boolean onVoyage = false;/*** Schedule a voyage for Superman. Note that this method first checks whether he is* already on a voyage, and if so calls the wait() method to hault the current thread* until notify is called and onVoyage is set to false.*/public synchronized void goOnVoyage() {if (onVoyage) {try {System.out.println("SuperMan is already on a voyage. Please wait until he returns from his quest.");wait();System.out.println("His goyage is over, time for him to go on a new voyage....");} catch (InterruptedException e) {System.out.println(" I am SuperMan, i do not handle these petty exceptions");}}onVoyage = true;notify();}/*** This method calls Superman back from his current voyage. Again the method* checks whether Super man is not already on a voyage. If so the current thread is* Halted until he is schedule to go on a voyage because he needs to be on a voyage* to be called back in the first place.*/public synchronized void returnFromVoyage() {if (!onVoyage) {try {System.out.println("SuperMan is not yet on a voyage. Please Wait.");wait();System.out.println("Great he has gone on a voyage, time to call him back!!");} catch (InterruptedException e) {System.out.println(" I am SuperMan, i do not handle these petty exceptions");}}onVoyage = false;notify();} }


因此,我們定義了超人。 請注意,他定義了兩種方法。 一個允許他繼續航行,另一個允許他從當前航行回叫。 如您所見,超人不會處理異常,因為……。 他是超人 ,他是
例外 。 您可以看到,在每次調用之前,我們檢查指示他是否在航行中的布爾值,并根據所調用的方法來調用Object的wait(),以暫停當前正在調用該方法的線程,直到通知()由當前在對象上運行的線程調用。 請注意,應在同步方法或塊內調用wait()和notify(),以使其正常工作。 因為您首先需要獲取鎖才能停止或通知它。

回到上一期,我們知道正義聯盟和Jor-El都需要超人,但出于不同的目的。 讓我們看一下下面的代碼片段如何使這場戰斗展開。

public class Test {public static void main(String[] args) {SuperMan superMan = new SuperMan();JusticeLeague justiceLeague = new JusticeLeague(superMan);justiceLeague.start();JorEl jorEl = new JorEl(superMan);jorEl.start();}}class JusticeLeague extends Thread{private SuperMan superMan = null;public JusticeLeague(SuperMan superMan){this.superMan = superMan;}@Overridepublic void run() {superMan.returnFromVoyage();} }class JorEl extends Thread{private SuperMan superMan = null;public JorEl(SuperMan superMan){this.superMan = superMan;}@Overridepublic void run() {superMan.goOnVoyage();}}

請注意,在這里,我們有JorEl和JusticeLeagure在兩個不同的線程上運行,試圖同時訪問SuperMan。 正如您從我們的主要方法中看到的那樣,JusticeLeague希望給超人打電話以拯救世界。 但是幸運的是他還沒有航行,所以要求他返回是違法的。 然后喬勒(JorEl)要求兒子繼續航行,以實現自己的真實命運。 只有在這次航行之后,他才能返回以拯救地球。 如果現在運行此命令,則可以看到JusticeLeague線程已暫停,直到超人繼續航行并調用notify為止。 只是為了好玩,嘗試注釋掉notify()方法,您將看到應用程序掛起,因為現在一個線程將無限期等待,直到通知該過程完成為止。

如果不是Java Monitors,SuperMan將會失敗,因為他將不得不面對世界末日而沒有先行航行并實現自己的命運。 Java再次拯救了世界。 注意:這個故事是虛構的,但Java Monitor是真實的

參考: 《 我的旅程》 IT博客上的JCG合作伙伴 Dinuka Arseculeratne 的Java Monitors約束的超人。

翻譯自: https://www.javacodegeeks.com/2013/04/superman-bound-by-java-monitors.html

監視器java

總結

以上是生活随笔為你收集整理的监视器java_Java监视器绑定的超人的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。