day-22死锁,线程通信,单例模式,线程池

1死锁
    就是在执行过程中,都遇到了对方进入加锁的方法中,从而导致大家都访问不了的状态

死锁原理:

        1某一个线程执行完成,需要先后嵌套 锁定,执行俩个对象,并在这个过程中, 先锁定第一个对象 

        2另外一个线程,执行完成,先后嵌套,锁定   执行俩个对象,并且在这个过程中,先锁定第二个对象

        3在第一个线程执行到第二个对象的时候,发现已经被锁定,只能等待

        4在第二个线程执行到第一个对象的时候,发现已经被锁定只能等等待

public static void main(String[] args){
		Object o1 = new Object();
		Object o2 = new Object();
		Thread t1 = new Thread(new A1(o1,o2));
		Thread t2 = new Thread(new A2(o1,o2));
		t1.setName("t1");
		t2.setName("t2");
		t1.start();
		t2.start();
	}
}
class A1 implements Runnable{
	Object o1;
	Object o2;
	public A1(Object o1, Object o2) {
		super();
		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 A2 implements Runnable{
	Object  o1;
	Object  o2;
	public A2(Object o1, Object o2) {
		super();
		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()+"执行完成");
			}
		}
		
	}

2线程通信

    wait: 让该线程进入等待状态,会释放持有的锁

                无参或者传入0 表示一直等待 不会自动唤醒,只能等着notify唤醒

                也可以传入long类型的值,类似于sleep  时间到了自己唤醒

notify: 随机唤醒一个在该对象中正在等待的一个线程

notifyAll: 唤醒在该对象中所有等待的线程 

以上方法只能用在加锁的成员方法中

使用方式:如打印奇数和偶数

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");
			t1.start();
			t2.start();
		}
}
//打印偶数
class PrintEven extends Thread{
	Num num;

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

	public PrintOdd(Num num) {
		super();
		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) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

3生产者消费者

package Day2;

import java.util.Random;

/**
 * 生产者  消费者
 * 1 定义一个业务类SynStack ,其中有一个变量,用来保存已产生的元素个数
 * 2 业务类中有一个 char数组,用于保存生产的元素(假如只生产 a-z)
 * 3 业务类中需要有俩个方法 : 一个是生产push    一个是消费pop
 * 						push方法  主要用于向数组中添加数据
 * 										个数+1,还要判断是否添加满了,满了就挂起进入等待
 * 						pop方法  主要用于取出数组中数据
 * 										个数-1,还要判断是否消费完了,完了就挂起等待
 * @author 田彪
 * @Date 2022年1月25日-下午6:29:50
 */
public class Thread_03 {
		public static void main(String[] args){
			SynStack1 ss =new SynStack1();
			Thread p = new Thread(new Producer(ss));
			Thread c = new Thread(new Consumer(ss));
			p.start();
			c.start();
		}
 }
class Producer implements Runnable{
	SynStack1 ss;

	public Producer(SynStack1 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{
	SynStack1 ss;

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

	@Override
	public void run() {
		while(true){
		try {
			Thread.sleep(600);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		ss.pop();
	}
}
}
class SynStack1{
	//保存数据的容器
	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;
	}
	
}

4单例模式

                让某个类只实例化一个对象

       构造方法私有化,静态变量保存对象,公共的静态方法用于获取类对象

饿汉安全模式

public class Thread_04 {
	private Thread_04(){
		
	}
	//饿汉
	private static Thread_04 singLeton = new Thread_04();
	public static Thread_04 getInstance() {
		return singLeton;
	}
}

懒汉安全模式

public class Thread_05 {
	private  Thread_05(){
		
	}
//	private  static  Thread_05 singLeton = null;
//	//效率低,因为每次都需要排队
//	public synchronized static Thread_05 getInstance(){
//		if(singLeton == null){
//			singLeton = new Thread_05();
//		}
//		return singLeton;
//		
//	}
	//volatile: 加它 防止指令重排
	private volatile static  Thread_05 singLeton = null;
	//效率较高,因为只需要第一次排队
	public  static Thread_05 getInstance(){
		//双重校验
		if(singLeton == null){
			synchronized(Thread_05.class){
				if(singLeton == null){
			singLeton = new Thread_05();
		}
		}
		}
		return singLeton;
		
	}
}

5 线程池

      背景:经常创建和销毁,使用量特别大的资源, 比如并发情况下单额线程,对性能影响很大

       思路: 提前创建好多个线程,放入线程池中,使用时直接获取,使用完放回池中,可以避免频繁创建销毁,实现重复利用

        好处:

        a提高响应速度(减少了创建新线程的时间)

        b降低资源消耗(重复利用线程池中线程,不需要每次都创建)

        c便于线程管理

corePoolSize: 核心池的大小

maximumPoolSize: 最大的线程数

keepAliveTime: 线程没有任务时最多保持多长时间后会终止

6线程池API

ExecutorService: 真正的线程池接口                 常见子类:ThreadPoolExecutor

 Executors: 工具类,线程池的工厂类。用于创建并返回不同类型的线程池

7线程池作用

       限制系统中执行线程的数量

        根据系统的环境情况,可以自动或手动设置线程数量,达到运行的最佳效果

       少了浪费系统资源,多了造成系统拥挤 效率不高

为什么要用线程池

        1减少了创建和销毁线程的次数,每个工作线程都可以被重复利用,可执行多个任务

        2可以根据系统的承受能力,调整线程池中工作线线程的数目,防止因为消耗过多的内存,而把服务器累趴(每个线程大约需要1MB,线程开的越多,消耗的内存也就越大,最后死机)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值