多线程摘记 007

多线程摘录 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