java - Multithreading: Issue in allocating and blocking resources -


i have multiple resources - sake of understanding 3 resources namely xresource, yresource , zresource (java classes - runnables) able task. there list of tasks needs done in parallel among 3 resources. need resources locked , if 1 of resource locked task should go other resource , if none of resources available should wait till 1 of resource available. trying lock resource using semaphore thread gets assigned 1 runnable , other runnables idle. new multithreading might overlooking obvious. using java se 1.6

below code -

public class test { private final static semaphore xresourcesphore = new semaphore(1, true); private final static semaphore yresourcesphore = new semaphore(1, true); private final static semaphore zresourcesphore = new semaphore(1, true);  public static void main(string[] args) {     arraylist<task> listoftasks = new arraylist<task>();     task task1 = new task();     task task2 = new task();     task task3 = new task();     task task4 = new task();     task task5 = new task();     task task6 = new task();     task task7 = new task();     task task8 = new task();     task task9 = new task();       listoftasks.add(task1);     listoftasks.add(task2);     listoftasks.add(task3);     listoftasks.add(task4);     listoftasks.add(task5);     listoftasks.add(task6);     listoftasks.add(task7);     listoftasks.add(task8);     listoftasks.add(task9);      //runnables     xresource xthread = new xresource();     yresource ythread = new yresource();     zresource zthread = new zresource();      executorservice executorservice = executors.newfixedthreadpool(3);     (int = 0; < listoftasks.size(); i++) {             if (xresourcesphore.tryacquire()) {                 try {                     xthread.settask(listoftasks.get(i));                     executorservice.execute(xthread );                 } {                     xresourcesphore.release();                 }             }else if (yresourcesphore.tryacquire()) {                 try {                     ythread.settask(listoftasks.get(i));                     executorservice.execute(ythread );                 } {                     yresourcesphore.release();                 }             }else if (zresourcesphore.tryacquire()) {                 try {                     zthread.settask(listoftasks.get(i));                     executorservice.execute(zthread );                 } {                     zresourcesphore.release();                 }             }     }     executorservice.shutdown(); } } 

you need move resource locking logic task run in thread.

by doing locking in current thread, not waiting task performed before releasing resource. reason seeing problem you not waiting task complete (or start) before calling settask() on same resource. replaces previous task set.

queue<resource> resources = new concurrentlinkedqueue<>(); resources.add(new xresource()); resources.add(new yresource()); resources.add(new zresource());  executorservice service = executors.newfixedthreadpool(resources.size()); threadlocal<resource> resourcetouse = threadlocal.withinitial(() -> resources.remove());  (int = 1; < 9; i++) {     service.execute(() -> {         task task = new task();         resourcetouse.settask(task);     }); } 

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -