i have solve problem monitor locks. did code need advice logic. in output example seems checks case of 1 person occupied bathroom , other 1 waiting turn (i'm sorry english, trying discribe can). here output:
man 0 enters bathroom man 0 in bathroom man 0 exits bathroom man 0 enters bathroom man 0 in bathroom man 0 exits bathroom woman 1 enters bathroom woman 1 in bathroom man 2 in waiting------------>>>> woman 1 exits bathroom man 2 in bathroom man 2 exits bathroom woman 2 enters bathroom woman 2 in bathroom woman 2 exits bathroom
and here code of 4 functions.
public void woman_wants_to_enter(int i) throws interruptedexception { lock.lock(); try { if (occupiedcount < numberoftoilets) { if (menusingn == 0) { if (womenwaitingn == 0) { system.out.println("woman " + + " enters bathroom "); womenusingn++; occupiedcount++; } else { while (womenwaitingn != 0) { system.out.println("woman " + + " in waiting------------>>>>"); womenwaitingn++; womenwaitingqueue.await(); } } } else { while (menusingn != 0) { system.out.println("woman " + + " in waiting------------>>>>"); womenwaitingn++; womenwaitingqueue.await(); } } } else { while (occupiedcount == numberoftoilets) { system.out.println("woman " + + " in waiting------------>>>>"); womenwaitingn++; womenwaitingqueue.await(); } } } { lock.unlock(); } } public void woman_leaves(int i) throws interruptedexception { lock.lock(); try { womenusingn--; occupiedcount--; system.out.println("woman " + + " exits bathroom "); if (womenwaitingn > 0) { womenwaitingqueue.signal(); womenusingn++; occupiedcount++; womenwaitingn--; } else if (menwaitingn > 0 && womenusingn == 0) { menwaitingqueue.signal(); menusingn++; occupiedcount++; menwaitingn--; } } { lock.unlock(); } } public void man_wants_to_enter(int i) throws interruptedexception { lock.lock(); try { if (occupiedcount < numberoftoilets) { if (womenusingn == 0) { if (womenwaitingn > 0) { womenwaitingqueue.signal(); womenusingn++; occupiedcount++; womenwaitingn--; } else { menusingn++; occupiedcount++; system.out.println("man " + + " enters bathroom "); menwaitingqueue.signal(); } } else { while (womenusingn != 0) { system.out.println("man " + + " in waiting------------>>>>"); menwaitingn++; menwaitingqueue.await(); } } } else { while (occupiedcount == numberoftoilets) { system.out.println("man " + + " in waiting------------>>>>"); menwaitingn++; menwaitingqueue.await(); } } } { lock.unlock(); } } public void man_leaves(int i) throws interruptedexception { lock.lock(); try { menusingn--; occupiedcount--; system.out.println("man " + + " exits bathroom "); if (womenwaitingn > 0 && menusingn == 0) { womenwaitingqueue.signal(); womenusingn++; occupiedcount++; womenwaitingn--; } else if (menwaitingn > 0) { menwaitingqueue.signal(); menwaitingn--; menusingn++; occupiedcount++; } } { lock.unlock(); } }
i'll appreciate advice btw numberoftoilets=3;
private lock lock = new reentrantlock(); private condition womenwaitingqueue = lock.newcondition(); private condition menwaitingqueue = lock.newcondition(); private int womenwaitingn = 0; private int menwaitingn = 0; private int womenusingn = 0; private int menusingn = 0; private int numberoftoilets; private int occupiedcount; public bathroom(int numberoftoilets, int occupiedcount) { this.numberoftoilets = numberoftoilets; this.occupiedcount = occupiedcount; }
main function
public static void main(string args[]) { thread[] women = new thread[3]; thread[] men = new thread[3]; int numberoftoilets = 3; int occupiedcount = 0; bathroom thebathroom = new bathroom(numberoftoilets, occupiedcount); (int = 0; < 3; i++) women[i] = new thread(new woman(i, thebathroom)); (int = 0; < 3; i++) men[i] = new thread(new man(i, thebathroom)); (int = 0; < 3; i++) women[i].start(); (int = 0; < 3; i++) men[i].start(); }
woman class
class woman implements runnable { private int n; /* identifies woman. */ private bathroom thebathroom; public woman(int n, bathroom b) { this.n = n; this.thebathroom = b; } public void run() { (int = 0; < 3; i++) { try { thread.sleep((long) (500 * math.random())); } catch (interruptedexception e) { } try { thebathroom.woman_wants_to_enter(n); } catch (interruptedexception e) { e.printstacktrace(); } system.out.println("woman " + n + " in bathroom "); try { thread.sleep((long) (500 * math.random())); } catch (interruptedexception e) { } try { thebathroom.woman_leaves(n); } catch (interruptedexception e) { e.printstacktrace(); } } }
} man class
class man implements runnable { private int n; /* identifies man */ private bathroom thebathroom; public man(int n, bathroom b) { this.n = n; this.thebathroom = b; } public void run() { (int = 0; < 3; i++) { try { thread.sleep((long) (500 * math.random())); } catch (interruptedexception e) { } try { thebathroom.man_wants_to_enter(n); } catch (interruptedexception e) { e.printstacktrace(); } system.out.println("man " + n + " in bathroom "); try { thread.sleep((long) (500 * math.random())); } catch (interruptedexception e) { } try { thebathroom.man_leaves(n); } catch (interruptedexception e) { e.printstacktrace(); } } }
}
your code shows men , women should not enter bathroom @ same time. seams work fine if persons of different gender want enter. try if it's working if several person of same gender want enter bathroom.
Comments
Post a Comment