Package Util;public Abstract class Runnable implements java.lang.runnable{ private int priority; Public Runnable () { this.priority = 0; } public int getpriority () { return priority; } public void SetPriority (int.) { this.priority = priority; } public static void Main (string[] args) { String libname = System.maplibraryname ("");} }
import java.util.timer;import java.util.timertask;/** * observed, notifies the viewer every second of check task * * @ Author Guoxiaoming * */public class Timerobserver extends java.util.observable{public static Timerobserver instance = New Timerobserver (); public static Long now = System.currenttimemillis (); public static timer timer; Every second, the observer is notified to check whether his or her task can start executing public timerobserver () {timer = new timer ("Schedule-task"); Timer.schedule (New TimerTask () {public void run () {now = Scheduledexecutiont IME (); Instance.setchanged (); Instance.notifyobservers (); }}, 0, 1000); }}
Import Java.util.collections;import java.util.iterator;import Java.util.linkedlist;import Java.util.Map;import Java.util.observable;import Java.util.observer;import Java.util.set;import Java.util.sortedmap;import Java.util.treemap;import util. RUNNABLE;/** * The Observer object, which holds the task set, the observer notifies the observer every 1s clock that the task may indeed be added after the task may have been executed * * @author guoxiaoming * */public class Timert Ask implements observer{private class Taskpair {private Runnable Runnable; Private long waitseconds; Public Taskpair (Runnable Runnable, long waitseconds) {this.runnable = Runnable; This.waitseconds = Waitseconds; }} public long elapse = 0; public static Sortedmap<long, linkedlist<taskpair>> taskList = collections. Synchronizedsortedmap (n EW Treemap<long, linkedlist<taskpair>> ()); Public TimerTask () {TimerObserver.instance.addObserver (this); } @Override public synchronized void update (Observable o, Object Arg) {set<map.entry<long, linkedlist<taskpair>>> entryset = taskList. EntrySet (); Iterator<map.entry<long, linkedlist<taskpair>>> iter = EntrySet. Iterator (); elapse++; while (Iter.hasnext ()) {map.entry<long, linkedlist<taskpair>> Entry = Iter.next (); linkedlist<taskpair> linkedtask = Entry.getvalue (); iterator<taskpair> itertask = Linkedtask.iterator (); while (Itertask.hasnext ()) {Taskpair task = Itertask.next (); if (Task.waitseconds > Elapse) {break; } threadpool.addtask (task.runnable); Itertask.remove (); } if (linkedtask.size () = = 0)//Linkedtask.isempty () {iter.remove (); }}}//Actually two methods can be combined into one method for each LinkedListThe tasks are added later in front of public synchronized void AddTask (Runnable Runnable, long waitseconds) {Linkedlist<taskpai r> ll = Tasklist.get (waitseconds); if (LL = = null) {LL = new linkedlist<taskpair> (); Tasklist.put (WAITSECONDS,LL); } synchronized (LL) {///here should use AddLast instead of AddFirst ll.addlast (new Taskpair (runnable, WA Itseconds + elapse)); } }}
Import Java.util.collections;import java.util.iterator;import Java.util.linkedlist;import Java.util.Map;import Java.util.sortedmap;import java.util.treemap;import java.util.map.entry;import util. runnable;//thread pool public class ThreadPool implements java.lang.runnable{//Key priority value thread list private static sortedmap& Lt;integer, linkedlist<runnable>> tasks = Collections.synchronizedsortedmap (New Treemap<integer, Linkedlist<runnable>> ()); private int priority; private static int task_count; private static Object Task_count_locker = new Object (); private static long Time_lastadd; Records the number of tasks that each priority thread manages, and the management thread itself is a task private static Sortedmap<integer, integer> count = new Treemap<integer, Integ Er> (); Private ThreadPool (int priority) {this.priority = priority; Synchronized (count) {Integer c = count.get (priority); Count.put (priority, c = = null? 1:c.intvalue () + 1); }} @Override public void Run () {for (;;) {try {Runnable r = null; linkedlist<runnable> LL = Tasks.get (priority); Synchronized (LL) {if (Ll.isempty ()) {Ll.wai T (); } r = Ll.removelast (); Synchronized (task_count_locker) {task_count--; }} r.run (); } catch (Exception ex) {ex.printstacktrace (); }}}//Add new thread public static void Addthread (Int. priority) {if (Tasks.get (priority) = = NULL) {Tasks.put (priority, new linkedlist<runnable> ()); New Thread (New ThreadPool, "Thread---" + priority). Start (); }}//Add a new task static method can only use static variables in a non-static method to use static variables Public static void AddTask (Runnable Runnable) {int prior = runnable.getpriority (); linkedlist<runnable> LL = Tasks.get (prior); if (LL = = null) {System.out.println ("No match priority thread"); Return } synchronized (LL) {synchronized (task_count_locker) {task_count++; } ll.add (runnable);//actually addfirst or add difference is not very time_lastadd = System.currenttimemillis (); Ll.notify (); }}//Get a mapped copy public static Map<integer, integer> gettaskcounts () {Sortedmap<integer, Integ er> map = new Treemap<integer, integer> (); Synchronized (count) {for (iterator<integer> iter = Tasks.keyset (). Iterator (); Iter.hasnext ();) {Integer prior = Iter.next (); Map.put (Prior, tasks.get (prior). Size ()); }//or for (Entry<integer, linkedlist<runnable>> Iter:tasks.entrySet ()) {Map.put (Iter.getkey (), it Er.getvalue (). Size ()); }//or use cumbersome while loop//set<map.entry<integer,linkedlist<runnable>>>} return map; } public static Long Gettasktotal () {synchronized (Task_count_locker) {return task_count; } }}
import util. Runnable;public class maintest{public static void Main (string[] args) throws Interruptedexception {//Add 1-1 0 10 Priority management threads for (int i = 1; i <=; i++) {threadpool.addthread (i); } timertask TimerTask = new TimerTask (); for (int i = 1; i <=; i++) {Timertask.addtask (new Runnabletask (i), i); }} static Class Runnabletask extends Runnable {public runnabletask (int priority) {SUP Er.setpriority (priority); } @Override public void Run () {int begin = (super.getpriority ()-1) * 10; int end = Super.getpriority () * 10; for (int i = begin; I <= end; i++) {System.out.print (i + ""); } System.out.println (); } }}
java--thread Pool