马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
前言
太告急了,好久没有走出头试的阴影,脑筋一片空缺,明显许多方案,却只写出来一种信号量方法,这里做一个简单的纪录。
前置知识
死锁
线程组中的每一个线程都无法继续推进,都只能由其他线程举行叫醒。
死锁的须要条件
互斥、不剥夺、哀求与保持、循环等候。这里次序打印实在很好表现对循环等候的条件的粉碎,也就是按照编号举行次序上锁。
思绪
并发的环境下,次序打印,实在观察的就是对独占锁的使用。这里给出三种方法:信号量、Volatile、ReentrantLock。
Semaphore
口试的时间临场想出来的写法,只能说一点封装、面向对象的头脑看不到,只能说相称貌寝。这里实在是一种变相的生产者斲丧者模式。
- @Test
- public void testForSemaphore() {
- Semaphore semaphoreA = new Semaphore (1);
- Semaphore semaphoreB = new Semaphore (0);
- Semaphore semaphoreC = new Semaphore (0);
- new Thread (() -> {
- for (int i = 0; i < 20; i++) {
- try {
- semaphoreA.acquire ();
- printf ('A');
- } catch (InterruptedException e) {
- throw new RuntimeException (e);
- } finally {
- semaphoreB.release ();
- }
- }
- }, "t1").start ();
- new Thread (() -> {
- for (int i = 0; i < 20; i++) {
- try {
- semaphoreB.acquire ();
- printf ('B');
- } catch (InterruptedException e) {
- throw new RuntimeException (e);
- } finally {
- semaphoreC.release ();
- }
- }
- }, "t2").start ();
- new Thread (() -> {
- for (int i = 0; i < 20; i++) {
- try {
- semaphoreC.acquire ();
- printf ('C');
- } catch (InterruptedException e) {
- throw new RuntimeException (e);
- } finally {
- semaphoreA.release ();
- }
- }
- }, "t3").start ();
- }
复制代码 Volatile
通过Volatile对缓存的禁用,每次都去读取最新的token值,来决定放行哪一个活动。这实在有点CAS的味道。固然代码的简便性稍微强了一丢丢,一点封装的味道都没有。
- volatile int token = 0;
- @Test
- public void testForVolatile() {
- new Thread (() -> {
- for (int i = 0; i < 20; i++) {
- while (token % 3 != 0) ;
- printf ('A');
- token++;
- }
- }, "t1").start ();
- new Thread (() -> {
- for (int i = 0; i < 20; i++) {
- while (token % 3 != 1) ;
- printf ('B');
- token++;
- }
- }, "t2").start ();
- new Thread (() -> {
- for (int i = 0; i < 20; i++) {
- while (token % 3 != 2) ;
- printf ('C');
- token++;
- }
- }, "t3").start ();
- }
复制代码 这里附上使用Unsafe实现Volatile,实在就是手动举行缓存举行,克制指令重排,每次都去内存中读取最新值,监听变量的变革
- @Test
- public void testForUnsafe() throws NoSuchFieldException, IllegalAccessException {
- Field field = Unsafe.class.getDeclaredField ("theUnsafe");
- field.setAccessible (true);
- Unsafe unsafe = (Unsafe) field.get (null);
- ExecutorService threadPool = Executors.newFixedThreadPool (3);
- for (int i = 0; i < 3; i++) {
- int finalI = i;
- threadPool.execute (() -> {
- for (int j = 0; j < 20; j++) {
- unsafe.loadFence ();
- while (num % 3 != finalI);
- char c = (char) ('A' + num % 3);
- printf (c);
- num++;
- }
- });
- }
- }
复制代码 ReentrantLock
通过ReentrantLock,并对打印任务举行封装得以复用。每个线程都必要去抢占Lock,之后通过num的来决定打印的是什么。代码看起来舒服了许多,但是毕竟哪个才是打印A的线程?哪个才是B?哪个才是C呢?实在职责就不明白了,这里是面向结果编程,丢失了详细类的职责,以是也不是很美满。
- private int num = 0;
- class PrintTask implements Runnable {
- @Override
- public void run() {
- for (int i = 0; i < 20; i++) {
- try {
- lock.lock ();
- char c = (char) ('A' + num % 3);
- num++;
- printf (c);
- } finally {
- lock.unlock ();
- }
- }
- }
- }
- @Test
- public void testForLock() {
- ExecutorService threadPool = Executors.newFixedThreadPool (3);
- for (int i = 0; i < 3; i++) {
- threadPool.execute (new PrintTask ());
- }
- }
复制代码 总结
只面过两次厂,看到大佬们实属告急,盼望以后能有所改善!真的很难熬,这么简单的题,却写出了最令人难以继承的代码,固然另有许多方式,只是不再写了
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |