Lambda表达式使用前提
1、匿名内部类是接口
2、接口只有一个方法
3、方法参数一一对应
线程
等待唤醒
import com.xiye.thread.Shower;
/**
* Create by xiye on 2019/12/2 14:49
*/
public class Demo1_等待唤醒机制 {
/*
* 1、同步代码块
*
* 2、锁机制
* */
public static void main(String[] args) {
Shower shower = new Shower();
// 同步代码块
/*new Thread(new Runnable() {
@Override
public void run() {
try {
shower.show1();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
shower.show2();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();*/
// 锁机制
new Thread(() -> {
try {
shower.show3();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(() -> {
try {
shower.show4();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(() -> {
try {
shower.show5();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}
package com.xiye.thread;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/**
* Create by xiye on 2019/12/2 14:53
*/
public class Shower {
// 所对象
private ReentrantLock lock = new ReentrantLock();
// 监听器对象
private Condition c1;
private Condition c2;
private Condition c3;
private int tag = 1;
public Shower() {
c1 = lock.newCondition();
c2 = lock.newCondition();
c3 = lock.newCondition();
}
public void show1() throws InterruptedException {
for (int i = 0; i < 100; i++) {
synchronized (this) {
while (tag != 1) {
this.wait();
}
tag = 2;
this.notify();
System.out.println("同步锁测试1");
}
}
}
public void show2() throws InterruptedException {
for (int i = 0; i < 100; i++) {
synchronized (this) {
while (tag != 2) {
this.wait();
}
tag = 1;
System.out.println("同步锁测试2");
this.notify();
}
}
}
public void show3() throws InterruptedException {
for (int i = 0; i < 100; i++) {
lock.lock();
while (tag != 1) {
c1.await();
}
tag = 2;
System.out.println("Condition1");
c2.signal();
lock.unlock();
}
}
public void show4() throws InterruptedException {
for (int i = 0; i < 100; i++) {
lock.lock();
while (tag != 2) {
c2.await();
}
tag = 3;
System.out.println("Condition2");
c3.signal();
lock.unlock();
}
}
public void show5() throws InterruptedException {
for (int i = 0; i < 100; i++) {
lock.lock();
while (tag != 3) {
c3.await();
}
tag = 1;
System.out.println("Condition3");
System.out.println();
c1.signal();
lock.unlock();
}
}
}
生产者与消费者
package com.xiye.thread;
import com.xiye.bean.BaoZi;
/**
* Create by xiye on 2019/12/2 16:12
*/
public class ProducerThread implements Runnable {
private BaoZi bz;
public ProducerThread(BaoZi bz) {
this.bz = bz;
}
@Override
public void run() {
while (true) {
synchronized (bz) {
try {
if (bz.getTag() == 0) {
bz.setName("肉包子");
bz.setPrice(2.5);
bz.setTag(1);
System.out.println("生产一个" + bz.getName() + ", 价格为" + bz.getPrice());
}
bz.notify();
bz.wait();
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
package com.xiye.thread;
import com.xiye.bean.BaoZi;
/**
* Create by xiye on 2019/12/2 16:12
*/
public class ConsumerThread implements Runnable {
private BaoZi bz;
public ConsumerThread(BaoZi bz) {
this.bz = bz;
}
@Override
public void run() {
while (true) {
synchronized (bz) {
try {
if (bz.getTag() == 1) {
bz.setTag(0);
System.out.println("消费一个" + bz.getName() + ", 价格为" + bz.getPrice());
}
bz.notify();
bz.wait();
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
package com.xiye.bean;
/**
* Create by xiye on 2019/12/2 16:09
*/
public class BaoZi {
private String name;
private double price;
private int tag = 0;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getTag() {
return tag;
}
public void setTag(int tag) {
this.tag = tag;
}
}
import com.xiye.bean.BaoZi;
import com.xiye.thread.ConsumerThread;
import com.xiye.thread.ProducerThread;
/**
* Create by xiye on 2019/12/2 16:07
*/
public class Demo2_示例 {
public static void main(String[] args) {
BaoZi bz = new BaoZi();
ProducerThread producer = new ProducerThread(bz);
ConsumerThread consumer = new ConsumerThread(bz);
new Thread(producer).start();
new Thread(consumer).start();
}
}
线程状态
/**
* Create by xiye on 2019/12/2 16:42
*/
public class Demo3_线程状态 {
/*
* 1、新建
* 2、就绪:等待获取CPU
* 3、运行:获取到CPU
* 4、阻塞
* 等待阻塞:wait
* 同步阻塞:同步锁代码块
* 其他阻塞:sleep、join
* 5、死亡
* */
/*sleep和wait
*
* sleep是Thread线程方法,wait是Object对象方法
* sleep不会自动释放锁,而wait会
* sleep不依赖同步代码块,wait依赖同步代码块
* sleep不用被唤醒,wait需要被唤醒
* */
/*
* 同步锁对象,这个对象要是同一个,wait和notify的调用者也应该是该对象
* */
private static Object obj = new Object();
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
synchronized (obj) {
try {
System.out.println("wait会自动释放锁");
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
System.out.println("sleep不会自动释放锁,同步代码块没有解锁操作时,不要将sleep放在里面");
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (obj) {
obj.notify();
}
}
}
}).start();
}
}
线程池
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Create by xiye on 2019/12/2 17:33
*/
public class Demo4_线程池 {
/*线程池:线程反复利用,减少线程频繁创建对象
* 1、减低资源消耗
* 2、提高响应速度
* 3、提高线程的可管理性(调整线程池中工作线程的数目)
* */
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(2); //线程池中线程的数量
service.submit(() -> System.out.println("当前获取到的线程是" + Thread.currentThread().getName()));
service.submit(() -> System.out.println("当前获取到的线程是" + Thread.currentThread().getName()));
service.submit(() -> System.out.println("当前获取到的线程是" + Thread.currentThread().getName()));
// service.shutdown(); // 关闭线程池,一般不关闭
}
}
本文深入探讨了线程的使用与管理,包括线程的等待唤醒机制、线程状态分析、生产者与消费者模式及线程池的实现原理。通过具体示例,讲解了如何使用同步代码块和锁机制来控制线程执行,以及如何利用线程池优化资源消耗。
1407

被折叠的 条评论
为什么被折叠?



