Java 17 Recipes
Download 3.2 Mb. Pdf ko'rish
|
Java 17 Recipes
- Bu sahifa navigatsiya:
- 10-9. Implementing Thread-Safe Counters Problem
How It Works
Solution 1 relies on the principle that a lock protects any change done to the object. Using the synchronized keyword is a shortcut to writing the expression synchronized (this). By synchronizing your getters and setters (and any other operation that alters the internal state of your object), you guarantee that the object is consistent. Also, any operations that should occur as a unit (e.g., something that modifies two collections at the same time) must be done within a method of the object and are protected by using the synchronized keyword. For instance, if an object offers a getSize() method and getItemNumber(int index), it would be unsafe to write the following object.getItemNumber (object .getSize()-1). Even though it looks that the statement is concise, another thread can alter the object’s contents between getting the size and getting the item number. Instead, it is safer to create a object.getLastElement() method, which atomically figures out the size and the last element. Solution 2 relies on the property of immutable objects. Immutable objects cannot change their internal state, and objects that cannot change their internal state (are immutable) are thread-safe. If you need to modify the immutable object due to an event, create a new object with the changed properties instead of explicitly changing its property. This new object then takes the place of the old object, and on future requests for the object, the new immutable object is returned. This is the easiest (albeit lengthy) method for creating thread-safe code. 10-9. Implementing Thread-Safe Counters Problem You need a thread-safe counter so that it can be incremented from within different execution threads. Solution Using the inherently thread-safe objects makes it possible to create a counter that guarantees thread safety and has an optimized synchronization strategy. In the following code, an Order object is created, and it requires a unique order ID that is generated using the AtomicLong incrementAndGet() method. Chapter 10 ConCurrenCy 387 AtomicLong orderIdGenerator = new AtomicLong(0); for (int i =0;i < 10;i++) { Thread orderCreationThread = new Thread(() -> { for (int i1 = 0; i1 < 10; i1++) { createOrder(Thread.currentThread().getName()); } }); orderCreationThread.setName("Order Creation Thread "+i); orderCreationThread.start(); } private void createOrder(String name) { long orderId = orderIdGenerator.incrementAndGet(); Order order = new Order(name, orderId); orders.add(order); } public class Order { String orderName; long orderId; Order(String orderName, long orderId) { this.orderName = orderName; this.orderId = orderId; } public String getOrderName() { return orderName; } public long getOrderId() { return orderId; } } Chapter 10 ConCurrenCy 388 The following is the output. Order id:9 Order id:2 Order id:8 Order id:11 Order id:12 Order id:13 Order id:14 Order id:15 Order id:16 Order id:18 Order id:19 Order id:21 Order id:22 Order id:7 Order id:23 Order id:24 ... Download 3.2 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling