View of load balancing thread pool in java

I am looking for a load-balanced thread pool with no success. (Not sure if load balancing is correct). Let me explain what I'm trying to achieve.

Part 1: I have jobs with 8-10 single tasks. On a 6-core processor, I allow 8 threads to work on these tasks in parallel, which seems to provide the best performance. One task is ready, another may begin. Once all ten tasks are completed, the full work will be completed. Typically, the task is completed after 30-60 seconds.

Part Two: Several times, unfortunately, the work takes more than two hours. This is correct because of the amount of data that needs to be calculated. The bad news is that no other job can start while job1 is running (assuming all threads have the same duration) because it uses all threads.

My first idea: Have 12 threads, allow up to three jobs in parallel. BUT: this means that ku is not completely prevailing when there is only 1 job.

I am looking for a solution to have full processor power to work when there is no other job. But when you need to run another task while the other is working, I want the processor power to be distributed for both tasks. And when the third or fourth task appears, I want the CPU to be fully distributed across all four tasks.

I appreciate your answers ...

early

+8
java multithreading threadpool pool
source share
3 answers

One possibility might be to use the standard ThreadPoolExecutor with another task queue task.

 public class TaskRunner { private static class PriorityRunnable implements Runnable, Comparable<PriorityRunnable> { private Runnable theRunnable; private int priority = 0; public PriorityRunnable(Runnable r, int priority) { this.theRunnable = r; this.priority = priority; } public int getPriority() { return priority; } public void run() { theRunnable.run(); } public int compareTo(PriorityRunnable that) { return this.priority - that.priority; } } private BlockingQueue<Runnable> taskQueue = new PriorityBlockingQueue<Runnable>(); private ThreadPoolExecutor exec = new ThreadPoolExecutor(8, 8, 0L, TimeUnit.MILLISECONDS, taskQueue); public void runTasks(Runnable... tasks) { int priority = 0; Runnable nextTask = taskQueue.peek(); if(nextTask instanceof PriorityRunnable) { priority = ((PriorityRunnable)nextTask).getPriority() + 1; } for(Runnable t : tasks) { exec.execute(new PriorityRunnable(t, priority)); priority += 100; } } } 

The idea here is that when you have a new assignment, you call

 taskRunner.runTasks(jobTask1, jobTask2, jobTask3); 

and he will queue the tasks in such a way that they interspersed well with any existing tasks in the queue (if any). Suppose you have one task, the tasks of which have priority numbers j 1 t 1 = 3, j 1 t 2 = 103 and j 1 t 3 = 203. In the absence of other tasks, these tasks will be performed one after another as quickly as possible. But if you send another task with your three tasks, they will be assigned priority numbers j 2 t 1 = 4, j 2 t 2 = 104 and j 2 t 3 = 204, which means that the queue now looks like

j 1 t 1 , j 2 t 1 , j 1 t 2 , j 2 t 2 , etc.

This is not ideal, because if all the threads are currently running (in tasks from task 1), the first task of task 2 cannot start until one of the tasks of task 1 is completed (if there is no external way for you detect this, interrupt and reinstall some tasks of the 1st task). The easiest way to make things fairer is to break longer tasks into smaller segments and queue them as separate tasks - you need to get to the point where each separate task includes more tasks than there are threads in the pool, so some tasks will always be started in queues, and not assigned directly to threads (if there are idle threads, then exec.execute() passes the task directly to the thread without going through all the queues).

+6
source share

I think, since your computer has 6 core processors. It is better to have 6 work threads for each work thread. Thus, when ever one thread gets a new job, it starts up to six concurrent workers to work on one job. This will ensure that full processor power is consumed when there is only one job at a time.

Also pay attention to the concept of Fork and Join in java 7.
Links_1
References_2
Links_3
Links_4

Also learn about newcachedthreadpool ()

Java newCachedThreadPool () vs newFixedThreadPool

+1
source share

The easiest way to do this is to rewrite your processor, as Kanaga suggests, but start with 8 threads. There may be some overhead at the competition, but if you deal with one situation, it will fully use the processor. The OS will process the time for each thread.

Your "first idea" will also work. Simple threads will not receive resources from 8 worker threads unless they actually perform the task. However, this will not distribute CPU resources as evenly if several tasks are performed.

Do you have a setup where you can test these different pipelines to see how they work for you?

+1
source share

All Articles