Saturday 15 June 2013

java - Program Threads hanging when using a semaphore -



java - Program Threads hanging when using a semaphore -

i have issue setting critical section semaphore between 2 threads. using semaphore acquire(send) in client thread , release in teller thread. when run programme hanging , don't know why. have tried several things , not sure issue was.

i trying utilize deposit semaphore set critical section client thread , teller thread.

import java.util.concurrent.semaphore; public class threads { // private int customernumber = 0; private static semaphore deposit = new semaphore (0, true); public static void main(string[] args) { final int customerthreads = 5; final int tellerthreads = 2; final int loanthreads = 1; client thr[] = new customer[customerthreads]; // create 5 client threads thread cthread[] = new thread[customerthreads]; // made 5 threads (int i= 0; < customerthreads; i++) { thr[i]= new customer(i); cthread[i] = new thread(thr [i]); cthread[i].start(); } ( int = 0; < customerthreads; i++ ) { seek { cthread[i].join (); system.out.println("customer"+i + "joined main"); } grab (interruptedexception e) { } } teller thr1[] = new teller[tellerthreads]; thread tthread[] = new thread[tellerthreads]; (int b = 0; b< tellerthreads; b++) { thr1[b] = new teller(b); tthread[b]= new thread(thr1 [b]); tthread[b].start(); } loanofficer thr2[] = new loanofficer[loanthreads]; thread lthread[] = new thread[loanthreads]; for(int c = 0; c< loanthreads; c++) { thr2[c] = new loanofficer(c); lthread[c] = new thread(thr2 [c]); lthread[c].start(); } // todo code application logic here } static class client implements runnable { private int customernumber = 0; private int balance = 0; customer(int cn) { this.customernumber = cn; balance = 1000; system.out.println("customer"+ customernumber + "created"); } public void run() { seek { thread.sleep(200); deposit.acquire(); } catch(interruptedexception e) { thread.currentthread().interrupt(); e.printstacktrace(); } //system.out.println("customer"+ customernumber + "created"); // seek } public void post() { } } static class teller implements runnable { private int tellernumber = 0; teller(int tn) { this.tellernumber = tn; system.out.println("teller"+ tellernumber +"created"); } public void run() { deposit.release(); // seek // { // // // deposit.release(); // // thread.sleep(100); // // deposit.acquire(); // } // catch(interruptedexception e) // { // deposit.release(); // } //system.out.println("teller"+ tellernumber +"created"); } public void post() { } } static class loanofficer implements runnable { private int loannumber = 0; loanofficer(int tn) { this.loannumber = tn; system.out.println("loanofficer"+loannumber+"created"); } public void run() { //system.out.println("loanofficer"+loannumber+"created"); } public void post() { } } }

you instantiate semaphore no permits below:

private static semaphore deposit = new semaphore (0, true);

hence when seek phone call acquire, wont permit execute beyond , hence blocks. seek atleast having 1 permit @ time 1 thread can execute post acquire permit semaphore. increment later well.

private static semaphore deposit = new semaphore (1, true);

refer doc here

java multithreading concurrency thread-safety semaphore

No comments:

Post a Comment