简体   繁体   English

Java Executor-不并行运行线程

[英]Java Executor - not running threads in parallel

I am trying to execute independent task parallel using the java.util.concurrent.Executor . 我正在尝试使用java.util.concurrent.Executor并行执行独立任务。

I have the following working code 我有以下工作代码

public class MiniTest {

    private static String[] input;
        static {
            input = new String[] { 
                    "task1", 
                    "task2",
                    "task3",
                    "task4"
                };
        }

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executor = Executors.newFixedThreadPool(2);
        boolean atleastOnePoolStarted = false;
        for (int i = 0; i < input.length; i++) {
            Runnable worker = new WorkerThread(input[i] + i);
            executor.execute(worker);
        }
        executor.shutdown();
        executor.awaitTermination(15,TimeUnit.MINUTES);
        System.out.println("Finished all threads");
    }
}

This is working fine, and I see parallel execution. 一切正常,我看到了并行执行。

But the problem is, when I am replacing the WorkerThread with my other class which is Runnable and does a stored procedure call by connecting to database. 但问题是,当我更换WorkerThread与我的其他类,这是Runnable ,并通过连接到数据库做一个存储过程调用。 In that case, the threads are getting started but the actual call to stored procedures seems to be in a procedural way ie the 2nd java-proc call is not getting executed until the first one finishes. 在那种情况下,线程正在启​​动,但是对存储过程的实际调用似乎是以一种过程方式进行的,即直到第一个java-proc调用才开始执行。

The database part is working fine, as that is verified and tested independently. 数据库部分运行良好,因为它是独立验证和测试的。 I just need to kick 2-3 calls simultaneously. 我只需要同时踢2-3个电话即可。 I am using Sybase as the database. 我正在使用Sybase作为数据库。

Has anyone encountered this problem before? 有人遇到过这个问题吗? Please let me know what could be possibly wrong. 请让我知道可能出了什么问题。

Several ideas abbout what might be wrong: 关于可能存在问题的一些想法:

  • You might open only one connection (Sybase Client was not Multithread-save, when I worked with it some years ago) 您可能只打开一个连接(几年前我使用Sybase Client时不是多线程保存的)
  • The Stored Procedure locks some table that lets the second call wait and block. 存储过程锁定了一些表,该表使第二个调用等待并阻塞。 Do the SP's some kind of insert, update, delete on a table somewhere at the beginning? SP在开始的某个位置是否对表进行某种插入,更新或删除? Maybe inside of a transaction? 也许在交易内? Well, then the second SP will wait for the first one to finish. 好吧,那么第二个SP将等待第一个SP完成。
  • I remember on Sybase one issue for a long time was, to create some object (table, columns, view, temporary table etc.) which exclusively locked some system tables and let the second SP wait for the first one to finish the creation. 我记得在Sybase上一个问题很长一段时间是,创建一些对象(表,列,视图,临时表等),该对象专门锁定了一些系统表,然后让第二个SP等待第一个SP完成创建。 Maybe somebody can advice, if that still can happen. 也许有人可以建议,如果那仍然可能发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM