ExecutorService that interrupts tasks after a timeout

后端 未结 9 2197
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 16:19

I\'m looking for an ExecutorService implementation that can be provided with a timeout. Tasks that are submitted to the ExecutorService are interrupted if they take longer t

9条回答
  •  清酒与你
    2020-11-22 16:49

    It seems problem is not in JDK bug 6602600 ( it was solved at 2010-05-22), but in incorrect call of sleep(10) in circle. Addition note, that the main Thread must give directly CHANCE to other threads to realize thier tasks by invoke SLEEP(0) in EVERY branch of outer circle. It is better, I think, to use Thread.yield() instead of Thread.sleep(0)

    The result corrected part of previous problem code is such like this:

    .......................
    ........................
    Thread.yield();         
    
    if (i % 1000== 0) {
    System.out.println(i + "/" + counter.get()+ "/"+service.toString());
    }
    
    //                
    //                while (i > counter.get()) {
    //                    Thread.sleep(10);
    //                } 
    

    It works correctly with amount of outer counter up to 150 000 000 tested circles.

提交回复
热议问题