1.线程的创建方法
继承Thread类
实现Runnable接口(重写run方法)
实现Callable接口(重写call方法)
2.静态代理
简单来说,静态代理就是代理对象和目标对象实现同样的接口,代理对象会将目标对象传递进来,用目标对象来调用方法并添加自己的操作。
new Thread(()->System.out.println(“通过lambda表达式重写run方法”))
也就是说,Thread里的target接受目标对象,并且Thread的run方法使用target的run方法
3.线程的五大状态
新建状态、就绪状态、运行状态、阻塞状态、终止状态
4.线程的常用方法
睡眠sleep
礼让yield
插队join
优先级setPriority getPriority
状态getState
守护线程setDaemon(true)
停止stop(建议设置flag进行控制)
5.同步和锁
synchronized 可同步代码块和方法
synchorinized() {}
lock 只能用于代码块
ReentrantLock lock = new ReentrantLock();
try {
lock.lock();
//执行操作
} finally {
lock.unlock();
}
CopyOnWriteArrayList
6.死锁的四个条件
互斥条件、请求和保持条件、不剥夺条件、循环等待条件
7.线程池
Executors和ExecutorService
1、创建服务newFixedThreadPool
2、执行操作execute submit
3、获取结果 get
4、关闭服务 shutdown
8.线程通信
生产者消费者问题
管程法(缓冲区)和信号灯法(flag)
public class TestPC {
public static void main(String[] args) {
Container container = new Container();
Producer producer = new Producer(container);
Consumer consumer = new Consumer(container);
producer.start();
consumer.start();
}
}
class Producer extends Thread {
Container container;
Producer(Container 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 {
Container container;
Consumer(Container container) {
this.container = container;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
Chicken chicken = container.pop();
System.out.println("消费了第" + chicken.id + "只鸡");
}
}
}
class Chicken {
int id;
public Chicken(int id) {
this.id = id;
}
}
class Container {
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 {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count--;
Chicken chicken = chickens[count];
this.notifyAll();
return chicken;
}
}
public class TestPC2 {
public static void main(String[] args) {
TV tv = new TV();
new Actor(tv).start();
new Audience(tv).start();
}
}
class Actor extends Thread {
TV tv;
Actor(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("精绝古城");
}
}
}
}
class Audience extends Thread {
TV tv;
Audience(TV tv) {
this.tv = tv;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
this.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("观众观看了了" + this.voice);
this.notifyAll();
this.flag = !this.flag;
}
}
狂神b站视频地址:https://www.bilibili.com/video/BV1V4411p7EF