Runnable和ExecutorService的奇怪行为 [英] Odd behavior with Runnable and ExecutorService

查看:227
本文介绍了Runnable和ExecutorService的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在多线程中遇到了一些非常奇怪的行为.我有两个类:DipoleTester和Dipole.

I'm getting some really weird behavior with multithreading. I have two classes: DipoleTester and Dipole.

DipoleTester尝试创建多个Dipole对象,然后异步运行它们.问题在于DipoleTester只是一次运行所有其Dipole对象,而不是一次运行2个.

DipoleTester attempts to create several Dipole objects, and then run them asynchronously. The problem is that DipoleTester just runs all of its Dipole objects at once, rather than 2 at a time.

这里是DipoleTester:

Here is DipoleTester:

public class DipoleTester {
    public static String DIR = "./save/";
    public static void main(String[] args) throws InterruptedException {
        Dipole trial;
        ExecutorService service = Executors.newFixedThreadPool(2);      

        for (int r = 10; r < 13; r += 1) {
            double radius = (double) r / 10000.0;
            for (int matType = 0; matType < 3; matType++) {
                String name = "Simple_mat"+matType + "_rad" + radius;
                trial = new DipoleSimple(DIR + "Simple/", name);
                trial.materialType = matType;
                trial.RADIUS = radius;
                service.submit(trial);

            }
        }
        service.shutdown();
        service.awaitTermination(100, TimeUnit.HOURS);
    }
}

这是偶极子的(相关位)

And here are the (relevant bits) from Dipole

public abstract class Dipole implements Runnable{
    ...     
    public void run() {
        initiate();
    }
    public void initiate() {
        DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        Date date = new Date();
        System.out.println(dateFormat.format(date) + ": Starting: " + NAME);
        model = ModelUtil.create(NAME);
        model.modelNode().create("mod1");
        makeParams();
        makeVariables();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    ...
}

现在的问题是,即使使用thread.sleep(5000),所有线程也会一次执行!我不知道是怎么回事.这是控制台输出:

The problem now is that all the threads execute at once, even with the thread.sleep(5000)! I have no idea what's going on. Here is the console output:

05/08/2013 19:17:31: Starting: Simple_mat0_rad0.001
05/08/2013 19:17:31: Starting: Simple_mat1_rad0.001
05/08/2013 19:17:31: Starting: Simple_mat2_rad0.001
05/08/2013 19:17:31: Starting: Simple_mat0_rad0.0011
05/08/2013 19:17:31: Starting: Simple_mat1_rad0.0011
05/08/2013 19:17:31: Starting: Simple_mat2_rad0.0011
05/08/2013 19:17:31: Starting: Simple_mat0_rad0.0012
05/08/2013 19:17:31: Starting: Simple_mat1_rad0.0012
05/08/2013 19:17:31: Starting: Simple_mat2_rad0.0012

推荐答案

您的Runnable任务在进入Thread.sleep()调用之前引发了异常.这允许下一个任务开始执行.所有任务都以如此快的速度连续失败,以至于它们似乎同时运行.

Your Runnable task is throwing an exception before it gets to the Thread.sleep() call. This allows the next task to begin execution. All the tasks fail in such quick succession that all appear to run concurrently.

首先在run()方法中进行对Thread.sleep()的调用,您将看到一次仅运行两个线程.

Make the call to Thread.sleep() the first thing you do in your run() method and you will see that only two threads run at a time.

要检测此类故障,您需要其他用于批量提交任务列表并等待其完成的方法,可能更适合您的应用程序.

To detect such failures, you'll need to examine the Future instance that results from each call to submit(). There are other methods for submitting a list of tasks in bulk, and waiting for them to complete, that might be better suited to your application.

这篇关于Runnable和ExecutorService的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