TAILIEUCHUNG - Effective Java Programming Language Guide phần 9

mà có thể đặt giới hạn nhân tạo về việc thực hiện của lớp con (khoản 14). Như một ví dụ cuối cùng, bằng cách sử dụng một loại thực hiện chứ không phải là một giao diện API một mối quan hệ bạn một thực hiện cụ thể, thậm chí mặc dù việc triển khai nhanh hơn có thể được viết trong tương lai (khoản 34). | Effective Java Programming Language Guide The problem with this code is that in the absence of synchronization there is no guarantee as to when if ever the stoppable thread will see a change in the the value of stopRequested that was made by another thread. As a result the requeststop method might be completely ineffective. Unless you are running on a multiprocessor you are unlikely to observe the problematic behavior in practice but there are no guarantees. The straightforward way to fix the problem is simply to synchronize all access to the stopRequested field Properly synchronized cooperative thread termination public class StoppableThread extends Thread private boolean stopRequested false public void run boolean done false while stopRequested done . do what needs to be done. public synchronized void requestStop stopRequested true private synchronized boolean stopRequested return stopRequested Note that the actions of each of the synchronized methods are atomic The synchronization is being used solely for its communication effects not for mutual exclusion. It is clear that the revised code works and the cost of synchronizing on each iteration of the loop is unlikely to be noticeable. That said there is a correct alternative that is slightly less verbose and whose performance may be slightly better. The synchronization may be omitted if stopRequested is declared volatile. The volatile modifier guarantees that any thread that reads a field will see the most recently written value. The penalty for failing to synchronize access to stopRequested in the previous example is comparatively minor the effect of the requestStop method may be delayed indefinitely. The penalty for failing to synchronize access to mutable shared data can be much more severe. Consider the double-check idiom for lazy initialization The double-check idiom for lazy initialization - broken private static Foo foo null public static Foo getFoo if foo null synchronized if foo null foo new .

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã 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.