您的位置:首页 > 编程语言 > Java开发

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: