JAVA入门—————线程协调 AND 懒汉饿汉单例模式

线程协调

void wait ()——在其他线程调用此对象的 notify () 方法或 notifyAll () 方法前,导致当前线程等待。
void wait (long timeout)——在其他线程调用此对象的 notify () 方法或 notifyAll () 方法,或者超过指定的时间量前,导致当前线程等待。
  • 若没有唤醒 则线程一直等待…
void notify ()——唤醒在此对象监视器上等待的单个线程。
void notifyAll ()——唤醒在此对象监视器上等待的所有线程。
public class Threaddemo {
    public static void main(String[] args) {
        DeadLocka deadLocka = new DeadLocka();
        Thread thread1 = new Thread(deadLocka);
        DeadLockb deadLockb = new DeadLockb();
        Thread thread2 = new Thread(deadLockb);
        thread1.start();
        thread2.start();
    }
}
class DeadLocka implements Runnable {

    @Override
    public void run() {
        synchronized ("A") {
            System.out.println("线程1获得了A锁");
            try {
                "A".wait();————————————使线程1等待 让线程2执行
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        synchronized ("B") {
            System.out.println("线程1获得了A锁和B锁");
        }
    }
}
class DeadLockb implements Runnable {
    @Override
    public void run() {
        synchronized ("B") {
            System.out.println("线程2获得了B锁");
            synchronized ("A") {
                System.out.println("线程2获得了B锁和A锁");
                "A".notifyAll();————————————当线程2执行结束一致  唤醒线程1 让其继续执行
            }
        }——————输出:线程1获得了A锁
					线程2获得了B锁
					线程2获得了B锁和A锁
					线程1获得了A锁和B锁
    }
}

懒汉式单例模式

  • 在使用这个对象时 才会去查看这个对象是否存在 若不存在则立马创建 若存在则返回这个对象
    线程不安全 需要加上同步锁——影响了程序执行效率
public class Threadlanhan {
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            HelloRunnable helloRunnable = new HelloRunnable();
            Thread thread = new Thread(helloRunnable,"线程名称: "+i);
            thread.start();
        }
    }
}
class LanHan {
    public LanHan() {
        System.out.println("对象实例化了");
    }
    private static LanHan man = null;
    public static LanHan getMan() {
        synchronized ("") {
            if (man == null) {   ————————若对象不存在
                man = new LanHan();   ——————————则创建
                System.out.println("对象被创建了");
            }
        }
        return man;
    }
}
class HelloRunnable implements Runnable{
    @Override
    public void run() {
        LanHan.getMan();
    }
}       ——————————输出:对象实例化了   对象被创建了

饿汉式单例模式

  • 在加载这个类时就已经创建好一个对象实例 等待调用
    线程安全 效率比懒汉式效率高
public class Threadehan {
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            Hello hello = new Hello();
            Thread thread = new Thread(hello,"线程名称: "+i);
            thread.start();
        }
    }
}
class ehan {
    private static final ehan man = new ehan();————————在定义时已创建好对象 等待调用
    public static ehan getMan() {
        synchronized ("") {
            if (man == null) {
                System.out.println("对象不存在");
            }else {
                System.out.println("对象已被创建成功  等待调用");
            }
        }
        return man;
    }
}
class Hello implements Runnable{

    @Override
    public void run() {
        ehan.getMan();
    }   ————————————输出:对象已被创建成功  等待调用
}

模拟生产者消费者和缓冲区

产品

public class Product {
    //  产品
    public String name;
    public Product(String name){
        this.name = name;
    }
   public void setName(String name){
        this.name = name;
   }
   public String getName(){
        return name;
   }
}

缓冲区

import java.util.List;
public class ProductPool {
    //   缓冲区—吧台
    public int MaxSize = 0;
    public List<Product> list;    //  在吧台声明一个产品列表集合
    public ProductPool(int MaxSize,List<Product> list){
        this.MaxSize = MaxSize;    //  生产者生产的产品放进吧台的集合中
        this.list = list;
    }
    public synchronized void push(Product product) {
        if(this.list.size() >= this.MaxSize){  //  当生产的产品数量大于缓冲区大小时
            try {
                this.wait();   //  生产者不再生产产品放入缓冲区中 而是等消费者消费
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.list.add(product);  //  若产品数量没有达到缓冲区的最大数量 则将产品放入缓冲区
        this.notifyAll();
    }

    public synchronized Product pop() {
        if(this.list.size() <= 0){  //  当生产的产品数量小于缓冲区大小时
            try {
                this.wait();   //  消费者不再从缓冲区取出产品 而是等生产者生产
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        Product product = this.list.remove(0);
        this.notifyAll();
        return product;
    }
}

生产者

public class Producer implements Runnable {
    // 生产者
    public ProductPool productpool;
    public Producer(ProductPool productpool){
        this.productpool = productpool;
    }
    @Override
    public void run() {
        int i = 1;
        for (int j = 1; j <= 500; j++) {
            Product product = new Product("产品名称: " + i++);
            this.productpool.push(product);    // 生产者把生产的产品放进吧台缓冲区
            System.out.println("生产者生产了产品 "+product.getName());
            //  product.getname() ———产品名称: +i++
        }
    }
}

消费者

public class Consumer implements Runnable{
    //   消费者
    public ProductPool productPool;
    public Consumer(ProductPool productPool){
        this.productPool = productPool;
    }
    @Override
    public void run() {
        while(true){
            Product product = this.productPool.pop(); // 从缓冲区取出产品
            System.out.println("消费者消费了产品 "+product.getName());
        }
    }
}

测试类

import java.util.LinkedList;
public class MainTest {
    public static void main(String[] args) {
        // 实例化缓冲区
        ProductPool productPool = new ProductPool(5, new LinkedList<Product>());
        //  实例化生产者
        Producer producer = new Producer(productPool);
        Thread thread = new Thread(producer);
        thread.start();
        // 实例化消费者
        Consumer consumer = new Consumer(productPool);
        Thread thread2 = new Thread(consumer);
        thread2.start();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值