java中的多執行緒程式設計

2021-08-30 00:04:54 字數 3270 閱讀 1373

一、當兩個併發執行緒訪問同乙個物件object中的這個synchronized(this)同步**塊時,乙個時間內只能有乙個執行緒得到執行。另乙個執行緒必須等待當前執行緒執行完這個**塊以後才能執行該**塊。

package ths;

public class thread1 implements runnable

} }

public static void main(string args)

}

結果:a synchronized loop 0

a synchronized loop 1

a synchronized loop 2

a synchronized loop 3

a synchronized loop 4

b synchronized loop 0

b synchronized loop 1

b synchronized loop 2

b synchronized loop 3

b synchronized loop 4

二、然而,當乙個執行緒訪問object的乙個synchronized(this)同步**塊時,另乙個執行緒仍然可以訪問該object中的非synchronized(this)同步**塊。

package ths;

public class thread2

catch (interruptedexception ie)

} }

}public void m4t2()

catch (interruptedexception ie) } }

public static void main(string args)

}, "t1");

thread t2 = new thread(

new runnable()

}, "t2");

t1.start();

t2.start();

}}

結果:

t1 : 4

t2 : 4

t1 : 3

t2 : 3

t1 : 2

t2 : 2

t1 : 1

t2 : 1

t1 : 0

t2 : 0

三、尤其關鍵的是,當乙個執行緒訪問object的乙個synchronized(this)同步**塊時,其他執行緒對object中所有其它synchronized(this)同步**塊的訪問將被阻塞。

package ths;

public class thread2

catch (interruptedexception ie)

} }

}public void m4t2()

catch (interruptedexception ie) }}

} }public static void main(string args)

}, "t1");

thread t2 = new thread(

new runnable()

}, "t2");

t1.start();

t2.start();

}}

結果:

t1 : 4

t2 : 3

t1 : 2

t2 : 1

t1 : 0

t2 : 4

t1 : 3

t2 : 2

t1 : 1

t2 : 0

四、第三個例子同樣適用其它同步**塊。也就是說,當乙個執行緒訪問object的乙個synchronized(this)同步**塊時,它就獲得了這個object的物件鎖。結果,其它執行緒對該object物件所有同步**部分的訪問都被暫時阻塞。

五、以上規則對其它物件鎖同樣適用:

package ths;

public class thread3 catch(interruptedexception ie) } }

private void m4t2() catch(interruptedexception ie) }}

}private void m4t1(inner inner)

}private void m4t2(inner inner)

public static void main(string args)

}, "t1");

thread t2 = new thread(

new runnable()

}, "t2");

t1.start();

t2.start();

}}

結果:

儘管執行緒t1獲得了對inner的物件鎖,但由於執行緒t2訪問的是同乙個inner中的非同步部分。所以兩個執行緒互不干擾。

t1 : inner.m4t1()=4

t2 : inner.m4t2()=4

t1 : inner.m4t1()=3

t2 : inner.m4t2()=3

t1 : inner.m4t1()=2

t2 : inner.m4t2()=2

t1 : inner.m4t1()=1

t2 : inner.m4t2()=1

t1 : inner.m4t1()=0

t2 : inner.m4t2()=0

現在在inner.m4t2()前面加上synchronized:

private synchronized void m4t2()  catch(interruptedexception ie) 

}}

結果:

儘管執行緒t1與t2訪問了同乙個inner物件中兩個毫不相關的部分,但因為t1先獲得了對inner的物件鎖,所以t2對inner.m4t2()的訪問也被阻塞,因為m4t2()是inner中的乙個同步方法。

t1 : inner.m4t1()=4

t1 : inner.m4t1()=3

t1 : inner.m4t1()=2

t1 : inner.m4t1()=1

t1 : inner.m4t1()=0

t2 : inner.m4t2()=4

t2 : inner.m4t2()=3

t2 : inner.m4t2()=2

t2 : inner.m4t2()=1

t2 : inner.m4t2()=0

java中的多執行緒

package testthread 店員從生產者取貨,消費者從店員取貨,店員最多只能存放20個產品,當產品不夠20個需通知生產者生產,超過20個時停止消費 author passenger 店員類 class clerk catch interruptedexception e else 消費產品...

java中的多執行緒

建立執行緒的第一種方法 繼承thread類。步驟 1,定義乙個類繼承thread 2,複寫thread類中的run方法 目的 將自定義 儲存在run方法中,讓執行緒執行 3,呼叫執行緒的start方法,該方法用兩個作用 啟動執行緒,呼叫run方法 多執行緒的特性 隨機性 示例 class demo ...

java 中 的多執行緒

package wait 執行緒同步涉及的 同步控制 wait 使當前執行緒等待,不在爭搶cpu,並釋放同步 塊 或 同步方法的 鎖 notify 喚醒 某乙個 被 wait 的執行緒 notifyall 喚醒所有 被 wait 的執行緒 public class testwait implemen...