多线程摘录 007

* 测试多线程程序的安全性和生存型
    - 不要做出"伪"测试代码, 即让有问题的代码也能通过的测试
* 测试要关注的几点
    - 吞吐量
    - 响应时间
    - 伸缩性(是否资源越多, 吞吐量越大?)
* 如何测试有阻塞的方法
        The obvious way to do this is via interruptionstart a blocking activity in a separate thread, wait until the thread blocks, interrupt it, and then assert that the blocking operation completed
        void testTakeBlocksWhenEmpty() {
            final BoundedBuffer<Integer> bb = new BoundedBuffer<Integer>(10);
            Thread taker = new Thread() {
                public void run() {
                    try {
                        int unused = bb.take(); //阻塞
                        fail(); // if we get here, it's an error
                    } catch (InterruptedException success) {
                        //忽略中断
                    }
                }};
            try {
                taker.start();
                Thread.sleep(LOCKUP_DETECT_TIMEOUT);
                taker.interrupt();
                taker.join(LOCKUP_DETECT_TIMEOUT);
                assertFalse(taker.isAlive());
            } catch (Exception unexpected) {
                fail();
            }
        }
* 不要使用Thread.getState, 不可靠

* xorShift随机数算法
static int xorShift(int y) {
    y ^= (y << 6);
    y ^= (y >>> 21);
    y ^= (y << 7);
    return y;
}

转自:http://hi.baidu.com/iwishyou2/blog/item/14adde1f66272f02304e15fe.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值