多线程互斥

多线程实现互斥

package com.startx.http.wordfilter.thread;

import sun.awt.windows.ThemeReader;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;


//三种方式实现  刷碗和洗碗操作
public class ThreadDemo2 {

    public static void main1(String[] args) {
        Object obj = new Object();
        Thread t2 = new Thread(() -> {
            synchronized (obj) {
                for (int i = 6; i >= 1; i--) {
                    System.err.println(i);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.err.println(i);
                    if (i == 1) {
                        obj.notifyAll();
                    }
                }
            }

        });


        Thread t1 = new Thread(() -> {
            synchronized (obj) {
                t2.start();


                try {
                    obj.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.err.println("t1 is finished!!");
            }
        });
        t1.start();
    }


    //ReentrantLock Condition 同步 代替synchronized
    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();
        Condition condition = lock.newCondition();

        Message message = new Message();


        new Thread(() -> {
            lock.lock();
            for (int i = 0; i < 10; i++) {
                System.err.println(Thread.currentThread() + "                 " + i);
                if (message.getId() == 0) {
                    message.setId(1);
                    System.err.println(Thread.currentThread().getName() + "   " + message.getId());
                    try {
                        condition.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    condition.signal();
                }
            }
            lock.unlock();
        }).start();


        new Thread(() -> {
            lock.lock();
            for (int i = 0; i < 10; i++) {
                System.err.println(Thread.currentThread() + "                 " + i);
                if (message.getId() != 0) {
                    message.setId(0);
                    System.err.println(Thread.currentThread().getName() + "   " + message.getId());
                    condition.signal();
                    try {
                        condition.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            lock.unlock();
        }).start();
    }


    //CountDownLatch 
    public static void mainByCountDownLatch(String[] args) {

        CountDownLatch countDownLatch = new CountDownLatch(1);

        Message message = new Message();


        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                System.err.println(Thread.currentThread() + "                 " + i);
                if (message.getId() == 0) {
                    message.setId(1);
                    System.err.println(Thread.currentThread().getName() + "   " + message.getId());
                    try {
                        countDownLatch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    countDownLatch.countDown();
                }
            }
        }).start();


        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                System.err.println(Thread.currentThread() + "                 " + i);
                if (message.getId() != 0) {
                    message.setId(0);
                    System.err.println(Thread.currentThread().getName() + "   " + message.getId());
                    countDownLatch.countDown();
                    try {
                        countDownLatch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        }).start();
    }


    //同步
    public static void mainByWaitAndNotify(String[] args) {

        Message message = new Message();


        new Thread(() -> {
            synchronized (message) {
                for (int i = 0; i < 10; i++) {
                    System.err.println(Thread.currentThread() + "                 " + i);
                    if (message.getId() == 0) {
                        message.setId(1);
                        System.err.println(Thread.currentThread().getName() + "   " + message.getId());
                        try {
                            message.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                          //如果不加 notify 那么当前线程会等待另一个调用notify的线程执行完毕后才会再执行此线程
                        message.notify();
                    }
                }
            }

        }).start();


        new Thread(() -> {
            synchronized (message) {
                for (int i = 0; i < 10; i++) {
                    System.err.println(Thread.currentThread() + "                 " + i);
                    if (message.getId() != 0) {
                        message.setId(0);
                        System.err.println(Thread.currentThread().getName() + "   " + message.getId());
                        message.notifyAll();
                        try {
                            message.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }

        }).start();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值