0

I am trying to create a kind of scheduler, which works fine for simple tasks in a queue.

For Example:
If my queue has 100 jobs for 100 customers means 1 job for each customer. 

But now I want to add multiple jobs for each customer.

for(int i = 1; i <= 100; i++) {
  customerIds.add(i);
}
List<Job> jobs = new ArrayList<>();
for(Integer customerId: customerIds) {
  for(int i = 1; i <= 10000; i++) {
    jobs.add(new MyJob(customerId, 100));
  }
}

Now I want to execute 1 job at a time for every customer instead of blocking the program to execute all jobs for 1 customer.

Can someone lead me in some direction? I am new to multithreading and not sure how can I make use of that here.

Also, the queue is not only for a predefined collection of jobs. It is kind of a stream where jobs will keep coming and gets executed.

  • are the customers the entity that doing the work? are they `thread`? this discussion on [multi-threading](https://stackoverflow.com/questions/2865315/threads-in-java) seems to be a good start. you can make list of `Jobs` to be finished and [distribute them](https://stackoverflow.com/questions/2041718/round-robin-scheduling-java-iterators) among your "customers." anyway, in regard to the *"it is kind of a stream where jobs keep coming"* you have to be specific how the jobs are coming in. – Bagus Tesa Nov 09 '22 at 15:10
  • @BagusTesa No, customers are not doing the work. The work means the task has a category that belongs to per customer. And the pain point for me is to get fairness for each customer instead of hogging all the resources only for executing all jobs for 1 customer. If I sleep my main thread, say for 1000 ms, there are 100 customers having 1000 jobs each. Each job takes around 1ms then. I want my system to execute one job for each customer instead of using all 1000ms for running all 1000 jobs for customer 1. – Himanshu Goyal Nov 10 '22 at 05:16
  • Also, at the start, I filled my queue with the job List as declared in the example. Now queue can accept more jobs and the system will not terminate until the shutdown is called. – Himanshu Goyal Nov 10 '22 at 05:20

0 Answers0