Day18 学习java(死锁,线程通信,线程池,单例模式)

死锁

概述

双方都持有对方需要的资源,同时双方又不肯主动交出自己拥有的对方所需要的资源

代码实现

public class Thread_01 {

	public static void main(String[] args) {
		Object o1 = new Object();
		Object o2 = new Object();
		Thread t1 = new Thread(new P1(o1, o2));
		Thread t2 = new Thread(new P2(o1, o2));
		t1.setName("t1");
		t2.setName("t2");
		t1.start();
		t2.start();
	}

}

class P1 implements Runnable {
	Object o1;
	Object o2;

	public P1(Object o1, Object o2) {
		this.o1 = o1;
		this.o2 = o2;
	}

	@Override
	public void run() {
		synchronized (o1) {
			try {
				// 睡一下,确保o2被锁
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			synchronized (o2) {
				System.out.println(Thread.currentThread().getName() + "执行完成");
			}
		}
	}

}

class P2 implements Runnable {
	Object o1;
	Object o2;

	public P2(Object o1, Object o2) {
		this.o1 = o1;
		this.o2 = o2;
	}

	@Override
	public void run() {
		synchronized (o2) {
			try {
				// 睡一下,确保o1被锁
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			synchronized (o1) {
				System.out.println(Thread.currentThread().getName() + "执行完成");
			}
		}
	}

}

在这里插入图片描述
程序永远运行,一直没有想要的输出结果

线程通信

概述

wait 让当前在该对象的线程进入等待状态,会释放持有的锁

无参或者传入0 表示一直等待,不会自动唤醒,只能等着notify唤醒它。也可以传入long类型的值,类似于sleep,时间到了自己唤醒wait和notify的使用时序非常重要,wait执行后原地等待,直到被唤醒之后继续执行(资源足够的时候)

使用方式

需求:打印奇数和偶数
1.有一个业务类,有一个打印奇数和打印偶数的方法
2 有一个变量 count 记录当前的数字
3 两个线程,分别调用打印奇数和打印偶数的方法

public class Thread_02 {

	public static void main(String[] args) {
		Num num = new Num();
		Thread t1 = new PrintE(num);
		Thread t2 = new PrintO(num);
		t1.start();
		t2.start();

	}

}

// 有一个业务类,有一个打印奇数和打印偶数的方法
class Num {
	// 有一个变量 count 记录当前的数字
	private int count = 1;

	public synchronized void printEven() {
		System.out.println("奇数" + count++);
		this.notify();
		try {
			Thread.sleep(1000);
			this.wait();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		// System.out.println("wait" + count);

	}

	public synchronized void printOdd() {
		System.out.println("偶数" + count++);
		this.notify();
		try {
			Thread.sleep(1000);
			this.wait();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}
}

// 打印奇数
class PrintE extends Thread {
	Num num;

	public PrintE(Num num) {
		super();
		this.num = num;
	}

	@Override
	public void run() {
		while (true) {
			num.printEven();
		}
	}

}

// 打印偶数
class PrintO extends Thread {
	Num num;

	public PrintO(Num num) {
		super();
		this.num = num;
	}

	@Override
	public void run() {
		while (true) {
			num.printOdd();
		}
	}

}

在这里插入图片描述

生产者和消费者

https://blog.csdn.net/qq_39575279/article/details/87940298
小练:
1 一个业务类SynStack其中有一个变量,用来保存已生产的元素个数
2 业务类中有一个char数组,用于保存生产的元素(假如只生产a-z这些字母)
3 业务类中需要有两个方法,一个是生产push,一个消费pop

public class Lwork {
	public static void main(String[] args) {
		SynStack ss = new SynStack();
		Producer pro = new Producer(ss);
		Consumer con = new Consumer(ss);
		Thread t1 = new Thread(pro);
		Thread t2 = new Thread(con);
		t1.setName("t1");
		t2.setName("t2");
		t1.start();
		t2.start();
	}
}

// 1 一个业务类SynStack其中有一个变量,用来保存已生产的元素个数
class SynStack {
	// 2 业务类中有一个char数组,用于保存生产的元素(假如只生产a-z这些字母)
	private char[] chars = new char[10];
	// 容量
	private int full = 10;
	// 个数
	private int empty = 0;

	// 3 业务类中需要有两个方法,一个是生产push,一个消费pop
	public synchronized void push(char c) {
		// push方法主要用于向数组中添加数据。个数要+1,还要判断是否添加满了,满了就挂起进入等待
		if (full == 0) {
			this.notify();
			try {
				System.out.println(Thread.currentThread().getName() + "wait");
				this.wait();
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			System.out.println("生产者添加了一个" + c);
			chars[empty++] = c;
			full--;
			this.notify();
		}
	}

	public synchronized char pop() {
		// pop方法主要用于取出数组中数据。个数要-1,还要判断是否消费完了,完了就挂起进入等待
		if (empty == 0) {
			this.notifyAll();
			try {
				System.out.println(Thread.currentThread().getName() + "wait");
				this.wait();
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			char c = chars[--empty];
			System.out.println("消费者取出了一个" + c);
			full++;
			this.notify();
			return c;
		}
		return 'A';
	}
}

// 负责生产
class Producer implements Runnable {
	SynStack ss;

	public Producer(SynStack ss) {

		this.ss = ss;
	}

	@Override
	public void run() {
		Random r = new Random();
		while (true) {
			try {
				Thread.sleep(1000);
			} catch (Exception e) {
				e.printStackTrace();
			}
			char c = (char) (r.nextInt(26) + 97);
			ss.push(c);
		}
	}

}

// 负责消费
class Consumer implements Runnable {
	SynStack ss;

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

	@Override
	public void run() {
		while (true) {
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			ss.pop();
		}
	}

}

在这里插入图片描述

线程池

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

思路:提前创建好多个线程,放入线程池中,使用时直接获取,使用完 放回池中。可以避免频繁创建销毁、实现重复利用。类似生活中的公共交 通工具。
好处
a) 提高响应速度(减少了创建新线程的时间)
b) 降低资源消耗(重复利用线程池中线程,不需要每次都创建)
c) 便于线程管理
i. corePoolSize:核心池的大小
ii. maximumPoolSize:最大线程数
iii. keepAliveTime:线程没有任务时最多保持多长时间后会终止

线程池API
 JDK 5.0起提供了线程池相关API:ExecutorServiceExecutors
 ExecutorService:真正的线程池接口。常见子类ThreadPoolExecutor
 void execute(Runnable command) :执行任务/命令,没有返回值,一般用来执行Runnable
 Future submit(Callable task):执行任务,有返回值,一般又来执行Callable
 void shutdown() :关闭连接池
 Executors:工具类、线程池的工厂类,用于创建并返回不同类型的线程池
 Executors.newCachedThreadPool():创建一个可根据需要创建新线程的线程池
 Executors.newFixedThreadPool(n); 创建一个可重用固定线程数的线程池
 Executors.newSingleThreadExecutor() :创建一个只有一个线程的线程池
 Executors.newScheduledThreadPool(n):创建一个线程池,它可安排在给定延迟后运行命令或者定期地执行。

单例模式

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

public class SingleLeton_01 {
	private static SingleLeton_01 singLeton = new SingleLeton_01();

	public static SingleLeton_01 getInstance() {
		return singLeton;
	}

}

test测试

public class Test_01 {

	public static void main(String[] args) {
		TestPrint t1 = new TestPrint();
		TestPrint t2 = new TestPrint();
		TestPrint t3 = new TestPrint();
		TestPrint t4 = new TestPrint();
		t1.start();
		t2.start();
		t3.start();
		t4.start();
	}

}

class TestPrint extends Thread {

	@Override
	public void run() {
		System.out.println(SingleLeton_01.getInstance());
	}

}

在这里插入图片描述
懒汉模式
存在问题

public class SingleLeton_02 {
	private static SingleLeton_02 singLeton = null;

	public static SingleLeton_02 getInstance() {
		if (singLeton == null) {
			singLeton = new SingleLeton_02();
		}
		return singLeton;
	}

}

test测试

public class Test_01 {

	public static void main(String[] args) {
		TestPrint t1 = new TestPrint();
		TestPrint t2 = new TestPrint();
		TestPrint t3 = new TestPrint();
		TestPrint t4 = new TestPrint();
		t1.start();
		t2.start();
		t3.start();
		t4.start();
	}

}

class TestPrint extends Thread {

	@Override
	public void run() {
		System.out.println(SingleLeton_02.getInstance());
	}

}

在这里插入图片描述
可以看到出现了不一样的内存地址
第一种方法就是直接把方法加上synchronized,如下
在这里插入图片描述
但是这种每次只允许一个线程判断,效率很低很低
下面提供第二种方法
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值