Skip to content

Instantly share code, notes, and snippets.

@dev-jonghoonpark
Last active August 6, 2023 17:10
Show Gist options
  • Save dev-jonghoonpark/536a31af34ffe8267f551c8d63fd55ab to your computer and use it in GitHub Desktop.
Save dev-jonghoonpark/536a31af34ffe8267f551c8d63fd55ab to your computer and use it in GitHub Desktop.
6장 JDK 동시성(concurrency) 라이브러리
private static AtomicInteger nextAccountId = new AtomicInteger();
private final int accountId;
private double balance;
public Account(int openingBalance) {
balance = openingBalance;
accountId = nextAccountId.getAndIncrement();
}
public static class Counter implemenets Runnable {
private final CountDownLatch latch;
private final int value;
private final AtomicInteger count;
public Counter(CounterDownLatch l, int v, AtomicInteger c) {
this.latch = l;
this.value = v;
this.count = c;
}
@Override
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
count.addAndGet(value);
latch.countDown();
}
}
var latch = new CountDownLatch(5);
var count = new AtomicInteger();
for (int i = 0; i < 5; i = i + 1) {
var r = new Counter(latch, i, count);
new Thread(r).start();
}
latch.await();
System.out.println("Total: "+ count.get());
var map = new HashMap<String, String>();
var SIZE = 10_000;
Runnable r1 = () -> {
for (int i = 0; i < SIZE; i = i + 1) {
map.put("t1" + i, "0");
}
System.out.println("Thread 1 done");
};
Runnable r2 = () -> {
for (int i = 0; i < SIZE; i = i + 1) {
map.put("t2" + i, "0");
}
System.out.println("Thread 2 done");
};
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("Count: "+ map.size());
public final class ImmutableDictionary extends Dictionary {
private final Dictionary d;
private ImmutableDictionary(Dictionary delegate) {
d = delegate;
}
public static ImmutableDictionary of(Dictionary delegate) {
return new ImmutableDictionary(delegate);
}
@Override
public int size() {
return d.size();
}
@Override
public String get(Object key) {
return d.get(key);
}
@Override
public String put(String key, String value) {
throw new UnsupportedOperationException();
}
// other mutating methods also throw UnsupportedOperationException
}
private final Lock lock = new ReentrantLock();
public boolean transferTo(SafeAccount other, int amount) {
// We also need code to check to see amount > 0, throw if not
// ...
if (accountId == other.getAccountId()) {
// Can't transfer to your own account
return false;
}
var firstLock = accountId < other.getAccountId() ? lock : other.lock;
var secondLock = firstLock == lock ? other.lock : lock;
firstLock.lock(); // the firstLock object has a lower account id.
try {
secondLock.lock(); // the secondLock object has a higher account id.
try {
if (balance >= amount) {
balance = balance - amount;
other.deposit(amount);
return true;
}
return false;
} finally {
secondLock.unlock();
}
} finally {
firstLock.unlock();
}
}
public final class SynchronizedDictionary extends Dictionary {
private final Dictionary d;
private SynchronizedDictionary(Dictionary delegate) {
d = delegate;
}
public static SynchronizedDictionary of(Dictionary delegate) {
return new SynchronizedDictionary(delegate);
}
@Override
public synchronized int size() {
return d.size();
}
@Override
public synchronized boolean isEmpty() {
return d.isEmpty();
}
// ... other methods
}
public class TaskManager implements Runnable {
private final AtomicBoolean shutdown = new AtomicBoolean(false);
public void shutdown() {
shutdown.set(true);
}
@override
public void run() {
while (!shutdown.get()) {
// do some work
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment