死锁,生产者和消费者,单例模式,线程通信

死锁 : 就是在执行过程中,都遇到了对方进入加锁的方法中,从而导致大家都访问不了的状态
 原理 :
        1 某一个线程 执行完成 需要 先后 嵌套 锁定 执行两个对象,并且在这个过程中,先锁定第一个对象
        2 另一个线程 执行完成 需要 先后 嵌套 锁定 执行两个对象,并且在这个过程中,先锁定第二个对象
        3 在第一个线程执行到第二个对象的时候,发现已经被锁定,只能等待
        4 在第二个线程执行到第一个对象的时候,发现已经被锁定,只能等待
ublic class Thread_01_DeadLock {
    public static void main(String[] args) {
        Object o1 = new Object();
        Object o2 = new Object();
        Thread t1 = new Thread(new Thread_01(o1, o2));
        Thread t2 = new Thread(new Thread_02(o1, o2));
        t1.setName("t1");
        t2.setName("t2");
        t1.start();
        t2.start();
    }
}
class Thread_01 implements Runnable{
    Object o1 ;
    Object o2;
    public Thread_01(Object o1, Object o2) {
        this.o1 = o1;
        this.o2 = o2;
    }

    @Override
    public void run() {
        synchronized (o1) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (o2) {
                System.out.println(Thread.currentThread().getName()+"执行完成");
            }
        }
    }
}
class Thread_02 implements Runnable{
    Object o1 ;
    Object o2;
    public Thread_02(Object o1, Object o2) {
        this.o1 = o1;
        this.o2 = o2;
    }

    @Override
    public void run() {
        synchronized (o2) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (o1) {
                System.out.println(Thread.currentThread().getName()+"执行完成");
            }
        }
    }
}

生产者和消费者

package work;

import java.util.Random;

//3 编写生产者和消费者
public class Text3 {
    public static void main(String[] args) {
        SynStack ss = new SynStack();
        Thread p = new Thread(new Producer(ss));
        Thread c = new Thread(new Consumer(ss));
        p.start();
        c.start();
    }
}
class Producer implements Runnable{
    SynStack ss ;

    public Producer(SynStack ss) {
        super();
        this.ss = ss;
    }

    @Override
    public void run() {
        Random random = new Random();
        while (true) {
            char ch = (char) (random.nextInt(26)+97);
            ss.push(ch);
        }
    }
}
class Consumer implements Runnable{
    SynStack ss ;

    public Consumer(SynStack ss) {
        super();
        this.ss = ss;
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            ss.pop();
        }
    }
}
class SynStack{
    // 保存数据的容器
    char[] data = new char[6];
    // 生产个数
    int count = 0;
    // 生产
    public synchronized void push(char ch) {
        // 判断是否满了
        if (count == data.length) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        // 到这里说明没有满,开始生产
        // 唤醒消费者准备消费
        this.notifyAll();
        data[count ] = ch;
        count++;
        System.out.println("生产了 "+ch+" , 剩余 "+count+" 个元素");
    }
    public synchronized char pop(){

        // 判断是否为空
        if (count == 0) {
            try {
                // 这里不用唤醒生产者,因为生产者是满了在wait,都为空了,说明生产者肯定没有wait
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        // 到这里说明不是空,开始消费
        count--;
        char ch = data[count];
        // 唤醒生产者
        this.notifyAll();
        System.out.println("消费了 "+ch+" , 剩余 "+count+" 个元素");

        return ch;
    }
}

单例模式

package day_02;

/**
 * 单例模式 : 让某个类只实例化一个对象
 *
 * 构造方法私有化, 静态变量保存对象,公共的静态方法用于获取类对象
 *
 * 饿汉模式在多线程环境下没有问题,因为不管多少线程 类只能被加载一次,所以只会被初始化一次,也就意味着只能创建一个对象
 *
 */
public class Thread_05_SingLeton {
    private Thread_05_SingLeton() {

    }

    /**
     * volatile : 为什么使用volatile呢? 防止指令重排
     *
     *
     */
    private volatile static Thread_05_SingLeton singLeton = null;

    // 效率低,因为每次都需要排队
    // public synchronized static Thread_05_SingLeton getInstance() {
    // if (singLeton == null) {
    // singLeton = new Thread_05_SingLeton();
    // }
    // return singLeton;
    // }

    // 效率较高,因为只需要第一次排队
    public static Thread_05_SingLeton getInstance() {

        if (singLeton == null) {

            synchronized (Thread_05_SingLeton.class) {
                // 1// 2
                if (singLeton == null) {
                    singLeton = new Thread_05_SingLeton();
                }
            }
        }

        return singLeton;
    }
}

线程通信

package day_02;

/**
 *wait : 让该线程进入等待状态,会释放持有的锁
 *  		无参或者传入0  表示一直等待,不会自动唤醒,只能等着notify唤醒它
 *   		也可以传入long类型的值,类似于sleep,时间到了自己唤醒
 *
 *   notify : 随机唤醒一个在该对象中正在等待的一个线程
 *
 *   notifyAll : 唤醒在该对象中所有等待的线程
 *
 *   以上方法只能用在加锁的成员方法中,
 *
 *
 *   需求 : 打印奇数和偶数
 *   		1 有一个业务类,有一个打印奇数和打印偶数的方法
 *   		2 有一个变量 count 记录当前的数字
 *   		3 两个线程,分别调用打印奇数和打印偶数的方法
 */
public class Thread_02_wait {
    public static void main(String[] args) {
        Num num = new Num();
        Thread t1 = new PrintEven(num);
        Thread t2 = new PrintOdd(num);
        t1.setName("t1");
        t2.setName("t2");
        t2.start();
        t1.start();
    }
}
// 打印偶数
class PrintEven extends Thread{
    Num num;

    public PrintEven(Num num) {
        this.num = num;
    }

    @Override
    public void run() {
        while (true) {
            num.printEven();
        }
    }
}
// 打印奇数
class PrintOdd extends Thread{
    Num num;

    public PrintOdd(Num num) {
        this.num = num;
    }

    @Override
    public void run() {
        while (true) {
            num.printOdd();
        }
    }
}
class Num{
    int count =1 ;
    public synchronized void printOdd(){
        System.out.println(Thread.currentThread().getName()+"--->"+count);
        count++;
        // 唤醒其他线程,去打印偶数
        this.notifyAll();
        // 进入等待
        try {
            Thread.sleep(500);
            this.wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized void printEven() {
        System.out.println(Thread.currentThread().getName() + "--->" + count);
        count++;
        // 唤醒其他线程,去打印偶数
        this.notifyAll();
        // 进入等待
        try {
            Thread.sleep(500);
            this.wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值