java - when to use Thread, interrupt and join -
i doing sleeping barber prob. so, first create 10 customer threads 2 sec "sleep" between each other. now, first client gets hair cut, means sleeping 5 sec.. have waiting room following threads have place 3 threads.
if customer thread getting hair cut, , 3 following threads in queue, other customer threads created after must stop unless there available position in 3 sit waiting room.
when 1st customer gets cut, second customers cut starts,, 2 threads in queue, in case 1 more thread can join waiting.
to these task, can tell me in short, use sleep, interrupt , join. how make threads wait others finish, , how detect how many threads waiting/sleeping?
you can use additiv semaphores 3 places waiting room , 1 binary semaphore barber.
semaphore waitingroom = new semaphore(3); semaphore barber= new semaphore(1);
the method cut shoud this:
waitingroom.acquire(); barber.acquire(); waitingroom.release() thread.sleep(300) barber.release()
you use .join()
if waiting thread continue programm. don't need interrupt thread, must finish run()
.
.interrupt()
hold on thread.
Comments
Post a Comment