Tag:java thread pool
Package com.letv.lazybox.task;/** A number of participants in the race race, each group has a different length of the whole number of meters, the total duration of the game, if the athletes do not complete the race in the course of the exit */import Java.text.simpledateformat;import Java.util.date;import Java.util.iterator;import Java.util.concurrent.concurrenthashmap;import Java.util.concurrent.executors;import Java.util.concurrent.scheduledexecutorservice;import Java.util.concurrent.timeunit;public class TaskContraller { public static void Main (string[] args) {//Initialize concurrenthashmap<string, walkingtask> walktaskmap = NE W concurrenthashmap<string, walkingtask> (); Player Tom = new player ("Tom", 1000, 2000, 20); Player Kitty = new player ("Kitty", 800, 3000, 10); Walktaskmap.put ("Tom", New Walkingtask (Tom)); Walktaskmap.put ("Kitty", New Walkingtask (Kitty)); System.out.println (New SimpleDateFormat ("HH:mm:ss"). Format (New Date (System.currenttimemillis ())); Defines a thread pool with a capacity of 50 threads final scheduledexecutorservice pool = executors.Newscheduledthreadpool (50); iterator<string> ite = Walktaskmap.keyset (). Iterator (); while (Ite.hasnext ()) {String name = Ite.next (); Walkingtask walkingtask = walktaskmap.get (name); SYSTEM.OUT.PRINTLN (name + "is ready to walking...!"); Perform the task immediately and start the race pool.execute (Walkingtask); Delay to perform the task, stop the game stoptask stoptask = new Stoptask (walktaskmap, name); Pool.schedule (Stoptask, Walkingtask.getplayer (). Gettotalseconds (), timeunit.seconds); } System.out.println ("Main thread ...."); }}/** walking Task class */class Walkingtask implements Runnable {Private Boolean isstop = false; Whether the game stops private player player;//athlete Information public boolean isstop () {return this.isstop; The public void Setstop (Boolean isstop) {this.isstop = Isstop; Public Player Getplayer () {return this.player; } public void Setplayer (player player) {This.player = player; } public Walkingtask (player player) {//TODO auto-generated constructor stub this.player = player; } @Override public void Run () {for (int i = 0; I <= this.player.getMeters (); i++) {if (this.i Sstop) {break; } System.out.println (This.player.getName () + "run:" + i + "m!"); try {thread.sleep (This.player.getMillsecond ()); } catch (Interruptedexception e) {e.printstacktrace (); }}}}/** abort match task class */class Stoptask implements Runnable {private final String key;//suspended athlete name private FINA L concurrenthashmap<string, walkingtask> MyTasks; Public Stoptask (concurrenthashmap<string, walkingtask> mytasks, String name) {this.mytasks = MyTasks; This.key = name; } @Override public void Run () {this.mytasks.get (This.key). Setstop (True); String endTime = new SimpleDateFormat ("HH:mm:ss"). Format (New Date (System.currenttimemillis ())); System.out.println ("[" + This.key + "] GAME over! EndTime = "+ EndTime); }}/** athlete Information class */class player {private String name;//name private int meters;//participant mileage private int millsecond;//The athlete averaged every run one M time private int totalseconds;//general game public Player (String name, int meters, int millsecond, int totalseconds) { THIS.name = name; This.meters = meters; This.millsecond = Millsecond; This.totalseconds = totalseconds; } public String GetName () {return this.name; } public void SetName (String name) {this.name = name; } public int getmeters () {return this.meters; } public void Setmeters (int meters) {this.meters = meters; } public int Getmillsecond () {return this.millsecond; } public void Setmillsecond (int millsecond) {this.millsecond = Millsecond; } public int GettotalsEconds () {return this.totalseconds; } public void Settotalseconds (int totalseconds) {this.totalseconds = totalseconds; }}
Write a deferred task using the Java.util.concurrent package