Thinking in java 4th Edition 读书笔记-Concurrency(2)
2009-04-30 17:52
519 查看
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.
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.
相关文章推荐
- Thinking in java 4th Edition 读书笔记-Concurrency(1)
- Thinking in java 4th Edition 读书笔记-Concurrency(3)
- Thinking in java 4th Edition 读书笔记-I/O(4)
- Thinking in java 4th Edition 读书笔记-I/O(5)
- Thinking in java 4th Edition 读书笔记-I/O(1)
- Thinking in java 4th Edition 读书笔记-I/O(6)
- Thinking in java 4th Edition 读书笔记-I/O(3)
- Thinking in Java 4th edition 中文非扫描版
- 不好意思, java的英文书换成thinking in java 4th edition
- 对 Thinking in java 4th Edition I/O DirList.java的疑问
- Thinking in java 4th Edition 读书笔记-I/O(2)
- thinking in java 4th 读书笔记
- Thinking in Java 4th Edition (Bruce Eckel) java编程思想 书上例题源代码
- Thinking in Java 4th Edition Source Code
- 【资源】Thinking in java 4th edition 英文版 高清晰pdf+源代码+练习题答案
- 深入解析Windows操作系统(Windows Internals) 4th Edition 读书笔记 - 序言
- 【thinking in java】读书笔记(一)
- 《Fundamentals of Project Management 4th Edition》 读书笔记 第十一章 使用挣值分析进行项目控制
- Thinking in Java 4th - 第9章 接口
- 《Fundamentals of Project Management 4th Edition》读书笔记 第一章 项目管理总览