今天學習了的主要內容:
1.什么是產生者消費者模型
2.sleep()與wait()兩個方法之間的差異
產生者消費者模型:
兩個共享固定大小的緩沖區的執行緒,一個是往里面生成資料的,
一個是往里面釋放使用資料的,
那么前者就是生產者,后者就是消費者,
生產者主要就是生成一定數量的資料在緩沖區供消費者執行緒使用,
一但到某個臨界值,生產者就不會繼續生產資料,
消費者就是消耗生產者產生的資料的執行緒,
一旦資料到某個臨界值,就不再消耗,
我們關鍵就是要控制這兩者的臨界值!!
解決方法:
生產者解決方法:
讓生產者在臨界值休眠,然后等消費者從緩沖區消耗多個或者一個資料之后再喚醒,
消費者解決方法:
當緩沖區到那個臨界值的時候,讓消費者休眠,等生產者放入資料之后再喚醒,
sleep()與wait()兩個方法之間的差異:
wait()方法是Object類提供的,sleep()方法是Thread類提供的,
wait()方法會把鎖給打開,但是sleep()方法不會,
wait()方法只能用在同步方法里,sleep()方法可以用在任意地點,
Resourcese類(提供取錢存錢方法):
package LessonForThread07;
public class Resourcese
{
private int money = 0;
public int getMoney()
{
return money;
}
public void setMoney(int money)
{
this.money = money;
}
public synchronized void save()
{
//if (this.getMoney() != 0)
while (this.getMoney() != 0)//為了防止多個執行緒無序喚醒,我們將if換成while這樣每進來一個執行緒就可以判斷是否要wait了,
{
try
{
//wait()方法只能使用在同步方法中!!!synchronized修飾
wait();//使當前執行緒隨呼叫這個方法的那個執行緒,等待另一個執行緒呼叫notify()方法喚醒時才繼,他不能保證只有一個執行緒進來,它不鎖定值
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
this.setMoney(this.getMoney()+10);
notify();//喚醒的是對方的wait,讓當前呼叫執行緒wait喚醒另一個呼叫的執行緒蘇醒,
System.out.println("余額:"+this.getMoney());
}
public synchronized void draw()
{
// if (this.getMoney() == 0)
while (this.getMoney() == 0)
{
try
{
wait();
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
this.setMoney(this.getMoney()-10);
notify();
System.out.println("被取錢之后還剩余:"+this.getMoney());
}
}
Producer類(生產者):
package LessonForThread07;
public class Producer implements Runnable
{
private Resourcese save_money;
public Producer(Resourcese save_money)
{
this.save_money = save_money;
}
@Override
public void run()
{
for (int i=0; i<5; i++)
{
try
{
Thread.sleep(500);//存錢回圈一次讓出500ms
} catch (InterruptedException e)
{
e.printStackTrace();
}
save_money.save();
}
}
}
Customer類(消費者):
package LessonForThread07;
public class Customer implements Runnable
{
private Resourcese save_money;
public Customer(Resourcese save_money)
{
this.save_money = save_money;
}
@Override
public void run()
{
for (int i=0; i<5; i++)
{
try
{
Thread.sleep(500);//取錢回圈一次讓出500ms
} catch (InterruptedException e)
{
e.printStackTrace();
}
save_money.draw();
}
}
}
ThreadTest05類(測驗notify和wait):
package LessonForThread07;
public class ThreadTest05
{
public static void main(String[] args)
{
Resourcese money = new Resourcese();
Producer p1 = new Producer(money);
new Thread(p1).start();
Producer p2 = new Producer(money);
new Thread(p2).start();
Producer p3 = new Producer(money);
new Thread(p3).start();
Customer c1 = new Customer(money);
new Thread(c1).start();
Customer c2 = new Customer(money);
new Thread(c2).start();
Customer c3 = new Customer(money);
new Thread(c3).start();
// Thread t1 = new Thread(p1);
// t1.start();
// Thread t2 = new Thread(c1);
// Thread t3 = new Thread(p1);
// t3.start();
// Thread t4 = new Thread(c1);
// Thread t5 = new Thread(p1);
// t5.start();
// Thread t6 = new Thread(c1);
// Thread t7 = new Thread(p1);
// t7.start();
// Thread t8 = new Thread(c1);
//
// t2.start();
// t4.start();
// t6.start();
// t8.start();
try
{
Thread.sleep(10000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("完畢");
}
}
本篇部分文字來源于:
咕嘟咖啡楊海濱老師 — 《java編程語言高級特性》
在這里十分感謝老師能夠給我帶來學習的激情,
2020.10.25
可以轉載我的學習日記但請注明出處,謝謝,
本文章是本人學習筆記,不進行任何商用!也請別拿去商用!只為記錄本人學習歷程,
畢
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/192666.html
標籤:python
