java筆記--多生產(chǎn)多消費(fèi)問(wèn)題

單一生產(chǎn)者,消費(fèi)者問(wèn)題:
class Resource{
    private String name;
    private int count=1;
    private boolean flag=false;
    public synchronized void set(String name){
        if(flag)
                try{
                    this.wait();
                    }catch(InterruptedException e){ 
                    }
        this.name=name+count;
        count++;
        System.out.println(Thread.currentThread().getName()+"...生產(chǎn)者。。。。。"+this.name);
        flag=true;
        notify();
    }
    public synchronized void out(){
        if(!flag)
                try{
                    this.wait();
                    }catch(InterruptedException e){ 
                    }
        System.out.println(Thread.currentThread().getName()+"...消費(fèi)者。。。"+this.name);
        flag=false; 
        notify();
    }
}
class Producer implements Runnable{
    private Resource r;
    Producer(Resource r){
        this.r=r;
    }
    public void run(){
        while(true){
            r.set("kaoya");
        }
    }
}
class Consumer implements Runnable{
    private Resource r;
    Consumer(Resource r){
        this.r=r;
    }
    public void run(){
        while(true){
            r.out();
        }
    }
}
public class XiaoFei {

    public static void main(String[] args) {
        Resource r=new Resource();
        Producer pro=new Producer(r);
        Consumer con=new Consumer(r);
        
        Thread t1=new Thread(pro);
        Thread t2=new Thread(con);
        t1.start();
        t2.start();
    }
}

運(yùn)行:
多生產(chǎn)多消費(fèi)問(wèn)題:
class Resource{
    private String name;
    private int count=1;
    private boolean flag=false;
    public synchronized void set(String name){
        if(flag)
                try{
                    this.wait();
                    }catch(InterruptedException e){ 
                    }
        this.name=name+count;
        count++;
        System.out.println(Thread.currentThread().getName()+"...生產(chǎn)者。。。。。"+this.name);
        flag=true;
        notify();
    }
    public synchronized void out(){
        if(!flag)
                try{
                    this.wait();
                    }catch(InterruptedException e){ 
                    }
        System.out.println(Thread.currentThread().getName()+"...消費(fèi)者。。。"+this.name);
        flag=false; 
        notify();
    }
}
class Producer implements Runnable{
    private Resource r;
    Producer(Resource r){
        this.r=r;
    }
    public void run(){
        while(true){
            r.set("kaoya");
        }
    }
}
class Consumer implements Runnable{
    private Resource r;
    Consumer(Resource r){
        this.r=r;
    }
    public void run(){
        while(true){
            r.out();
        }
    }
}
public class Many {

    public static void main(String[] args) {
        Resource r=new Resource();
        Producer pro=new Producer(r);
        Consumer con=new Consumer(r);
        
        Thread t0=new Thread(pro);
        Thread t1=new Thread(pro);
        Thread t2=new Thread(con);
        Thread t3=new Thread(con);
        t0.start();
        t1.start();
        t2.start();
        t3.start();

    }
}

如果直接創(chuàng)建多個(gè)線程,會(huì)出現(xiàn)生產(chǎn)一次,卻消費(fèi)多次的沖突,或者生產(chǎn)多個(gè),卻消費(fèi)最近一個(gè)(即有生產(chǎn)的未被消費(fèi))的沖突。

運(yùn)行:

這時(shí)可把判斷語(yǔ)句if換成while,但會(huì)出現(xiàn)死鎖現(xiàn)象。所以要在將notify改為notifyAll。即:

//多生產(chǎn)多消費(fèi)問(wèn)題

//if判斷標(biāo)記,只有一次會(huì)導(dǎo)致不該執(zhí)行的線程運(yùn)行了。出現(xiàn)了數(shù)據(jù)錯(cuò)誤的情況
//while判斷標(biāo)記,解決了線程獲取執(zhí)行權(quán)后,是否要運(yùn)行!

//notify只能喚醒一個(gè)線程,如果本方喚醒了本方,無(wú)意義。而且while判斷標(biāo)記+notify會(huì)導(dǎo)致死鎖。
//notifyAll解決了,本方線程一定會(huì)喚醒對(duì)方線程。
class Resource{
    private String name;
    private int count=1;
    private boolean flag=false;
    public synchronized void set(String name){
        while(flag)
                try{
                    this.wait();
                    }catch(InterruptedException e){ 
                    }
        this.name=name+count;
        count++;
        System.out.println(Thread.currentThread().getName()+"...生產(chǎn)者。。。。。"+this.name);
        flag=true;
        notifyAll();
    }
    public synchronized void out(){
        while(!flag)
                try{
                    this.wait();
                    }catch(InterruptedException e){ 
                    }
        System.out.println(Thread.currentThread().getName()+"...消費(fèi)者。。。"+this.name);
        flag=false; 
        notifyAll();
    }
}
class Producer implements Runnable{
    private Resource r;
    Producer(Resource r){
        this.r=r;
    }
    public void run(){
        while(true){
            r.set("kaoya");
        }
    }
}
class Consumer implements Runnable{
    private Resource r;
    Consumer(Resource r){
        this.r=r;
    }
    public void run(){
        while(true){
            r.out();
        }
    }
}
public class YouHua {

