小编jix*_*989的帖子

当executor遇到Thread.sleep时,为什么线程没有休眠就结束或者抛出InterruptedException?

我不知道为什么 junit 有以下行为:

当我将一个线程提交到执行程序(fixedThreadPool 或其他),而该线程具有睡眠行为(Thread.sleep())时,我发现该线程立即结束!没有睡眠,没有中断的异常。

我想知道为什么?

(我知道下面的设计不好,但我只是想描述一下上面的问题)

@Test
public void executorPoolTest(){
    ExecutorService cachedThreadPool = 
            Executors.newFixedThreadPool(10);
    //Executors.newCachedThreadPool();  
    for (int i = 0; i < 1000; i++) {  
        final int index = i;  
        cachedThreadPool.execute(new Runnable() {  
            public void run() {  
                System.out.println(index);
                try {  
                    Thread.sleep( 5000); 
                    System.out.println(index+"_");
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
            }  
        });  
    }
    //no matter whether you call shutdown, the above program will ends quickly.
    cachedThreadPool.shutdown();

}
Run Code Online (Sandbox Code Playgroud)

上面的程序没有打印任何数字+“_”,很奇怪。但是,如果上述方法在主方法中运行(我的意思是:public static void main(String[] args)),则行为正常,线程将休眠并且可以打印“_”。

我想知道为什么?junit框架发生了什么?如果是这样,我不能使用 junit 来测试执行器涉及的方法吗?

junit版本是4.12。

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId> …
Run Code Online (Sandbox Code Playgroud)

java junit multithreading executorservice thread-sleep

2
推荐指数
1
解决办法
2687
查看次数

使用 datastax java 驱动程序为 where in 子句绑定数据

我知道有人问过这个问题(http://www.datastax.com/support-forums/topic/java-driver-cql-3-bound-statements-in-clause#post-13114在 IN 中收集的准备好的语句Datastax Cassandra CQL 驱动程序中的子句

但是,我仍然不知道如何绑定数据。例如,我的 prepareStatement 是

select * from cf where key in (?)
Run Code Online (Sandbox Code Playgroud)

我想要的是绑定数据,cql 看起来像

select * from cf where key in ('key1', 'key2')
Run Code Online (Sandbox Code Playgroud)

现在我有一个 boundStatement。

当我使用 boundStatment.bind() api 时。我试着绑定?但是,对于列表或数组,它说:

列值为 varchar 类型,不能设置为列表

好的,我设置了一个字符串并像这样使用 boundStatement.bind:

boundStatement.bind("'key1','key2'");
Run Code Online (Sandbox Code Playgroud)

没有例外,但结果集是空的。为什么?我认为是因为驱动程序将其解析为

select * from cf where key in (''key1', 'key2'')
Run Code Online (Sandbox Code Playgroud)

(注意一个多余的引号,因为它认为所有的“'key1'和'key2'”都是一个字符串)。

所以,我知道 datastax 在条款中有支持。但我找不到一些例子..

任何人都可以帮助我吗?谢谢!

cassandra datastax-java-driver datastax

0
推荐指数
1
解决办法
1442
查看次数