简体   繁体   中英

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.

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.

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.

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. 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. You should create new class wich will implements Runnable and pass into constructor your variable.

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.

UPDATE:

When you already submit all your tasks, you can shutdown threadPool and wait while all tasks will be done. 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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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