《Java并发编程实战》第十二章 测试并发程序 读书笔记
一、正确性測试
import java.util.concurrent.Semaphore; public class BoundedBuffer<E> { private final Semaphore availableItems, availableSpaces;
private final E[] items;
private int putPosition = 0;
private int takePosition = 0; @SuppressWarnings("unchecked")
public BoundedBuffer(int capacity) {
availableItems = new Semaphore(0);
availableSpaces = new Semaphore(capacity);
items = (E[]) new Object[capacity];
} public boolean isEmpty() {
return availableItems.availablePermits() == 0;
} public boolean isFull() {
return availableSpaces.availablePermits() == 0;
} public void put(E x) throws InterruptedException {
availableSpaces.acquire();
doInsert(x);
availableItems.release();
} public E take() throws InterruptedException {
availableItems.acquire();
E item = doExtract();
availableSpaces.release();
return item;
} private synchronized void doInsert(E x) {
int i = putPosition;
items[i] = x;
putPosition = (++i == items.length)? 0 : i;
} private synchronized E doExtract() {
int i = takePosition;
E x = items[i];
items[i] = null;
takePosition = (++i == items.length)? 0 : i;
return x;
} }
import static org.junit.Assert.*;
import org.junit.Test; public class BoundedBufferTests { @Test
public void testIsEmptyWhenConstructed() {
BoundedBuffer<Integer> bb = new BoundedBuffer<Integer>(10);
assertTrue(bb.isEmpty());
assertFalse(bb.isFull());
} @Test
public void testIsFullAfterPuts() throws InterruptedException {
BoundedBuffer<Integer> bb = new BoundedBuffer<Integer>(10);
for (int i = 0; i < 10; i++) {
bb.put(i);
}
assertTrue(bb.isFull());
assertTrue(bb.isEmpty());
}
}
@Test
public void testTakeBlocksWhenEmpty(){
final BoundedBuffer<Integer> bb = new BoundedBuffer<Integer>(10);
Thread taker = new Thread(){
@Override
public void run() {
try {
int unused = bb.take();
fail(); //假设运行到这里。那么表示出现了一个错误
} catch (InterruptedException e) { }
}
};
try {
taker.start();
Thread.sleep(LOCKUP_DETECT_TIMEOUT);
taker.interrupt();
taker.join(LOCKUP_DETECT_TIMEOUT);
assertFalse(taker.isAlive());
} catch (InterruptedException e) {
fail();
}
}
被堵塞线程并不须要进入WAITING或者TIMED_WAITING等状态,因此JVM能够选择通过自旋等待来实现堵塞。
3 安全性測试
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import junit.framework.TestCase; public class PutTakeTest extends TestCase {
private static final ExecutorService pool = Executors.newCachedThreadPool();
private final AtomicInteger putSum = new AtomicInteger(0);
private final AtomicInteger takeSum = new AtomicInteger(0);
private final CyclicBarrier barrier;
private final BoundedBuffer<Integer> bb;
private final int nTrials, nPairs; public static void main(String[] args) {
new PutTakeTest(10, 10, 100000).test(); // 演示样例參数
pool.shutdown();
} static int xorShift(int y) {
y ^= (y << 6);
y ^= (y >>> 21);
y ^= (y << 7);
return y;
} public PutTakeTest(int capacity, int nPairs, int nTrials) {
this.bb = new BoundedBuffer<Integer>(capacity);
this.nTrials = nTrials;
this.nPairs = nPairs;
this.barrier = new CyclicBarrier(nPairs * 2 + 1);
} void test() {
try {
for (int i = 0; i < nPairs; i++) {
pool.execute(new Producer());
pool.execute(new Consumer());
}
barrier.await(); // 等待全部的线程就绪
barrier.await(); // 等待全部的线程运行完毕
assertEquals(putSum.get(), takeSum.get());
} catch (Exception e) {
throw new RuntimeException(e);
}
} class Producer implements Runnable {
@Override
public void run() {
try {
int seed = (this.hashCode() ^ (int) System.nanoTime());
int sum = 0;
barrier.await();
for (int i = nTrials; i > 0; --i) {
bb.put(seed);
sum += seed;
seed = xorShift(seed);
}
putSum.getAndAdd(sum);
barrier.await();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
} class Consumer implements Runnable {
@Override
public void run() {
try {
barrier.await();
int sum = 0;
for (int i = nTrials; i > 0; --i) {
sum += bb.take();
}
takeSum.getAndAdd(sum);
barrier.await();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
class Big {
double[] data = new double[100000];
}; void testLeak() throws InterruptedException{
BoundedBuffer<Big> bb = new BoundedBuffer<Big>(CAPACITY);
int heapSize1 = /* 生成堆的快照 */;
for (int i = 0; i < CAPACITY; i++){
bb.put(new Big());
}
for (int i = 0; i < CAPACITY; i++){
bb.take();
}
int heapSize2 = /* 生成堆的快照 */;
assertTrue(Math.abs(heapSize1 - heapSize2) < THRESHOLD);
}
5 使用回调
6 产生很多其它的交替操作
二、性能測试
1 在PutTakeTest中添加计时功能
this .timer = new BarrierTimer();
this .barrier = new CyclicBarrier(nPairs * 2 + 1, timer); public class BarrierTimer implements Runnable{
private boolean started ;
private long startTime ;
private long endTime ; @Override
public synchronized void run() {
long t = System.nanoTime();
if (!started ){
started = true ;
startTime = t;
} else {
endTime = t;
}
} public synchronized void clear(){
started = false ;
} public synchronized long getTime(){
return endTime - startTime;
}
}
void test(){
try {
timer.clear();
for (int i = 0; i < nPairs; i++){
pool .execute( new Producer());
pool .execute( new Consumer());
}
barrier .await();
barrier .await();
long nsPerItem = timer.getTime() / ( nPairs * (long )nTrials );
System. out .println("Throughput: " + nsPerItem + " ns/item");
assertEquals(putSum.get(), takeSum.get() )
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) throws InterruptedException {
int tpt = 100000; // 每一个线程中的測试次数
for (int cap = 1; cap <= tpt; cap *= 10){
System. out .println("Capacity: " + cap);
for (int pairs = 1; pairs <= 128; pairs *= 2){
TimedPutTakeTest t = new TimedPutTakeTest(cap, pairs, tpt);
System. out .println("Pairs: " + pairs + "\t");
t.test();
System. out .println("\t" );
Thread. sleep(1000);
t.test();
System. out .println();
Thread. sleep(1000);
}
}
pool .shutdown();
}
2 多种算法的比較
3 响应性衡量
三、避免性能測试的陷阱
1 垃圾回收
2 动态编译
3 对代码路径的不真实採样
4 不真实的竞争程度
5 无用代码的消除
四、其它的測试方法
1 代码审查
2 静态分析工具
4 分析与监測工具
版权声明:本文博主原创文章,博客,未经同意不得转载。
《Java并发编程实战》第十二章 测试并发程序 读书笔记的更多相关文章
- 《Java并发编程实战》第三章 对象的共享 读书笔记
一.可见性 什么是可见性? Java线程安全须要防止某个线程正在使用对象状态而还有一个线程在同一时候改动该状态,并且须要确保当一个线程改动了对象的状态后,其它线程能够看到发生的状态变化. 后者就是可见 ...
- 《Java并发编程实战》第七章 取消与关闭 读书笔记
Java没有提供不论什么机制来安全地(抢占式方法)终止线程,尽管Thread.stop和suspend等方法提供了这种机制,可是因为存在着一些严重的缺陷,因此应该避免使用. 但它提供了中断In ...
- 《Java并发编程实战》第四章 对象的组合 读书笔记
一.设计线程安全的类 在设计线程安全类的过程中,须要包括下面三个基本要素: . 找出构成对象状态的全部变量. . 找出约束状态变量的不变性条件. . 建立对象状态的并发訪问管理策略. 分析对象的 ...
- 《Java并发编程实战》第十章 避免活跃性危急 读书笔记
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/love_world_/article/details/27635333 一.死锁 所谓死锁: 是指两 ...
- 《Java并发编程实战》第八章 线程池的使用 读书笔记
一.在任务与运行策略之间的隐性解耦 有些类型的任务须要明白地指定运行策略,包含: . 依赖性任务.依赖关系对运行策略造成约束.须要注意活跃性问题. 要求线程池足够大,确保任务都能放入. . 使用线程封 ...
- 并发编程从零开始(十二)-Lock与Condition
并发编程从零开始(十二)-Lock与Condition 8 Lock与Condition 8.1 互斥锁 8.1.1 锁的可重入性 "可重入锁"是指当一个线程调用 object.l ...
- 【Java并发编程实战】----- AQS(二):获取锁、释放锁
上篇博客稍微介绍了一下AQS,下面我们来关注下AQS的所获取和锁释放. AQS锁获取 AQS包含如下几个方法: acquire(int arg):以独占模式获取对象,忽略中断. acquireInte ...
- 《Java并发编程实战》第六章 任务运行 读书笔记
一. 在线程中运行任务 无限制创建线程的不足 .线程生命周期的开销很高 .资源消耗 .稳定性 二.Executor框架 Executor基于生产者-消费者模式.提交任务的操作相当于生产者.运行任务的线 ...
- 《Java并发编程实战》第十一章 性能与可伸缩性 读书笔记
造成开销的操作包含: 1. 线程之间的协调(比如:锁.触发信号以及内存同步等) 2. 添加�的上下文切换 3. 线程的创建和销毁 4. 线程的调度 一.对性能的思考 1 性能与可伸缩性 执行速度涉及下 ...
随机推荐
- Android 最火的高速开发框架AndroidAnnotations使用具体解释
Android 最火的高速开发框架androidannotations配置具体解释文章中有eclipse配置步骤.Android 最火高速开发框架AndroidAnnotations简介文章中的简介. ...
- 【重拾Effective Java】一
之前看这本<Effective Java(第二版)>都是非常早曾经了.这本书确实是本好书.须要细嚼慢咽,每次看都有不同的体验. 在此写博客巩固一下. 第一章.创建和销毁对象 考虑用静态工厂 ...
- php课程 8-29 gd库能够画哪些东西
php课程 8-29 gd库能够画哪些东西 一.总结 一句话总结:文字,点,线,圆,弧线,矩形,各种形状都是可以的,和html5中的canva能画的东西很像,使用也很像,参数怎么记呢,参数完全不用记, ...
- Java 线程第三版 第九章 Thread调度 读书笔记
一.Thread调度的概述 import java.util.*; import java.text.*; public class Task implements Runnable { long n ...
- 黑马day18 jquery高级特性&Ajax的load方法
介绍jquery中的load方法: (1).前面没有jquery.修饰,能够判断出他是一个普通的非全局函数(也就是说是一个局部函数):$.,$().,jquery.等修饰的就是全局函数.没有这些修饰的 ...
- Html表单中遇到的问题
原文 https://www.jianshu.com/p/4466b8294007 大纲 1.表单提交的方式GET和POST的区别 2.js无法对input的file类型的值进行赋值 3.js获取in ...
- Delphi 获取Internet缓存文件 -- FindFirstUrlCacheEntry FindNextUrlCacheEntry
下面是我写的一个函数,把所有的缓存文件路径添加到一个字符串列表中,直接看代码,带了注释.另外还有删除缓存等等大家自己到msdn找找. 需要引用 WinInet // 获取Internet缓存文件 fu ...
- 创建、删除swap分区
创建 dd if=/dev/zero of=/data/swap bs=1M count=4000 mkswap /data/swap swapon /data/swap chmod 060 ...
- 浏览器对象模型(BOM)是什么?(体系结构+知识详解)(图片:结构)
浏览器对象模型(BOM)是什么?(体系结构+知识详解)(图片:结构) 一.总结 1.BOM操作所有和浏览器相关的东西:网页文档dom,历史记录,浏览器屏幕,浏览器信息,文档的地址url,页面的框架集. ...
- 工具类与工具函数 —— NextPrime
求大于某数的下一个素数: static int NextPrime (int N) { if (N % 2 == 0) ++N; int i; for (; ; N += 2){ for (i = 3 ...