溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

J2EE線程的示例代碼

發(fā)布時間:2021-12-18 13:38:11 來源:億速云 閱讀:119 作者:小新 欄目:編程語言

這篇文章將為大家詳細講解有關(guān)J2EE線程的示例代碼,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

J2EE線程:

public class TT implements Runnable {   int b = 100;      public synchronized void m1() throws Exception{    //Thread.sleep(2000);    b = 1000;    Thread.sleep(5000);    System.out.println("b = " + b);   }      public synchronized void m2() throws Exception {    Thread.sleep(2500);    b = 2000;    System.out.println( b);   }      public void run() {    try {     m1();    } catch(Exception e) {     e.printStackTrace();    }   }      public static void main(String[] args) throws Exception {    TT tt = new TT();    Thread t = new Thread(tt);    t.start();        tt.m2();    System.out.println(tt.b);   }  }

函數(shù)運行結(jié)果:2000
            2000
             b = 1000

函數(shù)分析:在main函數(shù)中,start了一個線程,即運行了該線程的run方法,run方法中要調(diào)用m1方法,而同時在main主線程中調(diào)用了m2的方法,注意此時m2鎖定了對象,因此即使m2中有sleep方法,同樣也要等m2結(jié)束后m1線程才能運行。

關(guān)于“J2EE線程的示例代碼”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI