multithreading - Java Resource-Game Simulation -


i'm project of resource-game simulation. have is...

"if there enough resources available, request satisfied, , requested quantities subtracted available quantities. if there not enough resources, animat consumes available quantities , wait more resources available."

the question how make possible... reduce resource , hold somewhere still needed , reduce when there available resources(like loan)...

example:

... 100 gold needed.... ... 50 gold available...

... reduce gold 50... , wait untill gold>=50 ,

... reduce gold 50...

etc...

sample code...

    public void feedarmy(){     if(food>=100){         food=food-100;         system.out.println("*feed soldiers  (-100 food)");         system.out.println(tostring());     }     else{         system.out.println("*feed soldiers  (-100 food)");         system.out.println("-not enough food!"); //get loan instead     } } 

(after edit) best solution now:

actually ... thought of reducing wanted value wood , if number goes negative keep negative value turn positive with

math.abs();

so if wood 30 , want 100... 30-100=-70; loan=-70; ...

then math.abs(loan); loan=70;

and if(wood>loan){ wood=wood-loan} //i might need put sleep untill wood refreshed again... , thats it... still have no idea producer/consumer stuff...

so solution looking known , studied in computer science. problem called producer consumer problem. search , find numerous examples , code solve problem.

here stackoverflow question it, producer/consumer threads using queue.

wikipedia page explaining problem in detail.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -