0
votes

I wonder if I can block thread and do something during blocking or just count time while thread's blocked, because I want to do queue for airplanes. My airports must have only one landing place, so I used queue with only one slot, then use put method:

world.getCities().get(cityID).getQueue().put(request);

Then I want to burn some fuel during landing:

Thread.sleep(500);
fuelTank = fuelTank - 100;

Now airplane can easly land so I use method take():

world.getCities().get(cityID).getQueue().take();

When everything's done next plane can start his landing procedure.

But airplane was waiting 0,5s so his fuel haven't changed and it'll burn the same amount of fuel as this airplane before and this second shoud burn much more.

So again. I need some kind of mechanism counting blocked thread time or do something during blocking.

Thanks in advance.

1

1 Answers

0
votes

you can use the wait()-notify() methods instead of sleep, they are neater. To count the amount of time for a blocked thread, spawn off a new thread from the thread about to be blocked by starting that thread just before the wait() method. Then halt that thread after immediate line of wait() method.

It should like roughly like this:

if(some_Condition_true) {
  //start counting thread
  wait();
  //halt counting thread
 }