標籤:代碼 同步 ant rac 取出 鎖對象 安全 == product
生產者和消費者的例子
一、wait() / notify()方法
wait() / nofity()方法是基類Object的兩個方法,也就意味著所有Java類都會擁有這兩個方法,這樣,我們就可以為任何對象實現同步機制。
wait()方法:當緩衝區已滿/空時,生產者/消費者線程停止自己的執行,放棄鎖,使自己處於等等狀態,讓其他線程執行。
notify()方法:當生產者/消費者向緩衝區放入/取出一個產品時,向其他等待的線程發出可執行檔通知,同時放棄鎖,使自己處於等待狀態。
直接貼上代碼 用as
class Person{
private String name;
private String sex;
private Boolean isEmpty = Boolean.TRUE;
public void set(String name,String sex){
synchronized(this){
while(!isEmpty.equals(Boolean.TRUE)){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.name=name;
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.sex=sex;
isEmpty=Boolean.FALSE;
this.notifyAll();
}
}
public void get(){
synchronized(this){
while(!isEmpty.equals(Boolean.FALSE)){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String name=getName();
String sex=getSex();
System.out.println(name + " --> " + sex);
isEmpty=Boolean.TRUE;
this.notifyAll();
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
class Producer implements Runnable{
private Person p;
public Producer(Person p){
this.p=p;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=2;i<100;i++){
if(i%2==0){
p.set("李果", "女");
}else{
p.set("李凡", "男");
}
}
}
}
class Consumer implements Runnable{
private Person p;
public Consumer(Person p){
this.p=p;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=2;i<100;i++){
p.get();
}
}
}
public class Productor_ConsumerDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Person p = new Person();
new Thread(new Producer(p)).start();
new Thread(new Consumer(p)).start();
new Thread(new Producer(p)).start();
new Thread(new Consumer(p)).start();
}
}
用Synchronized 同步方法
await() / signal()方法
在JDK5.0之後,Java提供了更加健壯的線程處理機制,包括同步、鎖定、線程池等,它們可以實現更細粒度的線程式控制制。await()和signal()就是其中用來做同步的兩種方法,它們的功能基本上和wait() / nofity()相同,完全可以取代它們,但是它們和新引入的鎖定機制Lock直接掛鈎,具有更大的靈活性。通過在Lock對象上調用newCondition()方法,將條件變數和一個鎖對象進行綁定,進而控制並發程式訪問競爭資源的安全。下面來看代碼:
class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
private String sex;
private final ReentrantLock lock = new ReentrantLock();
private final Condition con = lock.newCondition();
private Boolean isEmpty = Boolean.TRUE;
public void set(String name, String sex) {
lock.lock();
while (!isEmpty.equals(Boolean.TRUE)) {
try {
con.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
this.name = name;
Thread.sleep(1);
this.sex = sex;
isEmpty = Boolean.FALSE;
con.signal();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void get() {
lock.lock();
while (!isEmpty.equals(Boolean.FALSE)) {
try {
con.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
String name = getName();
String sex = getSex();
System.out.println(name + " --> " + sex);
isEmpty = Boolean.TRUE;
con.signal();
} finally {
lock.unlock();
}
}
}
class Productor implements Runnable {
private Person p;
public Productor(Person p) {
this.p = p;
}
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 2; i < 100; i++) {
if (i % 2 == 0) {
p.set("李果", "女");
} else {
p.set("李雨軒", "男");
}
}
}
}
class Cosumer implements Runnable {
private Person p;
public Cosumer(Person p) {
this.p = p;
}
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 2; i < 100; i++) {
p.get();
}
}
}
public class Producer_Demo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Person p = new Person();
new Thread(new Productor(p)).start();
new Thread(new Cosumer(p)).start();
}
}
java 生產者和消費者