TAILIEUCHUNG - Parallel Programming: for Multicore and Cluster Systems- P34

Parallel Programming: for Multicore and Cluster Systems- P34: Innovations in hardware architecture, like hyper-threading or multicore processors, mean that parallel computing resources are available for inexpensive desktop computers. In only a few years, many standard software products will be based on concepts of parallel programming implemented on such hardware, and the range of applications will be much broader than that of scientific computing, up to now the main application area for parallel computing | 322 6 Thread Programming Fig. Realization of a thread-safe buffer mechanism using Javawait and notify public class BoundedBufferSignal private final Object array private int putptr 0 private int takeptr 0 private int numel 0 number of items in buffer public BoundedBufferSignal int capacity throws IllegalArgumentException if capacity 0 throw new IllegalArgumentExceptionO array new Object capacity public synchronized int size return numel public int capacityO return public synchronized void put Object obj throws InterruptedException while numel waitO buffer full array putptr obj putptr putptr 1 if numel 0 notifyAllO wake up all threads public synchronized Object takeO throws InterruptedException while numel 0 waitO buffer empty Object x array takeptr takeptr takeptr 1 if numel notifyAllO 11 wake up all threads return x The class provides a put method to enable a producer to enter an item into the buffer and a take method to enable a consumer to remove an item from the buffer. A buffer object can have one of three states full partially full and empty. Figure illustrates the possible transitions between the states when calling take or put . The states are characterized by the following conditions State Condition Put possib Take le possible Full size capacity No Yes Partially full 0 size capacity Yes Yes Empty size 0 Yes No If the buffer is full the execution of the put method by a producer thread will block the executing thread this is implemented by a wait operation. If the put method is executed for a previously empty buffer all waiting consumer Java Threads 323 Fig. Illustration of the states of a thread-safe buffer mechanism threads will be woken up using notifyAll after the item has been entered into the buffer. If the buffer is empty the execution of the take method by a consumer thread will block the executing thread using wait . If the take method is executed for a previously .

Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.