Java---17---多線程之間的通訊,java---17---多線程

來源:互聯網
上載者:User

Java---17---多線程之間的通訊,java---17---多線程

   在前面我們已經介紹了多線程編程中使用同步機制的重要性,並學會了如何?同步的方法來正確的訪問共用資源。這些線程之間的關係是平等的,批次之間並不存在依賴,他們各自競爭CPU的資源,互不相讓,並且還無條件的阻止其他線程對共用資源的訪問。然而,也有很多現實問題要求不僅僅要同步的訪問同一共用資源,而且線程間還彼此牽制,通過相互連信來向前推進。那麼,多個線程之間是如何進行通訊的呢?

線程間的通訊其實就是多個線程在操作同一個資源,但操作的動作不同。


class Res {String name;String sex;}class Input implements Runnable {private Res r;public Input(Res r) {// TODO Auto-generated constructor stubthis.r = r;}public void run() {int x = 0;while (true) {if (x == 0) {r.name = "mike";r.sex = "man";} else {r.name = "麗麗";r.sex = "女女女";}x = ++x % 2;}}}class Output implements Runnable {private Res r;Output(Res r) {this.r = r;}public void run() {while (true) {System.out.println(r.name + "----" + r.sex);}}}public class Communicate {public static void main(String[] args) {// TODO Auto-generated method stubRes r = new Res();Input in = new Input(r);Output out = new Output(r);Thread t1 = new Thread(in);Thread t2 = new Thread(out);t1.start();t2.start();}}

但是這樣寫的會出現名字與性別不相匹配的情況。

這就表明了出現了安全問題,當出現安全問題時就要想到需要同步。

那我們加上同步再試一試:

class Res {String name;String sex;}class Input implements Runnable {private Res r;Object obj = new Object();public Input(Res r) {// TODO Auto-generated constructor stubthis.r = r;}public void run() {int x = 0;while (true) {synchronized (obj) {if (x == 0) {r.name = "mike";r.sex = "man";} else {r.name = "麗麗";r.sex = "女女女";}x = ++x % 2;}}}}class Output implements Runnable {private Res r;Object obj = new Object();Output(Res r) {this.r = r;}public void run() {while (true) {synchronized (obj){System.out.println(r.name + "----" + r.sex);}}}}public class Communicate {public static void main(String[] args) {// TODO Auto-generated method stubRes r = new Res();Input in = new Input(r);Output out = new Output(r);Thread t1 = new Thread(in);Thread t2 = new Thread(out);t1.start();t2.start();}}

但是這樣做之後還是沒有解決出現的問題。

那麼還是要考慮那兩個原則:

1.是否是兩個及兩個以上的線程?(滿足)

2.是否是同一個鎖?(??)

 

this 是本類對象就更不用想了。


在記憶體中已經有了四個.class的對象,用哪一個都是可以的。


class Res {String name;String sex;}class Input implements Runnable {private Res r;public Input(Res r) {// TODO Auto-generated constructor stubthis.r = r;}public void run() {int x = 0;while (true) {synchronized (Input.class) {if (x == 0) {r.name = "mike";r.sex = "man";} else {r.name = "麗麗";r.sex = "女女女";}x = ++x % 2;}}}}class Output implements Runnable {private Res r;Object obj = new Object();Output(Res r) {this.r = r;}public void run() {while (true) {synchronized (Input.class) {System.out.println(r.name + "----" + r.sex);}}}}public class Communicate {public static void main(String[] args) {// TODO Auto-generated method stubRes r = new Res();Input in = new Input(r);Output out = new Output(r);Thread t1 = new Thread(in);Thread t2 = new Thread(out);t1.start();t2.start();}}

但是在本例中,資源r也是獨一無二的,也就是說,如果將鎖換成是r的話也可以。









聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.