Consider there are 2 queues in a temple A and B. For the first ten minutes, A queue will be allowed inside the temple and for the second 10mins, B queue will be allowed. Only one queue should be allowed at any given time till both queues are empty.
If we need to implement the above using threads in java, How to make sure that both the queues are not open at a given time ?
I tried scheduledAtFixedRate and scheduledAtFixedDelay using ScheduledExecutor service and I don't get to control that the thread should be active for 10mins as the above methods only allows us to control the time intreval in which the thread should be active.
I need to know how we can specify that 2 threads cannot run parallelly and should run for a given amount of time.
I'm going to assume that this is a homework assignment, and you are required to use two threads.*
Since it's homework, I'm not going to just answer it for you. But here's a hint. If you want to have two (or more) threads take turns doing something, then you need some way for each thread to notify the next thread, "It's your turn now."
Each of your two threads can loop:
I'll leave it as an exercise for you to figure out how one thread can send an "It's your turn" message to another and, how the second thread can wait to receive that message. Depending on what programming language, libraries and/or framework you are using, there's probably several reasonable ways to do that.
* See Jeremy Friesner's comment for a concise explanation of why using two threads would be a pointless thing to do in any "real" program.