Timer:
Timer is a convenience mechanism for scheduling tasks to run at a later time, either once or periodically. The introduction of a Timer can complicate an otherwise sequential program, becauseTimerTasks are executed in a thread managed by the Timer, not the application. If aTimerTaskaccesses data that is also accessed by other application threads, then not only must theTimerTaskdo so in a thread-safe manner, but so much any other classes that access that data. Often the easiest way to achieve this is to ensure that objects accessed by theTimerTaskare themselves thread-safe, thus encapsulating the thread safety within the shared objects.
要点:
1. Timer作用为安排任务执行(一次或多次)
2. Timer有一单独thread,自行管理,不被application管理
3. Timer与application共同享有data使用权
4. 解决Timer产生的并行问题最简单的办法就是对shared objects进行多线程保护
Servlets and Java Server Pages(JSPs)
The servlets framework is designed to handle all the infrastructure of deploying a web application and dispatching requests from remote HTTP clients. A request arriving at the server is dispatched, perhaps through a chain of filters, to the appropriate servlet or JSP. Each servlet represents a component of application logic, and in high-volume web sites, multiple clients may require the services of the same servlet at once. The servlets specification requires the services of the same servlet at once. The servlets specification requires that a servlet be prepared to be called simultaneously from multiple threads. In other words, servlets need to be thread-safe.
Even if you could guarantee that a servlet was only called from one thread at a time, you would still have to pay attention to thread safety when building a web application. Servlets often acess state information shared with other servlets, such as application-scoped objects(those stored in the ServletContext) or session-scoped objects(those stored in the per-client HttpSession). When a servlet accesses objects shared across servlets or requests, it must coordinate access to these objects properly, since multiple requests could be accessing them simultaneously from separate threads. Servlets and JSPs, as well as servlet filters and objects stored in scoped containers like ServletContext and HttpSession, simply have to be thread-safe.
要点:
1. servlet与其他servlet共同拥有application-scoped以及session-scoped objects,因此需要保护其scope间数据安全。
Remote Method Invocation
RMI lets you invoke methods on objects running in another JVM. When you call a remote method with RMI, the method arguments are packaged(marshalled) into a byte stream and shipped over the network to the remote JVM, where they are unpacked(unmarshalled) and passed to the remote method.
When the RMI code calls your remote objects, in what thread does that call happen? You don't know, but it's definitely not in a thread you created--your objects gets called in a thread managed by RMI. How many threads does RMI create? Could the same remote method on the same remote object be called simultaneously in multiple RMI threads? (yes, but it's not all that clear from javadoc--you have to read the RMI spec)
A remote object must guard against two thread safety hazards: properly coordinating access to the state of the remote object itself(since the same object may be called in multiple threads simultaneously). Like servlets, RMI objects should be prepared for multiple simultaneous calls and must provide their own thread safety.
要点:
1. RMI framework自动多线程处理请求
2. 像servlet那样对shared objects进行保护