笔记总结来自狂神多线程
目录
什么是线程
程序:指令和数据的有序集合
进程:执行程序的一次执行方法。是系统资源分配的单位
线程:一个进程里有若干线程,至少有一个。它是CPU调度和执行的单位
注意:很多多线程是模拟出来的,真正的多线程是指有多个cpu,即多核,如服务器。如果是模拟出来的多线程,即在一个CPU的情况下,同一时间,CPU只能执行一个代码,因为切换的很快,所以有同时执行的错觉
线程创建
public class ThreadNew {
public static void main(String[] args) throws ExecutionException, InterruptedException {
new MyThread1().start();
new Thread(new MyThread2()).start(); //Thread代理
FutureTask<Integer> futureTask = new FutureTask<Integer>(new MyThread3());
new Thread(futureTask).start();
Integer integer = futureTask.get();
System.out.println(integer);
}
}
//1.继承Thread类
class MyThread1 extends Thread{
@Override
public void run() {
System.out.println("MyThread1");
}
}
//2. 实现runnable接口
class MyThread2 implements Runnable{
@Override
public void run() {
System.out.println("MyThread2");
}
}
//3.callable接口实现
class MyThread3 implements Callable<Integer>{
@Override
public Integer call() throws Exception {
System.out.println("MyThread3");
return 100;
}
总结:不建议使用Thread,避免oop单继承局限性,建议使用runnable接口
线程执行
在主线程中执行子线程,主线程继续执行,子线程也一起执行,他们并行交替执行
总结:线程开启并不一定是立即执行,由CPU调度执行
例子:网图下载
//网图下载
public class TestThread2 extends Thread{
private String url; //网络图片地址
private String name; //保存的文件名
public TestThread2(String url,String name){
this.url = url;
this.name = name;
}
@Override
public void run() {
WebDownloader webDownloader = new WebDownloader();
System.out.println("下载了文件名为:"+name);
try {
webDownloader.downloader(url,name);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
TestThread2 testThread1 = new TestThread2("https://profile-avatar.csdnimg.cn/6d19553e2a4c4e0ba95b514f2797f927_qq_54799493.jpg","1.jpg");
TestThread2 testThread2 = new TestThread2("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202107%2F09%2F20210709142454_dc8dc.thumb.1000_0.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1659167009&t=2bd714e763420e37119746babc117797","2.jpg");
TestThread2 testThread3 = new TestThread2("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202108%2F21%2F20210821060555_67898.thumb.1000_0.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1659167009&t=cfb9aa917367b678977ee4c2fd6ab4b7","3.jpg");
TestThread2 testThread4 = new TestThread2("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202107%2F17%2F20210717232533_2edcf.thumb.1000_0.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1659167009&t=9248fdce9284d001290cae4d98d6a7c6","4.jpg");
testThread1.start();
testThread2.start();
testThread3.start();
testThread4.start();
}
}
//下载器
class WebDownloader{
//下载方法
public void downloader(String url,String name) throws IOException {
FileUtils.copyURLToFile(new URL(url),new File(name));//FileUtils.copyURLToFile 把网络上的url地址变成一个文件
}
}
结果:
解析:并不是按照调用的顺序下载,而是根据大小,每次执行都不一定一样。
并发问题
例题 抢票
public class TestThread3 implements Runnable{
private int ticketNums = 10;
@Override
public void run() {
while (true){
if (ticketNums<=0){
break;
}
try {
Thread.sleep(100); //模拟延时
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"--->拿到了第===>"+ticketNums+"票");//Thread.currentThread().getName() 拿到当前线程的名字
ticketNums--;
}
}
public static void main(String[] args) {
TestThread3 ticket = new TestThread3();
new Thread(ticket,"小明").start();
new Thread(ticket,"老师").start();
new Thread(ticket,"黄牛党").start();
new Thread(ticket,"小李").start();
}
结果:
明显他们抢到了同一张票
例题2 龟兔赛跑
//模拟龟兔赛跑
public class Race implements Runnable{
//胜利者
private static String winner;
@Override
public void run() {
for (int i = 1; i <= 100; i++) {
//模拟兔子休息
if (Thread.currentThread().getName().equals("兔子")){
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"--->跑了"+i+"步");
//判断比赛结束没
boolean flag = gameOver(i);
if (flag){//比赛结束,返回程序
break;
}
}
}
//判断是否完成比赛
private Boolean gameOver(int steps){
//判断是否有胜利者
if (winner!=null){
return true;
}
if (steps>=100){
winner = Thread.currentThread().getName();
System.out.println(" winner is "+winner);
return true;
}
return false;
}
public static void main(String[] args) {
Race race = new Race();
new Thread(race,"兔子").start();
new Thread(race,"乌龟").start();
}
结果:
代理模式
静态代理
package com.xie.proxy;
//静态代理模式总结
//真实对象和代理对象都要实现同一个接口
//代理对象要代理真实角色
//好处:代理对象可以做很多真实对象做不了的事情
//真实对象只要专注做自己的事情
public class StaticProxy {
public static void main(String[] args) {
new Thread(()-> System.out.println("我爱你")).start();//Lamda表达式
new WeddingCompany(new You()).HappyMarry();
}
}
interface Marry{
void HappyMarry();
}
//真实角色,你去结婚
class You implements Marry{
@Override
public void HappyMarry() {
System.out.println("你要结婚了");
}
}
//代理对象,帮助你结婚
class WeddingCompany implements Marry{
private Marry target;
public WeddingCompany(Marry target){
this.target = target;
}
@Override
public void HappyMarry() {
before();
this.target.HappyMarry();
after();
}
private void after() {
System.out.println("结婚之后,收尾款");
}
private void before() {
System.out.println("结婚之前,布置现场");
}
}
结果:
线程状态
- 创建状态,就绪状态,阻塞状态,运行状态,死亡状态
线程方法
线程停止
package com.xie.state;
//测试stop
//1.建议线程正常停止---》利用次数,不建议死循环
//2.建议使用标志位----》设置一个标志位
//3.不要使用stop或者destroy等过时或者jdk不建议使用的方法
public class TestStop implements Runnable{
//1.设置一个标志位
private boolean flag = true;
@Override
public void run() {
int i =0;
while (flag){
System.out.println("run....Thread "+i++);
}
}
//设置用法公开的方法停止线程,转换标志位
public void stop(){
this.flag = false;
}
public static void main(String[] args) {
TestStop testStop = new TestStop();
new Thread(testStop).start();
for (int i = 0; i < 1000; i++) {
System.out.println("main "+i);
if(i==900){
//调用stop方法切换标志位,让线程停止
testStop.stop();
System.out.println("线程停止了");
}
}
}
}
线程休眠
/**
* 每一秒获取当前时间
*/
public class Demo18_SleepThread3 {
public static void main(String[] args) {
//获取系统当前时间
Date startTime = new Date(System.currentTimeMillis());
while (true) {
try {
Thread.sleep(1000);
//更新系统时间
System.out.println(new SimpleDateFormat("HH:mm:ss").format(startTime));
startTime = new Date(System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
线程礼让
线程插队
底层是使用wait方法
package com.xie.state;
public class TestJoin implements Runnable{
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println("线程VIP来了"+i);
}
}
public static void main(String[] args) throws InterruptedException {
//启动线程
TestJoin testJoin = new TestJoin();
Thread th = new Thread(testJoin);
th.start();
//主线程
for (int i = 0; i < 500; i++) {
if (i==200){
th.join(); //插队
}
System.out.println("main "+i);
}
}
}
线程状态观测
package com.xie.state;
//观察测试线程的状态
public class TestState {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(()->{
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("=======");
});
//观测状态
Thread.State state = thread.getState();
System.out.println(state); //New
//观测启动后
thread.start();
state = thread.getState();
System.out.println(state); //Run
while (state != Thread.State.TERMINATED){//只要线程不终止,就一直输出状态
Thread.sleep(100);
state = thread.getState();
System.out.println(state); //Run
}
thread.start();
}
}
结果:
因为线程死亡后不能再次启动,所以报错
线程优先级
package com.xie.state;
public class TestPriority {
public static void main(String[] args) {
//主线程默认优先级
System.out.println(Thread.currentThread().getName()+"---->"+Thread.currentThread().getPriority());
MyPriority myPriority = new MyPriority();
Thread t1 = new Thread(myPriority);
Thread t2 = new Thread(myPriority);
Thread t3 = new Thread(myPriority);
Thread t4 = new Thread(myPriority);
Thread t5 = new Thread(myPriority);
Thread t6 = new Thread(myPriority);
//先设置优先级再启动
t1.start();
t2.setPriority(1);
t2.start();
t3.setPriority(4);
t3.start();
t4.setPriority(Thread.MAX_PRIORITY);//MAX_PRIORITY=10
t4.start();
t5.setPriority(8);
t5.start();
t6.setPriority(7);
t6.start();
}
}
class MyPriority implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"---->"+Thread.currentThread().getPriority());
}
}
守护线程
package com.xie.state;
//测试守护线程
// 上帝守护你
public class TestDaemon {
public static void main(String[] args) {
God god = new God();
You you = new You();
Thread thread = new Thread(god);
thread.setDaemon(true); //默认是false表示是用户线程,正常线程都是用户线程,这里设置为守护线程
thread.start(); //上帝守护线程启动
new Thread(you).start(); // 你 用户线程启动
}
}
//上帝
class God implements Runnable{
@Override
public void run() {
while (true){
System.out.println("上帝保护你");
}
}
}
//你
class You implements Runnable{
@Override
public void run() {
for (int i = 0; i < 36500; i++) {
System.out.println("你一直开心的活着");
}
System.out.println("======goodbye ! world !====");
}
}
结果:因为虚拟机停止还需要时间,所以结束后还运行了一会
gc(垃圾回收线程)就是守护线程
线程同步
线程倒置:短线程优先级低的和长线程优先级高的因为锁导致短线程一直执行不了(破坏了短线程优先),引发性能问题
不安全案例
package com.xie.syn;
//线程不安全集合
import java.util.ArrayList;
import java.util.List;
public class UnSafeList {
public static void main(String[] args) throws InterruptedException {
List<String> list = new ArrayList<>();
for (int i = 0; i < 10000; i++) {
new Thread(()->{
list.add(Thread.currentThread().getName());
}).start();
}
Thread.sleep(1000);
System.out.println(list.size());
}
}
同步方法
注意:不能锁run方法,无效,因为synchronized 同步方法,锁的是this
这个就能锁run,因为对象就是在BuyTicket1执行的
package com.xie.syn;
//安全买票
public class Demo27_SafeBuyTicket {
public static void main(String[] args) {
BuyTicket1 buyTicket = new BuyTicket1();
new Thread(buyTicket, "张三").start();
new Thread(buyTicket, "李四").start();
new Thread(buyTicket, "王五").start();
}
}
class BuyTicket1 implements Runnable {
//票
private int ticketNums = 10;
boolean flag = true;
@Override
public void run() {
//买票
while (flag) {
try {
buy();
} catch (Exception e) {
e.printStackTrace();
}
}
}
//synchronized 同步方法,锁的是this
private synchronized void buy() {
//判断是否有票
if (ticketNums <= 0) {
flag = false;
return;
}
//延迟
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
//买票
System.out.println(Thread.currentThread().getName() + "拿到" + ticketNums--);
}
}
同步块
public class UnSafeList {
public static void main(String[] args) throws InterruptedException {
List<String> list = new ArrayList<>();
for (int i = 0; i < 10000; i++) {
new Thread(()->{
synchronized (list){
list.add(Thread.currentThread().getName());
}
}).start();
}
Thread.sleep(1000);
System.out.println(list.size());
}
/**
* 安全的取钱 同步块
*/
public class Demo28_SafeBank {
public static void main(String[] args) {
Account1 account = new Account1(100, "结婚基金");
Drawing1 you = new Drawing1(account, 50, "展堂");
Drawing1 girlfriend = new Drawing1(account, 100, "sad");
you.start();
girlfriend.start();
}
}
//账户
class Account1 {
int money;//余额
String cardName;//卡名
public Account1(int money, String cardName) {
this.money = money;
this.cardName = cardName;
}
}
//银行:模拟取款
class Drawing1 extends Thread {
Account1 account;//账户
int drawingMoney;//取金额
int nowMoney;//你手里的钱
public Drawing1(Account1 account, int drawingMoney, String name) {
super(name);
this.account = account;
this.drawingMoney = drawingMoney;
}
//取钱
@Override
public void run() {
//锁的对象就是变量的量,需要增删改查的对象
synchronized (account) {
//判断是否有钱
if (account.money - drawingMoney < 0) {
System.out.println(Thread.currentThread().getName() + "余额不足,不能进行取钱");
return;
}
try {
Thread.sleep(1000);//放大问题的发生性
} catch (InterruptedException e) {
e.printStackTrace();
}
//卡内金额 = 余额-你的钱
account.money = account.money - drawingMoney;
//你手里的钱
nowMoney = nowMoney + drawingMoney;
System.out.println(account.cardName + "余额为:" + account.money);
//this.getName()==Thread.currentThread().getName()
System.out.println(this.getName() + "手里的钱:" + nowMoney);
}
}
死锁
package com.xie.Lock;
//死锁:多个线程互相持有对方需要的资源,然后僵持
public class DeadLock {
public static void main(String[] args) {
Makeup g1 = new Makeup(0,"灰姑凉");
Makeup g2 = new Makeup(1,"白雪公主");
g1.start();
g2.start();
}
}
//口红
class Lipstick{
}
//镜子
class Mirror{
}
class Makeup extends Thread{
//需要的资源只有一份,用static来保证只有一份
static Lipstick lipstick = new Lipstick();
static Mirror mirror = new Mirror();
int choice;//选择
String girlName;//使用化妆品的人
Makeup(int choice,String girlName){
this.choice = choice;
this.girlName = girlName;
}
@Override
public void run() {
//化妆
try {
makeup();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//化妆,互相持有对方的锁,持有对方资源
private void makeup() throws InterruptedException {
if (choice == 0){
synchronized (lipstick){//获得口红的锁
System.out.println(this.girlName+"获得口红的锁");
Thread.sleep(10000);
synchronized (mirror){//一秒后想获得镜子的锁
System.out.println(this.girlName+"获得镜子的锁");
}
}
}else {
synchronized (mirror){//获得口红的锁
System.out.println(this.girlName+"获得镜子的锁");
Thread.sleep(1000);
synchronized (lipstick){//一秒后想获得镜子的锁
System.out.println(this.girlName+"获得口红的锁");
}
}
}
}
}
解决:把锁拿出来,执行完锁后一定时间会释放掉锁,等灰姑凉时间到了放锁出来,白雪公主就立刻执行,然后结束释放锁,灰姑凉再接着执行
package com.xie.Lock;
//死锁:多个线程互相持有对方需要的资源,然后僵持
public class DeadLock {
public static void main(String[] args) {
Makeup g1 = new Makeup(0,"灰姑凉");
Makeup g2 = new Makeup(1,"白雪公主");
g1.start();
g2.start();
}
}
//口红
class Lipstick{
}
//镜子
class Mirror{
}
class Makeup extends Thread{
//需要的资源只有一份,用static来保证只有一份
static Lipstick lipstick = new Lipstick();
static Mirror mirror = new Mirror();
int choice;//选择
String girlName;//使用化妆品的人
Makeup(int choice,String girlName){
this.choice = choice;
this.girlName = girlName;
}
@Override
public void run() {
//化妆
try {
makeup();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//化妆,互相持有对方的锁,持有对方资源
private void makeup() throws InterruptedException {
if (choice == 0){
synchronized (lipstick){//获得口红的锁
System.out.println(this.girlName+"获得口红的锁");
Thread.sleep(10000);
}
synchronized (mirror){//一秒后想获得镜子的锁
System.out.println(this.girlName+"获得镜子的锁");
}
}else {
synchronized (mirror){//获得口红的锁
System.out.println(this.girlName+"获得镜子的锁");
Thread.sleep(1000);
}
synchronized (lipstick){//一秒后想获得镜子的锁
System.out.println(this.girlName+"获得口红的锁");
}
}
}
}
结果:
Lock(锁)
package com.xie.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class TestLock {
public static void main(String[] args) {
TestLock2 testLock2 = new TestLock2();
new Thread(testLock2).start();
new Thread(testLock2).start();
new Thread(testLock2).start();
}
}
class TestLock2 implements Runnable{
int ticket = 10;
//定义lock锁
private final ReentrantLock lock = new ReentrantLock();
@Override
public void run() {
while (true){
try {
lock.lock();//加锁
if (ticket>0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(ticket--);
}else {
break;
}
}finally {
//解锁
lock.unlock();
}
}
}
}
线程协作
生产者消费者模式
sleep不释放锁,wait会释放
管程法:
package com.xie.communication;
//测试:生产者消费者模型--》利用缓冲区解决:管程发
//生产者,消费者,产品,缓冲区
public class TesyPC {
public static void main(String[] args) {
SynContainer synContainer = new SynContainer();
new Productor(synContainer).start();
new Consumer(synContainer).start();
}
}
//生产者
class Productor extends Thread{
SynContainer container;
public Productor(SynContainer container){
this.container = container;
}
//生产
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("生产了"+i+"只鸡");
try {
container.push(new Chicken(i));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//消费者
class Consumer extends Thread{
SynContainer container;
public Consumer(SynContainer container){
this.container = container;
}
//消费
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
System.out.println("消费了--》"+container.pop().id+"只鸡");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//产品
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) throws InterruptedException {
//如果容器满了,需要等待消费者消费
if (count == chickens.length){
//生产停止
this.wait();
}
//如果没满,继续生产
chickens[count++] = chicken;
//可以通知消费者消费
this.notifyAll();
}
//消费者消费产品
public synchronized Chicken pop() throws InterruptedException {
//判断能否消费
if (count ==0){
//等待生产者生产
this.wait();
}
//如果可以消费
count--;
Chicken chicken = chickens[count];
//吃完了,通知生产者生产
this.notifyAll();
return chicken;
}
}
信号灯法
package com.xie.communication;
//测试:生产者消费者模型--》利用缓冲区解决:信号灯法,标志位解决
public class TestPC2 {
public static void main(String[] args) {
Home h =new Home() ;
new bao(h).start();
new Phone(h).start();
}
}
//生产者--》充电宝
class bao extends Thread{
Home h;
public bao(Home h){
this.h = h;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
try {
this.h.charge("第"+i+"格电");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//消费者--》手机
class Phone extends Thread{
Home h;
public Phone(Home h){
this.h = h;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
try {
h.producer();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//产品--》电
class Home{
//充电宝充电,手机等待 T
//手机充电,充电宝等待 F
String ele; //电
boolean flag =true;
//给充电宝充电
public synchronized void charge(String ele) throws InterruptedException {
if (!flag){
this.wait();
}
System.out.println("充电宝充电"+ele);
//通知手机充电
this.notifyAll();
this.ele = ele;
this.flag = !flag;
}
//充电宝给手机充电
public synchronized void producer() throws InterruptedException {
if (flag){
this.wait();
}
System.out.println("充电宝给手机充了"+ele);
//通知手机充电
this.notifyAll();
this.flag = !flag;
}
}
上面例子不太好理解举个新例子
public class 多线程通信 {
public static void main(String[] args) {
Cake cake =new Cake() ;
new Cooker(cake).start();
new Customer(cake).start();
}
}
//生产者--》糕点师
class Cooker extends Thread{
Cake cake;
public Cooker(Cake cake){
this.cake = cake;
}
@Override
public void run() {
for (int i = 1; i <= 80; i++) {
try {
// sleep(1000); //测试厨师延迟
this.cake.producer(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//消费者--》客户
class Customer extends Thread{
Cake cake;
public Customer(Cake cake){
this.cake = cake;
}
@Override
public void run() {
for (int i = 1; i <= 60; i++) {
try {
// sleep(1000); //测试客户延迟
cake.consume(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//产品--》蛋糕
class Cake{
boolean flag =true; //t:客户等待,唤醒厨师,反之
int co; //用户买的蛋糕数
int pro; //厨师做的蛋糕数
int count;
//客户买蛋糕
public synchronized void consume(int c) throws InterruptedException {
this.co = c;
this.count =pro-co;
if (this.count<=0){
this.flag = true;
}
if (flag){
this.wait();
}
System.out.println("客户买了"+c+"个蛋糕");
this.count =pro-co; //因为有可能count还是之前厨师未做蛋糕时的值
System.out.println("店里有"+this.count+"个蛋糕");
this.notifyAll();
}
//厨师做蛋糕
public synchronized void producer(int p) throws InterruptedException {
this.pro = p;
System.out.println("厨师做了"+p+"个蛋糕");
if(this.pro-this.co==50){//店里有50个蛋糕了
this.flag = !flag;
System.out.println("店里有50个蛋糕");
}
if (!flag){
this.wait();
}
this.notifyAll();
}
}
线程池
package com.xie.pool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class pool {
public static void main(String[] args) {
//1.创建服务,创建线程池
//newFixedThreadPool 参数:为线程池大小
ExecutorService service = Executors.newFixedThreadPool(10);
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
//2.关闭线程池
service.shutdown();
}
}
class MyThread implements Runnable{
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()+" "+i);
}
}
}
结果:
Redis锁
public Result<?> zaiXianYuYueGaoBingFa(ZaiXianYuYue zaiXianYuYue, String weekend, String yueTai) throws ExecutionException, InterruptedException {
//使用redis加锁
// 尝试获取锁
String lockKey = "zaiXianYuYue_lock";
String clientId = zaiXianYuYue.getTiSongDanHao();
Semaphore semaphore = new Semaphore(20);//限流
long start = System.currentTimeMillis();
try {
// return zaiXianYuYueService.zaiXianYuYue(zaiXianYuYue);
//获取当前线程的本地变量
Map<ThreadLocal, Object> threadLocalMap = ThreadLocalUtil.getThreadLocalMap();
ThreadPoolTaskExecutor threadPoolTaskExecutor = threadPooTaskUtil.taskExecutorxjc();
Future<Result<?>> submit = threadPoolTaskExecutor.submit(new Callable<Result<?>>() {
@Override
public Result<?> call() throws Exception {
semaphore.acquire();
//需要重新设置线程池内线程的线程本地变量为主线程的变量
ThreadLocalUtil.resetThreadLocals(threadLocalMap);
while (!redisTemplate.opsForValue().setIfAbsent(lockKey, clientId, Duration.ofSeconds(10))) {
//获取锁失败,循环等待
if (System.currentTimeMillis() - start >= 5000) {
System.err.println("***");
System.err.println(System.currentTimeMillis() - start);
return Result.error("等待超时,请重新请求");
}
}
return yuYue(zaiXianYuYue, weekend, yueTai);
}
});
return submit.get();
} finally {
semaphore.release();
// 释放锁
String currentClientId = (String) redisTemplate.opsForValue().get(lockKey);
if (clientId.equals(currentClientId)) {
redisTemplate.delete(lockKey);
}
//发送模板消息
// sendTemplateMessageService.yuYueChengGong(zaiXianYuYue);
}
}