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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

用java智能锁远程,从生产者-消费者模型了解线程、同步、锁(java)

發布時間:2024/1/23 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用java智能锁远程,从生产者-消费者模型了解线程、同步、锁(java) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

代碼主要包括4個類和1個測試類:

1、Egg 類: 雞蛋,可以由生產者放入到籃子中,供消費者消費食用。

2、Blanket 類:一個籃子,最多能裝5個雞蛋。

3、Producer 類: 生產者(可以有1個或多個,但一次只能由一個人放入1個),負責把雞蛋放入到籃子中。

4、Customer 類 :消費者(可以有1個或多個,但一次只能由一個人吃1個), 會把籃子里的雞蛋取走食用。

5、Tester 類 : 用于測試,可設置多個生產者和消費者。

1、Egg類,有一個ID屬性,用于標識生產和消費的雞蛋是哪一個雞蛋,直觀一點:

public class Egg {

int id;

public Egg (int id) {

this.id = id;

}

public String toString () {

return "雞蛋" + id;

}

}

2、Blanket類,其中的放入雞蛋(add)和吃雞蛋(eat)方法使用了同步鎖,即同一時間內只能由一個人操作籃子,放入雞蛋或吃雞蛋:

public class Blanket {

int count = 0;

Egg [] eggs = new Egg[5];

public synchronized void eat() {

while(count == 0){

try{

this.wait();//當籃子里雞蛋數目為0時,等待生產者放入雞蛋

}catch(InterruptedException ex) {

ex.printStackTrace();

}

}

this.count--;

System.out.println(Thread.currentThread().getName() + "吃了"+this.eggs[count].toString());

this.notifyAll();

}

public synchronized void add(Egg e) {

while(count == 5){

try {

this.wait();//當籃子里有5個雞蛋時,先休息

}catch(InterruptedException ex){

ex.printStackTrace();

}

}

System.out.println(Thread.currentThread().getName() + "生產了:" + e.toString());

this.eggs[count] = e;//生產雞蛋

count++;

this.notifyAll();//喚醒消費者

}

}

3、Producer 類,繼承Runable的生產者,在條件允許(籃子雞蛋未滿)的情況不停的放入雞蛋:

public class Producer implements Runnable{

Blanket blanket;

public Producer(Blanket blanket) {

this.blanket = blanket;

}

@Override

public void run() {

// TODO Auto-generated method stub

while(true){

int i = blanket.count;

Egg e = new Egg(i);

blanket.add(e);

try {

Thread.sleep((int)(Math.random()*1000));

} catch (InterruptedException ex) {

ex.printStackTrace();

}

}

}

}

4、Customer 類 ,繼承Runable 的消費者,在籃子里有雞蛋的時候就會去取出來吃:

public class Customer implements Runnable {

Blanket blanket;

public Customer(Blanket blanket) {

this.blanket = blanket;

}

@Override

public void run() {

// TODO Auto-generated method stub

while(true) {

blanket.eat();

try{

Thread.sleep((int)(Math.random()*2800));

}catch(InterruptedException ex){

ex.printStackTrace();

}

}

}

}

5、Tester 類,用于進行測試,定義一個籃子,和1個或多個生產者,消費者,使這些生產者和消費者共用一個籃子:

public class Tester {

public static void main(String[] args) {

// TODO Auto-generated method stub

Blanket b = new Blanket();

Producer p = new Producer(b);

Producer q = new Producer(b);

Customer c = new Customer(b);

Customer d = new Customer(b);

Customer e = new Customer(b);

Customer f = new Customer(b);

new Thread(p,"生產者A").start();

new Thread(q,"生產者B").start();

new Thread(c,"消費者A").start();

new Thread(d,"消費者B").start();

new Thread(e,"消費者C").start();

new Thread(f,"消費者D").start();

}

}

上面的程序運行效果如下:

生產者A生產了:雞蛋0

消費者A吃了雞蛋0

生產者B生產了:雞蛋0

消費者B吃了雞蛋0

生產者A生產了:雞蛋0

消費者D吃了雞蛋0

生產者A生產了:雞蛋0

消費者C吃了雞蛋0

生產者A生產了:雞蛋0

生產者B生產了:雞蛋1

消費者D吃了雞蛋1

生產者B生產了:雞蛋1

生產者A生產了:雞蛋2

生產者A生產了:雞蛋3

生產者A生產了:雞蛋4

消費者B吃了雞蛋4

生產者B生產了:雞蛋5

消費者A吃了雞蛋5

生產者A生產了:雞蛋5

消費者B吃了雞蛋5

它將不停的運行下去,不會出現死鎖。代碼的同步(synchronized)模塊設置在共用的資源(籃子)中,每次只能由一名生產者或者消費者操作籃子,其他人處于等待(wait)狀態,等它操作完了,它才通知其他人(notifyAll),其它人則通過競爭,然后只有一個競爭勝利的可以操作籃子,如此循環。

總結

以上是生活随笔為你收集整理的用java智能锁远程,从生产者-消费者模型了解线程、同步、锁(java)的全部內容,希望文章能夠幫你解決所遇到的問題。

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