After taking help from StackOverflow, I found the solution that I have implemented below.
Problem Statement:-
Each thread needs to use UNIQUE ID
every time and it has to run for 60 minutes
or more, So in that 60 minutes
it is possible that all the ID's
will get finished so I need to reuse those ID's
again. So I am using ArrayBlockingQueue
concept here.
Two Scenario:-
- If the
command.getDataCriteria()
containsPrevious
then each thread always needs to useUNIQUE ID
between1 and 1000
and release it for reusing again. - Else if the
command.getDataCriteria()
containsNew
then each thread always needs to useUNIQUE ID
between2000 and 3000
and release it for reusing again.
Question:-
One weird thing that I have just noticed is- In the below else if loop if you see my below code in run method if the command.getDataCriteria() is Previous
then also it gets entered in the else if block(which is for New)
which shouldn't be happening right as I am doing a .equals check
? Why this is happening?
else if(command.getDataCriteria().equals(PDSLnPConstants.DATA_CRITERIA_NEW)) {
Below is my code:-
class ThreadNewTask implements Runnable {
private Command command;
private BlockingQueue<Integer> existPool;
private BlockingQueue<Integer> newPool;
private int existId;
private int newId;
public ThreadNewTask(Command command, BlockingQueue<Integer> pool1, BlockingQueue<Integer> pool2) {
this.command = command;
this.existPool = pool1;
this.newPool = pool2;
}
public void run() {
if(command.getDataCriteria().equals(PDSLnPConstants.DATA_CRITERIA_PREVIOUS)) {
try {
existId = existPool.take();
someMethod(existId);
} catch (Exception e) {
System.out.println(e);
} finally {
existPool.offer(existId);
}
} else if(command.getDataCriteria().equals(PDSLnPConstants.DATA_CRITERIA_NEW)) {
try {
newId = newPool.take();
someMethod(newId);
} catch (Exception e) {
System.out.println(e);
} finally {
newPool.offer(newId);
}
}
}
// And this method needs to be synchronized or not?
private synchronized void someMethod(int i) {
System.out.println();
System.out.println("#####################");
System.out.println("Task ID: " +i);
System.out.println("#####################");
System.out.println();
}
}
public class TestingPool {
public static void main(String[] args) throws InterruptedException {
int size = 10;
int durationOfRun = 60;
LinkedList<Integer> availableExistingIds = new LinkedList<Integer>();
LinkedList<Integer> availableNewIds = new LinkedList<Integer>();
for (int i = 1; i <= 1000; i++) {
availableExistingIds.add(i);
}
for (int i = 2000; i <= 3000; i++) {
availableNewIds.add(i);
}
BlockingQueue<Integer> existIdPool = new ArrayBlockingQueue<Integer>(1000, false, availableExistingIds);
BlockingQueue<Integer> newIdPool = new ArrayBlockingQueue<Integer>(1000, false, availableNewIds);
// create thread pool with given size
ExecutorService service = new ThreadPoolExecutor(size, size, 500L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), new ThreadPoolExecutor.CallerRunsPolicy());
// queue some tasks
long startTime = System.currentTimeMillis();
long endTime = startTime + (durationOfRun * 60 * 1000L);
// Running it for 60 minutes
while(System.currentTimeMillis() <= endTime) {
Command nextCommand = getNextCommandToExecute();
service.submit(new ThreadNewTask(nextCommand, existIdPool, newIdPool));
}
// wait for termination
service.shutdown();
service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
}
}
Update:-
One weird thing that I have just noticed is- In the below else if loop
if the command is Previous
then also it gets entered in the else if block which shouldn't be happening right? Why this is happening? I have no clue why this thing is happening?
else if(command.getDataCriteria().equals(PDSLnPConstants.DATA_CRITERIA_NEW)) {