生产者与消费者 升级

倾然丶 夕夏残阳落幕 提交于 2020-03-08 20:26:28
/**
 * @DESCRIPTION:
 * Sychronized wait notifyAll
 * 注意: while来判断唤醒条件 if会引起 虚假唤醒
 * @Author: WangLt
 * @Date: 2020/3/8
 */
@SuppressWarnings("all")
public class SellerAndBuyer {

    public static void main(String[] args) {
        Data data = new Data();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"T1").start();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"T2").start();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"T3").start();

    }

}

class Data{

    private int num = 1;

    public synchronized void increment() throws InterruptedException{
        System.out.println(Thread.currentThread().getName()+"在准备++");
        while (num != 0) {
            System.out.println(Thread.currentThread().getName()+"++不了==========");
            this.wait();
            System.out.println(Thread.currentThread().getName()+"++继续==========");
        }
        System.out.println(Thread.currentThread().getName()+" "+(num++));
        this.notifyAll();
    }

    public synchronized void decrement() throws InterruptedException {
        System.out.println(Thread.currentThread().getName()+"在准备--");
        while (num == 0) {
            System.out.println(Thread.currentThread().getName()+"--不了@@@@@@@@@@");
            this.wait();
            System.out.println(Thread.currentThread().getName()+"--继续@@@@@@@@@@");
        }
        System.out.println(Thread.currentThread().getName()+" "+(num--));
        this.notifyAll();
    }
}
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @DESCRIPTION: ReentrantLock lock unlock
 * @Author: WangLt
 * @Date: 2020/3/8
 */

public class SellerAndBuyer2 {

    public static void main(String[] args) {
        Data2 data = new Data2();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    data.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "T1").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    data.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "T2").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    data.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "T3").start();

    }

}

class Data2 {

    private int num = 1;
    private Lock lock = new ReentrantLock();

    public void increment() throws InterruptedException {
        try {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + "在准备++");
            while (num != 0) {
                System.out.println(Thread.currentThread().getName() + "++不了==========");
                this.wait();
                System.out.println(Thread.currentThread().getName() + "++继续==========");
            }
            System.out.println(Thread.currentThread().getName() + " " + (num++));
            this.notifyAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }


    }

    public void decrement() throws InterruptedException {
        try {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + "在准备--");
            while (num == 0) {
                System.out.println(Thread.currentThread().getName() + "--不了@@@@@@@@@@");
                this.wait();
                System.out.println(Thread.currentThread().getName() + "--继续@@@@@@@@@@");
            }
            System.out.println(Thread.currentThread().getName() + " " + (num--));
            this.notifyAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }
}
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @DESCRIPTION: 
 * ReentrantLock
 * lock unlock
 * condition -> await signalAll
 * @Author: WangLt
 * @Date: 2020/3/8
 */

public class SellerAndBuyer3 {

    public static void main(String[] args) {
        Data3 data = new Data3();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    data.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "T1").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    data.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "T2").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    data.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "T3").start();

    }

}

class Data3 {

    private int num = 1;
    private Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();


    public void increment() throws InterruptedException {
        try {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + "在准备++");
            while (num != 0) {
                System.out.println(Thread.currentThread().getName() + "++不了==========");
                condition.await();
                System.out.println(Thread.currentThread().getName() + "++继续==========");
            }

            System.out.println(Thread.currentThread().getName() + " " + (num++));
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }


    }

    public void decrement() throws InterruptedException {
        try {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + "在准备--");
            while (num == 0) {
                System.out.println(Thread.currentThread().getName() + "--不了@@@@@@@@@@");
                condition.await();
                System.out.println(Thread.currentThread().getName() + "--继续@@@@@@@@@@");
            }
            System.out.println(Thread.currentThread().getName() + " " + (num--));
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }
}
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @DESCRIPTION: 指定线程执行顺序
 * ReentrantLock
 * lock unlock
 * condition -> await signalAll
 *
 * @Author: WangLt
 * @Date: 2020/3/8
 */

public class SellerAndBuyer4 {

    public static void main(String[] args) {
        Data4 data = new Data4();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.first();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"T1").start();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.second();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"T2").start();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.third();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"T3").start();
    }
}

class Data4{

    private int num = 1;
    private Lock lock = new ReentrantLock();
    Condition condition1 = lock.newCondition();
    Condition condition2 = lock.newCondition();
    Condition condition3 = lock.newCondition();

    public void first() throws InterruptedException{
        try {
            lock.lock();
            System.out.println(Thread.currentThread().getName()+"在准备11");
            while (num != 1) {
                System.out.println(Thread.currentThread().getName()+"11不了==========");
                condition1.await();
                System.out.println(Thread.currentThread().getName()+"11继续==========");
            }
            System.out.println("------------------------------"+Thread.currentThread().getName());
            num = 2;
            condition2.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void second() throws InterruptedException {
        try {
            lock.lock();
            System.out.println(Thread.currentThread().getName()+"在准备22");
            while (num != 2) {
                System.out.println(Thread.currentThread().getName()+"22不了@@@@@@@@@@");
                condition2.await();
                System.out.println(Thread.currentThread().getName()+"22继续@@@@@@@@@@");
            }

            System.out.println("------------------------------"+Thread.currentThread().getName());
            num = 3;
            condition3.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }

    public void third() throws InterruptedException {
        try {
            lock.lock();
            System.out.println(Thread.currentThread().getName()+"在准备33");
            while (num != 3) {
                System.out.println(Thread.currentThread().getName()+"33不了##########");
                condition3.await();
                System.out.println(Thread.currentThread().getName()+"33继续##########");
            }
            System.out.println("------------------------------"+Thread.currentThread().getName());
            num = 1;
            condition1.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!