//请求总数 public static int clientTotal = 5000; //同时并发执行的线程数 public static int threadTotal = 200; //变量声明:计数 public static AtomicInteger count = new AtomicInteger(0);
public static void main(String[] args) throws InterruptedException { ExecutorService executorService = Executors.newCachedThreadPool();//创建线程池 final Semaphore semaphore = new Semaphore(threadTotal);//定义信号量,给出允许并发的数目 final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);//定义计数器闭锁 for (int i = 0;i<clientTotal;i++){ executorService.execute(()->{ try { semaphore.acquire();//判断进程是否允许被执行 add(); semaphore.release();//释放进程 } catch (InterruptedException e) { log.error("excption",e); } countDownLatch.countDown(); }); } countDownLatch.await();//保证信号量减为0 executorService.shutdown();//关闭线程池 log.info("count:{}",count.get());//变量取值 }
public final int incrementAndGet() { return unsafe.getAndAddInt(this, valueOffset, 1) + 1; }123
而getAndAddInt方法的具体实现为:
1 2 3 4 5 6 7
public final int getAndAddInt(Object var1, long var2, int var4) { int var5; do { var5 = this.getIntVolatile(var1, var2); } while(!this.compareAndSwapInt(var1, var2, var5, var5 + var4)); return var5; }1234567
public final native boolean compareAndSwapObject(Object var1, long var2, Object var4, Object var5); public final native boolean compareAndSwapInt(Object var1, long var2, int var4, int var5); public final native boolean compareAndSwapLong(Object var1, long var2, long var4, long var6);123
public final boolean compareAndSet(boolean expect, boolean update) { int e = expect ? 1 : 0; int u = update ? 1 : 0; return unsafe.compareAndSwapInt(this, valueOffset, e, u); }12345
//是否发生过 private static AtomicBoolean isHappened = new AtomicBoolean(false); // 请求总数 public static int clientTotal = 5000; // 同时并发执行的线程数 public static int threadTotal = 200;
public static void main(String[] args) throws Exception { ExecutorService executorService = Executors.newCachedThreadPool(); final Semaphore semaphore = new Semaphore(threadTotal); final CountDownLatch countDownLatch = new CountDownLatch(clientTotal); for (int i = 0; i < clientTotal ; i++) { executorService.execute(() -> { try { semaphore.acquire(); test(); semaphore.release(); } catch (Exception e) { log.error("exception", e); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); log.info("isHappened:{}", isHappened.get()); }
源码: private static class Pair<T> { final T reference; final int stamp; private Pair(T reference, int stamp) { this.reference = reference; this.stamp = stamp; } static <T> Pair<T> of(T reference, int stamp) { return new Pair<T>(reference, stamp); } }
public class SynchronizedExample { public void test(int j){ synchronized (this){ for (int i = 0; i < 10; i++) { log.info("test - {} - {}",j,i); } } } //使用线程池方法进行测试: public static void main(String[] args) { SynchronizedExample example1 = new SynchronizedExample(); SynchronizedExample example2 = new SynchronizedExample(); ExecutorService executorService = Executors.newCachedThreadPool(); executorService.execute(()-> example1.test(1)); executorService.execute(()-> example2.test(2)); } }1234567891011121314151617
结果:不同对象之间的操作互不影响
synchronized 修饰一个方法
被修饰的方法称为同步方法,作用的范围是大括号括起来的部分,作用的对象是调用这段代码的对象。 验证:
1 2 3 4 5 6 7 8 9
public class SynchronizedExample public synchronized void test(int j){ for (int i = 0; i < 10; i++) { log.info("test - {} - {}",j,i); } } //验证方法与上面相同 ... }123456789
public class SynchronizedExample{ public static synchronized void test(int j){ for (int i = 0; i < 10; i++) { log.info("test - {} - {}",j,i); } } //验证方法与上面相同 ... }123456789
结果:同一时间只有一个线程可以执行
synchronized 修饰一个类
验证:
1 2 3 4 5 6 7 8 9 10 11
public class SynchronizedExample{ public static void test(int j){ synchronized (SynchronizedExample.class){ for (int i = 0; i < 10; i++) { log.info("test - {}-{}",j,i); } } } //验证方法与上面相同 ... }1234567891011