Reformat rest of the design patterns - Issue #224
This commit is contained in:
		@@ -6,38 +6,37 @@ import java.util.concurrent.TimeUnit;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 
 | 
			
		||||
 * Double Checked Locking is a concurrency design pattern used to reduce the overhead 
 | 
			
		||||
 * of acquiring a lock by first testing the locking criterion (the "lock hint") without 
 | 
			
		||||
 * actually acquiring the lock. Only if the locking criterion check indicates that 
 | 
			
		||||
 * locking is required does the actual locking logic proceed.
 | 
			
		||||
 * Double Checked Locking is a concurrency design pattern used to reduce the overhead of acquiring a
 | 
			
		||||
 * lock by first testing the locking criterion (the "lock hint") without actually acquiring the
 | 
			
		||||
 * lock. Only if the locking criterion check indicates that locking is required does the actual
 | 
			
		||||
 * locking logic proceed.
 | 
			
		||||
 * <p>
 | 
			
		||||
 * In {@link Inventory} we store the items with a given size. However, we do not store
 | 
			
		||||
 * more items than the inventory size. To address concurrent access problems we
 | 
			
		||||
 * use double checked locking to add item to inventory. In this method, the
 | 
			
		||||
 * thread which gets the lock first adds the item.
 | 
			
		||||
 * In {@link Inventory} we store the items with a given size. However, we do not store more items
 | 
			
		||||
 * than the inventory size. To address concurrent access problems we use double checked locking to
 | 
			
		||||
 * add item to inventory. In this method, the thread which gets the lock first adds the item.
 | 
			
		||||
 * 
 | 
			
		||||
 */
 | 
			
		||||
public class App {
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Program entry point
 | 
			
		||||
	 * @param args command line args
 | 
			
		||||
	 */
 | 
			
		||||
	public static void main(String[] args) {
 | 
			
		||||
		final Inventory inventory = new Inventory(1000);
 | 
			
		||||
		ExecutorService executorService = Executors.newFixedThreadPool(3);
 | 
			
		||||
		for (int i = 0; i < 3; i++) {
 | 
			
		||||
			executorService.execute(() -> {
 | 
			
		||||
                while (inventory.addItem(new Item()))
 | 
			
		||||
                    ;
 | 
			
		||||
            });
 | 
			
		||||
		}
 | 
			
		||||
  /**
 | 
			
		||||
   * Program entry point
 | 
			
		||||
   * 
 | 
			
		||||
   * @param args command line args
 | 
			
		||||
   */
 | 
			
		||||
  public static void main(String[] args) {
 | 
			
		||||
    final Inventory inventory = new Inventory(1000);
 | 
			
		||||
    ExecutorService executorService = Executors.newFixedThreadPool(3);
 | 
			
		||||
    for (int i = 0; i < 3; i++) {
 | 
			
		||||
      executorService.execute(() -> {
 | 
			
		||||
        while (inventory.addItem(new Item()));
 | 
			
		||||
      });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
		executorService.shutdown();
 | 
			
		||||
		try {
 | 
			
		||||
			executorService.awaitTermination(5, TimeUnit.SECONDS);
 | 
			
		||||
		} catch (InterruptedException e) {
 | 
			
		||||
			System.out.println("Error waiting for ExecutorService shutdown");
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
    executorService.shutdown();
 | 
			
		||||
    try {
 | 
			
		||||
      executorService.awaitTermination(5, TimeUnit.SECONDS);
 | 
			
		||||
    } catch (InterruptedException e) {
 | 
			
		||||
      System.out.println("Error waiting for ExecutorService shutdown");
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -12,32 +12,30 @@ import java.util.concurrent.locks.ReentrantLock;
 | 
			
		||||
 */
 | 
			
		||||
public class Inventory {
 | 
			
		||||
 | 
			
		||||
	private final int inventorySize;
 | 
			
		||||
	private final List<Item> items;
 | 
			
		||||
	private final Lock lock;
 | 
			
		||||
  private final int inventorySize;
 | 
			
		||||
  private final List<Item> items;
 | 
			
		||||
  private final Lock lock;
 | 
			
		||||
 | 
			
		||||
	public Inventory(int inventorySize) {
 | 
			
		||||
		this.inventorySize = inventorySize;
 | 
			
		||||
		this.items = new ArrayList<>(inventorySize);
 | 
			
		||||
		this.lock = new ReentrantLock();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public boolean addItem(Item item) {
 | 
			
		||||
		if (items.size() < inventorySize) {
 | 
			
		||||
			lock.lock();
 | 
			
		||||
			try {
 | 
			
		||||
				if (items.size() < inventorySize) {
 | 
			
		||||
					items.add(item);
 | 
			
		||||
					System.out.println(Thread.currentThread()
 | 
			
		||||
							+ ": items.size()=" + items.size()
 | 
			
		||||
							+ ", inventorySize=" + inventorySize);
 | 
			
		||||
					return true;
 | 
			
		||||
				}
 | 
			
		||||
			} finally {
 | 
			
		||||
				lock.unlock();
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		return false;
 | 
			
		||||
	}
 | 
			
		||||
  public Inventory(int inventorySize) {
 | 
			
		||||
    this.inventorySize = inventorySize;
 | 
			
		||||
    this.items = new ArrayList<>(inventorySize);
 | 
			
		||||
    this.lock = new ReentrantLock();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public boolean addItem(Item item) {
 | 
			
		||||
    if (items.size() < inventorySize) {
 | 
			
		||||
      lock.lock();
 | 
			
		||||
      try {
 | 
			
		||||
        if (items.size() < inventorySize) {
 | 
			
		||||
          items.add(item);
 | 
			
		||||
          System.out.println(Thread.currentThread() + ": items.size()=" + items.size()
 | 
			
		||||
              + ", inventorySize=" + inventorySize);
 | 
			
		||||
          return true;
 | 
			
		||||
        }
 | 
			
		||||
      } finally {
 | 
			
		||||
        lock.unlock();
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -6,7 +6,7 @@ package com.iluwatar.doublechecked.locking;
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
public class Item {
 | 
			
		||||
	
 | 
			
		||||
	private String name;
 | 
			
		||||
	private int level;
 | 
			
		||||
 | 
			
		||||
  private String name;
 | 
			
		||||
  private int level;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -11,9 +11,9 @@ import com.iluwatar.doublechecked.locking.App;
 | 
			
		||||
 */
 | 
			
		||||
public class AppTest {
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
	public void test() {
 | 
			
		||||
		String[] args = {};
 | 
			
		||||
		App.main(args);
 | 
			
		||||
	}
 | 
			
		||||
  @Test
 | 
			
		||||
  public void test() {
 | 
			
		||||
    String[] args = {};
 | 
			
		||||
    App.main(args);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user