有个网友,发了个帖子其中说到了迅雷的笔试题。帖子地址如下:
http://topic.csdn.net/u/20091129/21/4bbf398d-431a-4f8e-accc-b8de6572b8af.html
其中有道关于多线程的题目,觉得挺好玩,拿来分析一下:
有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印10次ABCABC…
题目分析:
三个线程,交替打印ABC,10次。所以可能的考点是
A.三个线程顺序执行。
B.10次说明至少有一个计数器,初步假设可能为每个线程都有一个计数器(个人认为这样子不可取)。另外一种假设是有公共区域,即关键区,题目意在考对关键区的读写操作。
粗略的看一下,感觉和Guarded Suspension Pattern设计模式很像。
【补充】举例介绍一下Guarded Suspension Pattern模式。
【例子】你在家里换衣服,忽然门铃响了,原来是邮递员送信来了。可是你衣服换到一半,不能开门,所以就说“请等我一下,我马上来。”,邮递员不敢闯入,只能等待在外。当你换号衣服,再打开门,说句“不好意思,久等了。。。”,顺利取到邮件。
guarded的意思是“被保护着,被防备着”,suspension的意思是“暂停”的意思。当现在不适合马上执行某个操作时,就要求执行该操作的线程等候,以保证实例的安全性(在例子中就如同你的隐私权一样)。这就是Guarded Suspension Pattern的核心思想。
这种设计模式还有其他的一些名称,如 "guardard wait"或"spin wait"等。
对于本题,我设定了三个类。
PrintThread
ID非别为A、B、C的三个线程就是他了。通过构造函数传入的name的不同,区分不同的线程。
RequestQueue
三个线程请求的公共区域。RequestQueue会储存三个线程要打印的信息和打印次数。通过对printMsg方法的同步限制,从而使三个并发线程安全,正确的访问关键区,即RequestQueue对象。
TestABC
Main函数所在的类,用来新建一个RequestQueue和三个线程类。
以上代码,运行过,可以按题目打印。但是可能仍有未发现的问题隐患,欢迎大家拍砖,交流。
分享到:
相关推荐
Guarded Suspension 模式 Producer Consumer 模式 Worker Thread 模式 Thread-Per-Message 模式 Future 模式 Read-Write-Lock 模式 Two-phase Termination 模式 Thread-Specific Storage 模式
第3章 Guarded Suspension——要等到我准备好喔 第4章 Balking——不需要的话,就算了吧 第5章 Producer-Consumer——我来做,你来用 第6章 Read-Write Lock——大家想看就看吧,不过看的时候不能写喔 第7章 read-...
第1章 Single Threaded Execution——能通过这座桥的,只有一个人 第2章 Immutable——想破坏它也没办法 第3章 Guarded Suspension——要等到我准备好喔 第4章 Balking——不需要的话,就算了吧 第5章 Producer-...
3、Guarded Suspension ———— 要等到我们准本好哦 4、Balking ———— 不需要的话,就算了吧 5、Producer-Consumer ———— 我来做,你来用 6、Read-Write Lock ———— 大家想看就看吧,不过看的时候不能写哦...
│ 高并发编程第二阶段24讲、Guarded Suspension设计模式-上.mp4 │ 高并发编程第二阶段25讲、Guarded Suspension设计模式-下.mp4 │ 高并发编程第二阶段26讲、ThreadLocal使用详解,深入原理介绍.mp4 │ 高并发...
│ 高并发编程第二阶段24讲、Guarded Suspension设计模式-上.mp4 │ 高并发编程第二阶段25讲、Guarded Suspension设计模式-下.mp4 │ 高并发编程第二阶段26讲、ThreadLocal使用详解,深入原理介绍.mp4 │ 高并发...
今天小编就为大家分享一篇laravel中的fillable和guarded属性详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
var guard = require ( 'guarded-array' ) //First create any old array var array = [ 0 , 1 , 2 , 3 , 4 , 5 ] //Then we protect it using guard! var guardedArray = guard ( array ) //The guarded array ...
yarn add guarded-string 用法 重要的! 应该将其用于防止XSS攻击之类的东西,而不是用于隐藏敏感信息。 import guardedString from 'guarded-string' ; const myString = guardedString `My very important (but ...
scalac-guardedblocks-plugin 简单的Scala编译器插件
JAX-RS 模板应用程序这是使用 JAX-RS 的轻量级 RESTful API 的模板。 示例代码是获取当前时间的调用。在本地运行应用程序首先构建: $mvn clean install然后运行它: $ java -cp target/classes:target/dependency/*...
8.5 Guarded Commands 367 8.6 Conclusions 371 Summary • Review Questions • Problem Set •Programming Exercises 372 Chapter 9 Subprograms 377 9.1 Introduction 378 9.2 Fundamentals of ...
我们遵循使用Node和MySQL的MVC设计模式来查询和路由应用程序中的数据。 把手用于生成HTML。 Travis CI用于配置棉绒并进行连续的分割。然后将该项目加载到heroku中,以与JawsDB MySQL附加组件一起用作数据库。目录...
防护命令语言六月7,2018 CS522正式语言和自动机20183111金李涵概括 2018年Spring正式语言和自动机理论项目1〜2 防护命令语言(以下称为GCL)解析和执行功能的实现语句语法说明中止论文说“做任何事情”,但实际上并...
When specified as false, checks for required views being non-null are elided and casts are no longer guarded with user-friendly error messages. This reduces the amount of generated code for release ...
异步去抖 防止在异步操作正在进行... var guarded = debounce(function (args, callback) { // do something with the arguments, then call the callback }); guarded('hello', 'world'); // Parameters are comb
These details are highly guarded Microsoft intellectual property that is of interest only to a relative handful of software developers. The purpose of this book is to provide an educational vehicle...
These details are highly guarded Microsoft intellectual property that is of interest only to a relative handful of software developers. The purpose of this book is to provide an educational vehicle...
You’ll learn some of the most closely guarded Photoshop CS4 special effects—the same ones you see on TV, in magazines, and on the Web. Using Scott’s simple step-by-step method, with hundreds of ...