Thinking in Java:并发

论坛 期权论坛     
选择匿名的用户   2021-5-23 02:00   66   0
<div style="font-family:微软雅黑; font-size:14px; line-height:21px">
<span style="background-color:inherit">PS:一个任务对象可以由多个线程执行</span>
</div>
<div style="font-family:微软雅黑; font-size:14px; line-height:21px">
<span style="background-color:inherit"><br style="background-color:inherit"> </span>
</div>
<div style="font-family:微软雅黑; font-size:14px; line-height:21px">
1.并发的意义:从性能角度看,如果没有任务会阻塞,那么单处理器机器上使用并发就没有任何意义
</div>
<div style="font-family:微软雅黑; font-size:14px; line-height:21px">
<br style="background-color:inherit">
</div>
<div style="font-family:微软雅黑; font-size:14px; line-height:21px">
<span style="background-color:inherit">使用Executor</span>
</div>
<div style="font-family:微软雅黑; font-size:14px; line-height:21px">
1.Java SE5de Executor将为你管理Thread对象,从而简化了并发变成。Executor允许你管理异步任务的执行,而无需显式地管理线程的生命周期,因此是启动任务的优选方法。通过Executors的静态方法创建封装好的线程池(封装了ThreadPoolExecutor)。
</div>
<div style="font-family:微软雅黑; font-size:14px; line-height:21px">
<br style="background-color:inherit">
</div>
<div style="font-family:微软雅黑; font-size:14px; line-height:21px">
2.任何线程池中,现有线程在可能的情况下,都会被自动复用。
</div>
<div style="font-family:微软雅黑; font-size:14px; line-height:21px">
<br style="background-color:inherit">
</div>
<div style="font-family:微软雅黑; font-size:14px; line-height:21px">
3.Runnable是执行工作的独立任务,但是它不返回任何值,返回值的需要实现Callable接口泛型,类型参数表示为call(),并且必须使用ExecutorService.submit()方法调用。submit方法会产生Future对象,它用Callable返回结果的特定类型进行了参数化。可以用isDone方法判断是否完成,使用get方法来获取结果(阻塞或超时设置)。
</div>
<div style="font-family:微软雅黑; font-size:14px; line-height:21px">
<img alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-8338a015a76c552c511fbe6babb54387">
<br>
</div>
<div style="font-family:微软雅黑; font-size:14px; line-height:21px">
<br style="background-color:inherit">
</div>
<div style="font-family:微软雅黑; font-size:14px; line-height:21px">
4.休眠:TimeUnit.MILLISECONDS.sleep封装了Thread.sleep方法,可以使用时间单位
</div>
<div style="font-family:微软雅黑; font-size:14px; line-height:21px">
<br style="background-color:inherit">
</div>
<div style="font-family:微软雅黑; font-size:14px; line-height:21px">
5.优先级:可以用getPriority来读取现有线程的优先级,并且在任何时刻都可以通过setPriority来修改它
</div>
<div style="font-family:微软雅黑; font-size:14px; line-height:21px">
<br style="background-color:inherit">
</div>
<div style="font-family:微软雅黑; font-size:14px; line-height:21px">
6.让步:yield方法暗示可以让别的线程使用CPU,但并没有任何机制保证一定会执行。
</div>
<div style="font-family:微软雅黑; font-size:14px; line-height:21px">
<br style="background-color:inherit">
</div>
<div style="font-family:微软雅黑; font-size:14px; line-height:21px">
7.后台线程daemon:通过设置setDaemon方法可以设为后台线程,这种线程并不属于程序中不可或缺的部分,因此所有非后台线程结束时,程序也就终止了,会杀死进程中的所有后台线程(甚至不会执行finally子句,这样是正确的)。
</div>
<div style="font-family:微软雅黑; font-size:14px; line-height:21px">
<br style="background-color:inherit">
</div>
<div style="font-family:微软雅黑; font-size:14px; line-height:21px">
8.使用Executor而不是显式创建Thread对象的原因:
</div>
<div style="font-family:微软雅黑; font-size:14px; line-height:21px">
     1.实现Runable接口可以另外继承需要的任务类,而Thread不行
</div>
<div style="font-family:微软雅黑; font-size:14px; line-height:21px">
     2.通过下图这样创建新任务,在构造器启动任务可能会变得有问题,因为另一个
<span style="background-color:inherit">任务可能会在构</span>造器结束之前开始执行,如下结果所示。
</div>
<div style="font-family:微软雅黑; font-size:14px; line-height:21px">
<img alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-27c527e3f29e7e7a23407e66cd1cbef5">
<br>
</div>
<div style="font-family:微软雅黑; font-size:14px; line-height:21px">
<br>
</div>
<div style="font-family:微软雅黑; font-size:14px; line-height:21px">
<img alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-840e21d948e0eff20e1e6dc94fc5dd5e">
<br style="background-color:inherit">
</div>
<div style="font-family:微软雅黑; font-size:14px; line-height:21px">
9.有时会通过内部类来将线程代码隐藏在类中将会很有用。
</div>
<div style="font-family:微软雅黑; font-si
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP