提交 a636658e 编写于 作者: B Benjamin Winterberg

Merge remote-tracking branch 'origin/master'

package com.winterbe.java8.samples.concurrent;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.StampedLock;
/**
* @author Benjamin Winterberg
*/
public class Lock4 {
private static int count = 0;
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(2);
StampedLock lock = new StampedLock();
executor.submit(() -> {
long stamp = lock.writeLock();
try {
count++;
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
} finally {
lock.unlockWrite(stamp);
}
});
executor.submit(() -> {
long stamp = lock.readLock();
try {
System.out.println(Thread.currentThread().getName() + ": " + count);
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
} finally {
lock.unlockRead(stamp);
}
});
executor.submit(() -> {
long stamp = lock.readLock();
try {
System.out.println(Thread.currentThread().getName() + ": " + count);
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
} finally {
lock.unlockRead(stamp);
}
});
ConcurrentUtils.stop(executor);
}
}
package com.winterbe.java8.samples.concurrent;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.StampedLock;
/**
* @author Benjamin Winterberg
*/
public class Lock5 {
private static int count = 0;
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(2);
StampedLock lock = new StampedLock();
executor.submit(() -> {
long stamp = lock.tryOptimisticRead();
try {
System.out.println("Optimistic Lock Valid: " + lock.validate(stamp));
TimeUnit.SECONDS.sleep(1);
System.out.println("Optimistic Lock Valid: " + lock.validate(stamp));
} catch (InterruptedException e) {
throw new IllegalStateException(e);
} finally {
lock.unlock(stamp);
}
});
executor.submit(() -> {
long stamp = lock.writeLock();
try {
System.out.println("Write Lock acquired");
incrementAndSleep(2);
} finally {
lock.unlock(stamp);
System.out.println("Write done");
}
});
ConcurrentUtils.stop(executor);
}
private static void incrementAndSleep(int sleepSeconds) {
try {
count++;
TimeUnit.SECONDS.sleep(sleepSeconds);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
}
}
package com.winterbe.java8.samples.concurrent;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.StampedLock;
/**
* @author Benjamin Winterberg
*/
public class Lock6 {
private static int count = 0;
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(2);
StampedLock lock = new StampedLock();
executor.submit(() -> {
long stamp = lock.readLock();
try {
if (count == 0) {
stamp = lock.tryConvertToWriteLock(stamp);
if (stamp == 0L) {
System.out.println("Could not convert to write lock");
stamp = lock.writeLock();
}
count = 23;
}
System.out.println(count);
} finally {
lock.unlock(stamp);
}
});
ConcurrentUtils.stop(executor);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册