    public static void main(String[] args) {
        Resource r=new Resource();
        Producer pro=new Producer(r);
        Consumer con=new Consumer(r);
        
        Thread t0=new Thread(pro);
        Thread t1=new Thread(pro);
        Thread t2=new Thread(con);
        Thread t3=new Thread(con);
        t0.start();
        t1.start();
        t2.start();
        t3.start();

    }
}

運(yùn)行:
image.png
JDK1.5以后將同步和鎖封裝成了對(duì)象。并將操作鎖的隱式方式定義到了該對(duì)象中,將隱式變成了顯示動(dòng)作。

Lock接口:出現(xiàn)替代了同步代碼塊或者同步函數(shù)。將同步的隱式鎖操作變成了顯式鎖操作。同時(shí)更為靈活??梢砸粋€(gè)鎖上加上多組監(jiān)視器。——lock();unclock();

Condition接口:出現(xiàn)替代了Object中的wait,notify,notifyAll方法。將這些監(jiān)視器方法單獨(dú)進(jìn)行了封裝,變成Condition監(jiān)視器對(duì)象。可以任意鎖進(jìn)行組合?!猘wait();singnal();singnalAll();

import java.util.concurrent.locks.*;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

class Resource{
    private String name;
    private int count=1;
    private boolean flag=false;
    
//  創(chuàng)建一個(gè)鎖對(duì)象。
    Lock lock=new ReentrantLock();
    
    //通過(guò)已有的鎖獲取該鎖上的監(jiān)視器對(duì)象。
//  Condition con=lock.newCondition();
    
    //通過(guò)已有的鎖獲取兩組監(jiān)視器,一組監(jiān)視生產(chǎn)者,一組監(jiān)視消費(fèi)者。
        Condition producer_con = lock.newCondition();
        Condition consumer_con = lock.newCondition();
    public void set(String name){
        lock.lock();
//      try{lock.wait();}catch(InterruptedException e){}
        try{
            while(flag)
                try{
                    producer_con.await();;
                    }catch(InterruptedException e){ 
                    }
        this.name=name+count;
        count++;
        System.out.println(Thread.currentThread().getName()+"...生產(chǎn)者。。。。。"+this.name);
        flag=true;
//      con.signalAll();
        consumer_con.signal();
        }finally{
            lock.unlock();
        }
        
    }
    public  void out(){
        lock.lock();
//      try{this.wait();}catch(InterruptedException e){}
        try{
            while(!flag)
                try{
                    consumer_con.await();
                    }catch(InterruptedException e){ 
                    }
        System.out.println(Thread.currentThread().getName()+"...消費(fèi)者。。。"+this.name);
        flag=false; 
//      con.signalAll();
        producer_con.signal();
        }finally{
            lock.unlock();
        }
        
    }
}
class Producer implements Runnable{
    private Resource r;
    Producer(Resource r){
        this.r=r;
    }
    public void run(){
        while(true){
            r.set("kaoya");
        }
    }
}
class Consumer implements Runnable{
    private Resource r;
    Consumer(Resource r){
        this.r=r;
    }
    public void run(){
        while(true){
            r.out();
        }
    }
}
public class lockandcondition {

    public static void main(String[] args) {
        Resource r=new Resource();
        Producer pro=new Producer(r);
        Consumer con=new Consumer(r);
        
        Thread t0=new Thread(pro);
        Thread t1=new Thread(pro);
        Thread t2=new Thread(con);
        Thread t3=new Thread(con);
        t0.start();
        t1.start();
        t2.start();
        t3.start();

    }
}

運(yùn)行效果同上。

JDK1.5以后將同步和鎖封裝成了對(duì)象。
 并將操作鎖的隱式方式定義到了
該對(duì)象中,將隱式變成了顯示動(dòng)作。

Lock接口:出現(xiàn)替代了同步代碼塊或者同步函數(shù)。將同步的隱式鎖操作變成了顯式鎖操作。
         同時(shí)更為靈活??梢砸粋€(gè)鎖上加上多組監(jiān)視器。
lock();unclock();

Condition接口:出現(xiàn)替代了Object中的wait,notify,notifyAll方法。
               將這些監(jiān)視器方法單獨(dú)進(jìn)行了封裝,變成Condition監(jiān)視器對(duì)象。
               可以任意鎖進(jìn)行組合。
await();singnal();singnalAll();

wait與sleep的區(qū)別:
1.wait可以指定時(shí)間也可以不指定。
sleep必須指定時(shí)間

2.在同步中時(shí),對(duì)cpu的執(zhí)行權(quán)和鎖的處理不同。
wait:釋放執(zhí)行權(quán),釋放鎖。
sleep:釋放執(zhí)行權(quán),不釋放鎖。

class Resource{
    private String name;
    private int count=1;
    private boolean flag=false;
    
//  創(chuàng)建一個(gè)鎖對(duì)象。
    Lock lock=new ReentrantLock();
    
    //通過(guò)已有的鎖獲取該鎖上的監(jiān)視器對(duì)象。
//  Condition con=lock.newCondition();
    
    //通過(guò)已有的鎖獲取兩組監(jiān)視器,一組監(jiān)視生產(chǎn)者,一組監(jiān)視消費(fèi)者。
        Condition producer_con = lock.newCondition();
        Condition consumer_con = lock.newCondition();
    public void set(String name){
        lock.lock();
//      try{lock.wait();}catch(InterruptedException e){}
        try{
            while(flag)
                try{
                    producer_con.await();;
                    }catch(InterruptedException e){ 
                    }
        this.name=name+count;
        count++;
        System.out.println(Thread.currentThread().getName()+"...生產(chǎn)者。。。。。"+this.name);
        flag=true;
//      con.signalAll();
        consumer_con.signal();
        }finally{
            lock.unlock();
        }
        
    }
    public  void out(){
        lock.lock();
//      try{this.wait();}catch(InterruptedException e){}
        try{
            while(!flag)
                try{
                    consumer_con.await();
                    }catch(InterruptedException e){ 
                    }
        System.out.println(Thread.currentThread().getName()+"...消費(fèi)者。。。"+this.name);
        flag=false; 
//      con.signalAll();
        producer_con.signal();
        }finally{
            lock.unlock();
        }
        
    }
}
class Producer implements Runnable{
    private Resource r;
    Producer(Resource r){
        this.r=r;
    }
    public void run(){
        while(true){
            r.set("kaoya");
        }
    }
}
class Consumer implements Runnable{
    private Resource r;
    Consumer(Resource r){
        this.r=r;
    }
    public void run(){
        while(true){
            r.out();
        }
    }
}
public class lockandcondition {

    public static void main(String[] args) {
        Resource r=new Resource();
        Producer pro=new Producer(r);
        Consumer con=new Consumer(r);
        
        Thread t0=new Thread(pro);
        Thread t1=new Thread(pro);
        Thread t2=new Thread(con);
        Thread t3=new Thread(con);
        t0.start();
        t1.start();
        t2.start();
        t3.start();

    }
}

/*

停止線程:

1 stop方法。

2 run方法結(jié)束。

怎么控制線程的任務(wù)結(jié)束呢?
任務(wù)中都會(huì)有循環(huán)結(jié)構(gòu),只要控制住循環(huán)就可以結(jié)束任務(wù)。

控制循環(huán)通常就用定義標(biāo)記來(lái)完成(變量flag)。

但是如果線程處于凍結(jié)狀態(tài),無(wú)法讀取標(biāo)記。如何結(jié)束呢?

可以使用interrupt()方法將線程從凍結(jié)狀態(tài)強(qiáng)制恢復(fù)到運(yùn)行狀態(tài)中來(lái),讓線程具備cpu的執(zhí)行資格。

但是強(qiáng)制動(dòng)作會(huì)發(fā)生了InterruptedException,記得要處理。
*/

class StopThread implements Runnable{
    private boolean flag=true;
    public synchronized void run(){
        while(flag){
            try{
                wait();
            }catch(InterruptedException e){
                System.out.println(Thread.currentThread().getName()+"....."+e);
                flag=false;
            }
            System.out.println(Thread.currentThread().getName()+".....++++");
        }
    }
    public void setFlag(){
        flag=false;
    }
}

public class StopThreadDemo {
    public static void main(String[] args) 
    {
        StopThread st = new StopThread();

        Thread t1 = new Thread(st);
        Thread t2 = new Thread(st);

        t1.start();
        t2.setDaemon(true);
        t2.start();


        int num = 1;
        for(;;)
        {
            if(++num==50)
            {
//              st.setFlag();
                t1.interrupt();
//              t2.interrupt();
                break;
            }
            System.out.println("main...."+num);
        }

        System.out.println("over");
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容