当前位置:网站首页>腾讯面试之--请你设计一个实现线程池顺序执行
腾讯面试之--请你设计一个实现线程池顺序执行
2022-07-28 14:24:00 【evanYang_】
背景
小编可开心啦,屁颠屁颠的把ExecutorService executor = Executors.newSingleThreadExecutor();单例线程的方案给说出来啦;顺便把代码也贴上。
SingleThreadExecutor 实现
package com.evan.springboot.study;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @author evanYang
* @version 1.0
* @date 2020/5/26 上午 10:41
*/
public class ExecutorDemos {
public static void main(String[] args) {
Thread a = new Thread(){
@Override
public void run() {
System.out.println("thread a 执行");
try {
Thread.sleep(200);
System.out.println("执行ing。。。。。");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread b = new Thread(){
@Override
public void run() {
System.out.println("thread b 执行");
try {
Thread.sleep(200);
System.out.println("执行ing。。。。。");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread c = new Thread(){
@Override
public void run() {
System.out.println("thread c 执行");
System.out.println("执行ing。。。。。");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(a);
executor.execute(b);
executor.execute(c);
executor.shutdown();
}
}

我们可以看到这里了是按照顺序执行啦;很开心有木有;
面试官: 你这不就是个单线程执行吗? 有木有其他更好的idea呢?
不知道小伙伴们有没有更好的想法呢
基于信号量(Semaphore)实现
Lock和synchronized是锁的互斥,一个线程如果锁定了一个资源,那么其他线程只能等待资源的释放。也就是一次只有一个线程执行,直到这个线程执行完毕或者unlock。而Semaphore可以控制多个线程同时对每个资源的访问。Semaphore实现的功能就类似厕所有5个坑,假如有10个人要上厕所,那么同时只能有多少个人去上厕所呢?
同时只能有5个人能够占用,当五个人中的任何一个人让开后,其中等待的另外5个人又有一个人可以占用了
。另外等待的5个人是可以随机的获取优先机会,也可以是按照先来后到的顺序获得机会,这取决于构造Semaphore对象时传入的参数选项。当然单个信号量的Semaphore对象可以实现互斥锁的功能,并且可以是由一个线程获得了锁,再由另外一个线程释放锁,这块引用于死锁恢复的一些场合。信号量用在多线程多任务多任务同步的,一个线程完成了某一个动作就通过信号量告诉别的线程,别的线程在进行某些动作。也就是说Semaphore不一定是锁定某个资源,而是流程上的概念。比方说有A,B两个线程,B线程的操作可能要等A线程执行完毕之后才执行,这个任务
并不一定是锁定某一资源,还可以是进行一些计算或者数据处理之类,它们也许并不访问共享变量,只是逻辑上的先后顺序。java计数信号量(Semaphore)维护着一个许可集。调用acquire()获取一个许可,release()释放一个许可。在java中,还可以设置该信号量是否采用公平模式,如果以公平方式执行,则线程将会按到的的顺序(FIFO)执行,如果是非公平,则可以后请求的有可能排在队列的头部。
Semaphore当前在多线程环境下被扩放使用,操作系统的信号量是个很重要的概念,在进程控制方面都有运用。java并发库Semaphore可以很轻松完成信号量控制,Semaphore可以控制某个资源可被同时访问的个数。
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(5);
final Semaphore semaphore = new Semaphore(5);
for (int index = 0; index < 20; index++) {
final int NO = index;
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
semaphore.acquire();
System.out.println(Thread.currentThread().getName()+"Accessing:" + NO);
Thread.sleep((long) (Math.random() * 10000));
semaphore.release();
System.out.println("------------------" + semaphore.availablePermits());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
executorService.execute(runnable);
}
executorService.shutdown();
}

边栏推荐
- 分享一下一二线大厂HR面经分享
- Volatile principle
- 解决pycharm使用powershell出错问题
- Dj-131/60c voltage relay
- 使用cpolar发布树莓派网页(apache2网页的发布)
- Development status of security and privacy computing in China
- Crmeb knowledge paid manual installation tutorial
- Svg verification code recognition experience
- 从thinkphp远程代码执行学php反射类
- idea调试burpsuit插件
猜你喜欢

Crmeb Standard Version window+phpstudy8 installation tutorial (I)

使用cpolar发布树莓派网页(apache2的安装测试)

shellcode编写(未完)

Publish raspberry pie web page with cpolar (apache2 installation test)

Install MOSEK, license installation location search

Publish raspberry pie web page with cpolar (release of apache2 web page)

sql语句的执行流程

HJS-DE1/2时间继电器

Stack expression

Jwy-32b voltage relay
随机推荐
Multi merchant mall system with relatively clean code
Shellcode writing (unfinished)
volatile原理
Repvgg paper explanation and model reproduction using pytoch
7/13 (pond sampling)
I heard that many merchants of crmeb have added the function of planting grass?
No files or folders found to process
从thinkphp远程代码执行学php反射类
PMP practice once a day | don't get lost in the exam -7.28 (including agility + multiple choices)
subst命令将一个文件夹镜像成本地的一个磁盘
For loop
分享一下一二线大厂HR面经分享
Knowledge payment open source system
Table lock query and unlocking in SQL development part 1
Encapsulate the unified return object messageresult
Hjs-de1/2 time relay
Svg verification code recognition experience
JWY-32B电压继电器
redis常用命令总结(自备)
Solve the problem of pycharm using PowerShell