public class ReduceLockDuration implements Runnable {
private static final int NUMBER_OF_THREADS = 5;
private static final Map<String, Integer> map = new HashMap<String, Integer>();
public void run() {
for (int i = 0; i < 10000; i++) {
synchronized (map) {
UUID randomUUID = UUID.randomUUID();
Integer value = Integer.valueOf(42);
String key = randomUUID.toString();
map.put(key, value);
}
Thread.yield();
}
}
public static void main(String[] args) throws InterruptedException {
Thread[] threads = new Thread[NUMBER_OF_THREADS];
for (int i = 0; i < NUMBER_OF_THREADS; i++) {
threads[i] = new Thread(new ReduceLockDuration());
}
long startMillis = System.currentTimeMillis();
for (int i = 0; i < NUMBER_OF_THREADS; i++) {
threads[i].start();
}
for (int i = 0; i < NUMBER_OF_THREADS; i++) {
threads[i].join();
}
System.out.println((System.currentTimeMillis()-startMillis)+"ms");
}
}
public void run() {
for (int i = 0; i < 10000; i++) {
UUID randomUUID = UUID.randomUUID();
Integer value = Integer.valueOf(42);
String key = randomUUID.toString();
synchronized (map) {
map.put(key, value);
}
Thread.yield();
}
}
public static class CounterOneLock implements Counter {
private long customerCount = 0;
private long shippingCount = 0;
public synchronized void incrementCustomer() {
customerCount++;
}
public synchronized void incrementShipping() {
shippingCount++;
}
public synchronized long getCustomerCount() {
return customerCount;
}
public synchronized long getShippingCount() {
return shippingCount;
}
}
public static class CounterSeparateLock implements Counter {
private static final Object customerLock = new Object();
private static final Object shippingLock = new Object();
private long customerCount = 0;
private long shippingCount = 0;
public void incrementCustomer() {
synchronized (customerLock) {
customerCount++;
}
}
public void incrementShipping() {
synchronized (shippingLock) {
shippingCount++;
}
}
public long getCustomerCount() {
synchronized (customerLock) {
return customerCount;
}
}
public long getShippingCount() {
synchronized (shippingLock) {
return shippingCount;
}
}
}
public class LockSplitting implements Runnable {
private static final int NUMBER_OF_THREADS = 5;
private Counter counter;
public interface Counter {
void incrementCustomer();
void incrementShipping();
long getCustomerCount();
long getShippingCount();
}
public static class CounterOneLock implements Counter { ... }
public static class CounterSeparateLock implements Counter { ... }
public LockSplitting(Counter counter) {
this.counter = counter;
}
public void run() {
for (int i = 0; i < 100000; i++) {
if (ThreadLocalRandom.current().nextBoolean()) {
counter.incrementCustomer();
} else {
counter.incrementShipping();
}
}
}
public static void main(String[] args) throws InterruptedException {
Thread[] threads = new Thread[NUMBER_OF_THREADS];
Counter counter = new CounterOneLock();
for (int i = 0; i < NUMBER_OF_THREADS; i++) {
threads[i] = new Thread(new LockSplitting(counter));
}
long startMillis = System.currentTimeMillis();
for (int i = 0; i < NUMBER_OF_THREADS; i++) {
threads[i].start();
}
for (int i = 0; i < NUMBER_OF_THREADS; i++) {
threads[i].join();
}
System.out.println((System.currentTimeMillis() - startMillis) + "ms");
}
}
public static class CounterReadWriteLock implements Counter {
private final ReentrantReadWriteLock customerLock = new ReentrantReadWriteLock();
private final Lock customerWriteLock = customerLock.writeLock();
private final Lock customerReadLock = customerLock.readLock();
private final ReentrantReadWriteLock shippingLock = new ReentrantReadWriteLock();
private final Lock shippingWriteLock = shippingLock.writeLock();
private final Lock shippingReadLock = shippingLock.readLock();
private long customerCount = 0;
private long shippingCount = 0;
public void incrementCustomer() {
customerWriteLock.lock();
customerCount++;
customerWriteLock.unlock();
}
public void incrementShipping() {
shippingWriteLock.lock();
shippingCount++;
shippingWriteLock.unlock();
}
public long getCustomerCount() {
customerReadLock.lock();
long count = customerCount;
customerReadLock.unlock();
return count;
}
public long getShippingCount() {
shippingReadLock.lock();
long count = shippingCount;
shippingReadLock.unlock();
return count;
}
}
public static class CounterAtomic implements Counter {
private AtomicLong customerCount = new AtomicLong();
private AtomicLong shippingCount = new AtomicLong();
public void incrementCustomer() {
customerCount.incrementAndGet();
}
public void incrementShipping() {
shippingCount.incrementAndGet();
}
public long getCustomerCount() {
return customerCount.get();
}
public long getShippingCount() {
return shippingCount.get();
}
}
public static class CarRepositoryWithCounter implements CarRepository {
private Map<String, Car> cars = new HashMap<String, Car>();
private Map<String, Car> trucks = new HashMap<String, Car>();
private Object carCountSync = new Object();
private int carCount = 0;
public void addCar(Car car) {
if (car.getLicencePlate().startsWith("C")) {
synchronized (cars) {
Car foundCar = cars.get(car.getLicencePlate());
if (foundCar == null) {
cars.put(car.getLicencePlate(), car);
synchronized (carCountSync) {
carCount++;
}
}
}
} else {
synchronized (trucks) {
Car foundCar = trucks.get(car.getLicencePlate());
if (foundCar == null) {
trucks.put(car.getLicencePlate(), car);
synchronized (carCountSync) {
carCount++;
}
}
}
}
}
public int getCarCount() {
synchronized (carCountSync) {
return carCount;
}
}
}
public static class CarRepositoryWithoutCounter implements CarRepository {
private Map<String, Car> cars = new HashMap<String, Car>();
private Map<String, Car> trucks = new HashMap<String, Car>();
public void addCar(Car car) {
if (car.getLicencePlate().startsWith("C")) {
synchronized (cars) {
Car foundCar = cars.get(car.getLicencePlate());
if (foundCar == null) {
cars.put(car.getLicencePlate(), car);
}
}
} else {
synchronized (trucks) {
Car foundCar = trucks.get(car.getLicencePlate());
if (foundCar == null) {
trucks.put(car.getLicencePlate(), car);
}
}
}
}
public int getCarCount() {
synchronized (cars) {
synchronized (trucks) {
return cars.size() + trucks.size();
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有