简体   繁体   English

Java中的多重处理-Drools和数据库

[英]Multithreaing in java - drools and database

I am pretty new to multi threading and tried something to apply it, but I am kinda stuck. 我对多线程还很陌生,并尝试了一些方法来应用它,但是我还是有点坚持。

Here is the scenario: using drools to apply rules on a set of objects read from the db, then write the updated values back to the db. 这是场景:使用流口水将规则应用于从db读取的一组对象,然后将更新后的值写回到db。

Now, I repeat the above process multiple times, so i want to run the reading+drool process in one thread(main thread) and the writing part in another. 现在,我多次重复上述过程,所以我想在一个线程(主线程)中运行read + drool进程,在另一个线程中运行写作部分。

So I write the below code: 所以我写下面的代码:

Thread thread = new Thread(new Runnable() 
{     public void run()
    { 
         try 
         {
    //writing the updated data in DB    
    aggregationDAO.updateCaseDetailsInOracle(queryList);
    } 
         catch (Exception e) {                              throw new RuntimeException();
    }
   }
});
    thread.start();

But, I am stuck here. 但是,我被困在这里。

Firstly, it expects my queryList to be final. 首先,它期望我的queryList是最终的。

I cannot make it final coz every time new updated data loads in the same variable. 每当新的更新数据加载到同一变量中时,我都无法将其定为最终结果。

Secondly, 其次,

even after making my program run multi threads, there is no improvement in my run time. 即使使程序运行多线程之后,运行时间也没有任何改善。

Can, someone tell me where I am getting wrong? 有人可以告诉我我哪里错了吗?

You just use your custom thread instead of 'main', so there is no improvment. 您只使用自定义线程而不是'main',因此没有任何改进。 There is no 'multi' threading in your example. 您的示例中没有“多”线程。

If you want to see speed improvment, you should run several thread simultaniously. 如果您想提高速度,则应同时运行多个线程。 Use some sort of thread pool for concurrent task processing, and then you will get improvment. 使用某种线程池进行并发任务处理,您将得到即兴的体验。

Also, variable must be final, because you are creating anonymous class - Runnable. 另外,变量必须是最终变量,因为您正在创建匿名类-Runnable。 You should create new class wich will implements Runnable and pass into constructor your variable. 您应该创建新类,该类将实现Runnable并将变量传递给构造函数。

class QueryTask implements Runnable {
    private final List queryList;
    public QueryTask(List queryList) {
        this.queryList = queryList;
    }

    @Override
    public void run() {
        try { //writing the updated data in DB
            aggregationDAO.updateCaseDetailsInOracle(queryList);
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }
}

USAGE: 用法:

final ExecutorService threadPool = Executors.newCachedThreadPool();
threadPool.submit(new QueryTask(queryList.subList(0, 5)));
threadPool.submit(new QueryTask(queryList.subList(6, 10)));

This will process your queryList by portions concurrently. 这将同时处理您的queryList。

UPDATE: 更新:

When you already submit all your tasks, you can shutdown threadPool and wait while all tasks will be done. 当您已经提交了所有任务后,可以关闭threadPool并等待所有任务完成。 You can not add any new task after that. 之后,您无法添加任何新任务。

    threadPool.shutdown();
    try {
        threadPool.awaitTermination(10, TimeUnit.MINUTES);
    } catch (InterruptedException e) {               
        // Current thread was interrupted.
        final List<Runnable> runnables = threadPool.shutdownNow();
        System.out.println("Can't stop tasks: " + runnables.size());

        // Restore interrupted status.
        Thread.currentThread().interrupt();
    }

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

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