java concurrency in practice - 1
2012-05-22 10:56
525 查看
In early timesharing systems, each process was a virtual von Neumann computer; it had a memory space storing both instructions and data, executing instructions sequentially according to the semantics of the machine language, and interacting with the outside
world via the operating system through a set of I/O primitives.
Threads allow multiple streams of program control flow to coexist within a process. They share process-wide resources such as memory and file handles, but each thread has its own program counter, stack, and local variables.
multiple threads within the same program can be scheduled simultaneously on multiple CPUs.
Threads are sometimes called lightweight processes, and most modern operating systems treat threads, not processes, as the basic units of scheduling.
Since threads share the memory address space of their owning process, all threads within a process have access to the same variables and allocate objects from the same heap, which allows finer-grained data sharing than inter-process mechanisms. But without
explicit synchronization to coordinate access to shared data, a thread may modify variables that another thread is in the middle of using, with unpredictable results.
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, because TimerTasks are executed in a thread managed by the
Timer, not the application. If a TimerTask accesses data that is also accessed by other application threads, then not only must the
TimerTask do so in a thread-safe manner, but so must any other classes that access that data. Often
the
easiest way to achieve this is to ensure that objects accessed by the TimerTask are themselves thread-safe, thus encapsulating the thread safety within the shared objects.
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 (marshaled) into a byte stream and shipped
over the network to the remote JVM, where they are unpacked (unmarshaled) and passed to the remote method.
When the RMI code calls your remote object, in what thread does that call happen? You don't know, but it's definitely not in a thread you createdyour object 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?[4]
[4] Answer: yes, but it's not all that clear from the Javadocyou have to read the RMI spec.
A remote object must guard against two thread safety hazards: properly coordinating access to state that may be shared with other objects, and 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.
world via the operating system through a set of I/O primitives.
Threads allow multiple streams of program control flow to coexist within a process. They share process-wide resources such as memory and file handles, but each thread has its own program counter, stack, and local variables.
multiple threads within the same program can be scheduled simultaneously on multiple CPUs.
Threads are sometimes called lightweight processes, and most modern operating systems treat threads, not processes, as the basic units of scheduling.
Since threads share the memory address space of their owning process, all threads within a process have access to the same variables and allocate objects from the same heap, which allows finer-grained data sharing than inter-process mechanisms. But without
explicit synchronization to coordinate access to shared data, a thread may modify variables that another thread is in the middle of using, with unpredictable results.
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, because TimerTasks are executed in a thread managed by the
Timer, not the application. If a TimerTask accesses data that is also accessed by other application threads, then not only must the
TimerTask do so in a thread-safe manner, but so must any other classes that access that data. Often
the
easiest way to achieve this is to ensure that objects accessed by the TimerTask are themselves thread-safe, thus encapsulating the thread safety within the shared objects.
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 (marshaled) into a byte stream and shipped
over the network to the remote JVM, where they are unpacked (unmarshaled) and passed to the remote method.
When the RMI code calls your remote object, in what thread does that call happen? You don't know, but it's definitely not in a thread you createdyour object 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?[4]
[4] Answer: yes, but it's not all that clear from the Javadocyou have to read the RMI spec.
A remote object must guard against two thread safety hazards: properly coordinating access to state that may be shared with other objects, and 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.
相关文章推荐
- [Java Concurrency in Practice]第十六章 Java内存模型
- Java Concurrency In Practice -Chapter 2 Thread Safety
- 《Java Concurrency in Practice》ch8 Applying Thread Pools
- 《Java Concurrency in Practice》之初识并发编程
- 《Java Concurrency in Practice》之Disruptor简单类图
- [Java Concurrency in Practice]第十五章 原子变量与非阻塞同步机制
- Java并发编程学习——《Java Concurrency in Practice》学习笔记 1.简介
- [Java Concurrency in Practice]第七章 取消与关闭
- 《Java Concurrency in Practice》之加锁机制(Locking)
- [Java Concurrency in Practice]第十章 避免活跃性危险
- Java concurrency in practice 笔记
- Java并发编程学习——《Java Concurrency in Practice》学习笔记 2.线程安全性
- 《Java Concurrency in Practice》ch9 GUI Applications
- Java并发编程实践评价(Java Concurrency in Practice)
- Java Concurrency in Practice 思维导图
- Java Concurrency In Practice Notes
- [Java Concurrency in Practice]第十一章 性能与可伸缩性
- java并发也许你不知道的一些内容(读Java Concurrency in Practice)(转)
- [Java Concurrency in Practice]第八章 线程池的使用
- 《Java Concurrency in Practice》之线程封闭(Thread Confinement)