Java多线程
- 本系列博客为学习Java多线程(狂神说)时所做的笔记
- 本篇博客讲了生产者消费者,但他不是设计模式中的一个
线程协作
生产者消费者问题
线程通信
-
应用场景
- 假设仓库只能存放一件产品,生产者将生产出来的产品放入仓库,消费者从仓库中取走产品
- 如果仓库中没有产品,则生产者将产品放入仓库,否则停止生产并等待,直到消费者把产品从仓库在取走
- 如果仓库在存有产品,则消费者将产品从仓库取出,否则停止消费并等待,指导仓库中再次放入产品为止
这是一个线程同步问题,生产者和消费者共享同一个资源,并且消费者和生产者之间互为依赖,互为条件。
-
对于生产者,没有生产产品之前,要通知消费者等待,而产生了产品后,有需要通知消费者消费
-
对于消费者,在消费之后,要通知生产者结束消费
-
在这个问题中,仅有synchronized是不够的
- synchronized可阻止并发更新同一个共享资源,实现了同步
- synchronized不能用来实现不同线程之间的消息传递(通信)
-
Java提供了几个方法解决线程之间的问题
- wait():表示线程一直等待,直到其他线程通知,和sleep不同,wait会释放锁,在等待的时候不会抱着锁睡觉
- wait(long timeout):等待的毫秒数
- notify():唤醒一个处于等待状态的锁
- notifyAll():唤醒同一个对象上所有调用wait()方法的线程,优先级别高的线程优先调度
以上方法均为Object类的方法,都只能在同步方法或者同步代码块中使用,否则会抛出异常
解决方法1:
并发协作模型“生产者/消费者模式”---->管程法
- 生产者:负责生成数据的模块(可能是方法、对象、进程、线程)
- 消费者:负责处理数据的模块(可能是方法、对象、进程、线程)
- 缓冲区:消费者不能直接使用生产者的数据,他们之间有个“缓冲区”
public class TestPC {
public static void main(String[] args) {
SynContainer container = new SynContainer();
new Producer(container).start();
new Consumer(container).start();
}
}
class Producer extends Thread{
SynContainer container;
public Producer(SynContainer container){
this.container = container;
}
@Override
public void run(){
for (int i = 0; i < 100; i++) {
System.out.println("生产了"+i+"只鸡");
container.push(new Chicken(i));
}
}
}
class Consumer extends Thread{
SynContainer container;
public Consumer(SynContainer container){
this.container = container;
}
@Override
public void run(){
for (int i = 0; i < 100; i++) {
System.out.println("消费了-->"+container.pop().id+"只鸡");
}
}
}
class Chicken{
int id;
public Chicken(int id){
this.id = id;
}
}
class SynContainer{
Chicken[] chickens = new Chicken[10];
int count = 0;
public synchronized void push(Chicken chicken){
if (count == chickens.length){
try{
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果没有满,就需要丢入产品
chickens[count] = chicken;
count++;
this.notifyAll();
}
public synchronized Chicken pop(){
if (count == 0){
try{
wait();
//等待
} catch (InterruptedException e){
e.printStackTrace();
}
}
count--;
Chicken chicken = chickens[count];
this.notifyAll();
return chicken;
}
}
解决方法2:
- 并发协助模型---->信号灯法:设置一个标志位
public class TestPC2 {
public static void main(String[] args) {
TV tv = new TV();
new Player(tv).start();
new Watcher(tv).start();
}
}
class Player extends Thread{
TV tv;
public Player(TV tv){
this.tv = tv;
}
@Override
public void run(){
for (int i = 0; i < 20; i++) {
if (i%2==0){
this.tv.play("快乐大本营播放中");
} else {
this.tv.play("抖音:xxxx");
}
}
}
}
class Watcher extends Thread{
TV tv;
public Watcher(TV tv){
this.tv = tv;
}
@Override
public void run(){
for (int i = 0; i < 20; i++) {
tv.watch();
}
}
}
class TV{
String voice;
boolean flag = true;
public synchronized void play(String voice){
if (!flag){
try {
this.wait();
} catch (InterruptedException e){
e.printStackTrace();
}
}
System.out.println("演员表演了:"+voice);
this.notifyAll();
this.voice = voice;
this.flag = !this.flag;
}
public synchronized void watch(){
if(flag){
try {
this.wait();
} catch (InterruptedException e){
e.printStackTrace();
}
}
System.out.println("观看了:"+voice);
this.notifyAll();
this.flag = !this.flag;
}
}
线程池
-
背景:经常创建和销毁、使用量特别大的资源,比如并发情况下的线程,对性能影响很大
-
思路:提前创建好多个线程,放入线程池中,使用时直接获取,使用完放回池中。避免频繁创建和销毁、实现重复利用。
-
好处:
-
提高了响应速度(减少了创建新线程的时间)
-
降低了资源消耗(重复利用线程池中线程,不需要每次都创建)
-
便于线程管理(。。。)
- corePoolSize:核心池的大小
- maximumPoolSize:最大线程池
- keepAliveTime:线程没有任务时最多保持多长时间后会终止
-
使用线程池
- JDK 5.0 起提供了线程池相关API:ExecutorService和Executors
- ExecutorService:真正的线程池接口。常见子类ThreadPoolExecutor
- void execute(Runnable command):执行任务/命令,没有返回值,一般用来执行Runnable
Future submit(Callable task):执行任务,有返回值,一般又来执行Callable - void shutdown(): 关闭连接池
- Executors: 工具类、线程池的工厂类,用于创建并返回不同类型的线程池
public class TestPool {
public static void main(String[] args) {
//创建服务,创建线程池,参数为线程池大小
ExecutorService service = Executors.newFixedThreadPool(10);
//执行
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
//关闭
service.shutdown();
}
}
class MyThread implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}