學習Java的同學注意了!!!
學習進程中遇到甚么問題或想獲得學習資源的話,歡迎加入Java學習交換群,群號碼:183993990 我們1起學Java!
F區分
F代碼
/** * Thread sleep和wait區分 * @author DreamSea * 2012⑴⑴5 */ public class ThreadTest implements Runnable { int number = 10; public void firstMethod() throws Exception { synchronized (this) { number += 100; System.out.println(number); } } public void secondMethod() throws Exception { synchronized (this) { /** * (休息2S,阻塞線程) * 以驗證當前線程對象的機鎖被占用時, * 是不是被可以訪問其他同步代碼塊 */ Thread.sleep(2000); //this.wait(2000); number *= 200; } } @Override public void run() { try { firstMethod(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws Exception { ThreadTest threadTest = new ThreadTest(); Thread thread = new Thread(threadTest); thread.start(); threadTest.secondMethod(); } }
學習Java的同學注意了!!!
學習進程中遇到甚么問題或想獲得學習資源的話,歡迎加入Java學習交換群,群號碼:183993990 我們1起學Java!