Thinking in java 4th Edition 读书笔记-Concurrency(2)

本文深入探讨了Java并发编程中的核心概念,包括Runnable与Callable的区别、Future模式的应用、线程的优先级设置、守护线程的工作原理及其实现方式,并讨论了如何通过ExecutorService来管理和控制任务执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.A Runnable is a separate task that performs work, but it doesn’t return a value. If you want the task to produce a value when it’s done, you can implement the Callable interface rather than the Runnable interface.It's a type parameter representing the return value from the method call( ) (instead of run( )), and must be invoked using an ExecutorService submit( ) method.
The submit( ) method produces a Future object, parameterized for the particular type of result returned by the Callable. You can query the Future with isDone( ) to see if it has completed. When the task is completed and has a result, you can call get( ) to fetch the result. You can simply call get( ) without checking isDone( ), in which case get( ) will block until the result is ready. You can also call get( ) with a timeout, or isDone( ) to see if the task has completed, before trying to call get( ) to fetch the result.
2.The sequential behavior relies on the underlying threading mechanism, which is different from one operating system to another, so you cannot rely on it. If you must control the order of execution of tasks, your best bet is to use synchronization controls
3." toString( ) " is overridden to use Thread.toString( ), which prints the thread name, the priority level, and the "thread group" that the thread belongs to.You can get a reference to the Thread object that is driving a task, inside that task, by calling Thread.currentThread( ).
4.Although the JDK has 10 priority levels, this doesn't map well to many operating systems. For example, Windows has 7 priority levels that are not fixed, so the mapping is indeterminate. Sun's Solaris has 231 levels. The only portable approach is to stick to MAX_PRIORITY, NORM_PRIORITY, and MIN_PRIORITY when you're adjusting priority levels.
5.A "daemon" thread is intended to provide a general service in the background as long as the program is running, but is not part of the essence of the program. Thus, when all of the non-daemon threads complete, the program is terminated, killing all daemon threads in the process. Conversely, if there are any non-daemon threads still running, the program doesn’t terminate.You must set the thread to be a daemon by calling setDaemon( ) before it is started.
6.Each of the static ExecutorService creation methods is overloaded to take a ThreadFactory object that it will use to create new threads.

ExecutorService exec = Executors.newCachedThreadPool(
new DaemonThreadFactory());
/////////////////////////////////////////////
/////////////////////////////////////////////
public class DaemonThreadFactory implements ThreadFactory {
 public Thread newThread(Runnable r) {
  Thread t = new Thread(r);
  t.setDaemon(true);
  return t;
 }
}
7.two different methods:
//////////////////////////////////////////////
//////////////////////////////////////////////
Thread d = new Thread(new Daemon());
d.setDaemon(true);//only when it's a daemon thread.
d.start();

class Daemon implements Runnable {
public void run() {
……………………………………
}
}

////////////////////////////////////////////////////////////
//pattern: Abstract Factory or Factory Method?//
////////////////////////////////////////////////////////////
ExecutorService exec = Executors.newCachedThreadPool(
new DaemonThreadFactory());
exec.execute(new DaemonFromFactory());

public class DaemonFromFactory implements Runnable {
public void run() {
……………………………………
}
}

public class DaemonThreadFactory implements ThreadFactory {
…………………………………………
}
8.What I do not understand now:
Non-daemon Executors are generally a better approach, since all the tasks controlled by an Executor can be shut down at once. As you shall see later in the chapter, shutdown in this case proceeds in an orderly fashion.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值