当前位置:网站首页>腾讯面试之--请你设计一个实现线程池顺序执行
腾讯面试之--请你设计一个实现线程池顺序执行
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();
}

边栏推荐
- Crmeb Standard Version window+phpstudy8 installation tutorial (I)
- 3477. Simple sorting
- In 2022, the average salary of global programmers was released, and China ranked unexpectedly
- crmeb知识付费手动安装教程
- No files or folders found to process
- [touch] count the number of engineering documents, code lines, and line comment coverage
- 知识付费开源系统
- Specific operation process of network security emergency response
- 苹果iPhone手机APP应用图标隐藏怎么找回恢复显示在iPhone苹果手机桌面显示被隐藏的应用APP图标到iPhone苹果手机桌面?
- List of security technologies to be considered in cloud computing
猜你喜欢

Pyinstaller packages py as an EXE file

I heard that many merchants of crmeb have added the function of planting grass?

每日一题(回溯)

7/13 (pond sampling)

crmeb 标准版window+phpstudy8安装教程(三)

Crmeb Standard Edition window+phpstudy8 installation tutorial (III)

Classic Dijkstra and the longest way

3588. Permutation and binary
新版数据同步问题

Slider restore and validation (legal database)
随机推荐
sql语句的执行流程
安装mosek,license安装位置查找
.net core version 2.2 cross domain configuration
Idea debugging burpsuit plug-in
Specific operation process of network security emergency response
Compilation learning
Here comes the full open source free customer service system
Publish raspberry pie web page with cpolar (apache2 installation test)
ERROR:bokeh.core.validation. check:E-1001 (BAD_COLUMN_NAME)
Chrome plug-in debugging
DAY:7/11
How to set some app application icons on the iPhone Apple phone that you don't want others to see? How to hide the app application settings on the mobile desktop so that they can be used normally afte
Apple iPhone app icon hidden how to retrieve and restore the hidden app icon displayed on the iPhone iPhone desktop to the iPhone iPhone iPhone desktop?
What are the CCSP cloud security design principles
What functions will be added to crmeb Standard Version 4.4
4518. Minimum ticket price
游戏测试的概念是什么?测试方法和流程有哪些?
Shader顶点着色器修改顶点高度的一个思路
DataTables warning: table id=campaignTable - Cannot reinitialise DataTable.解决
2022年全球程序员平均薪资发布,中国排名很意外