Copy Code code as follows:
Package com.smart.frame.task.autoTask;
Import java.util.Collection;
Import Java.util.Vector;
/**
* Task Distributor
*/
public class Taskmanage extends Thread
{
protected vector<runnable> tasks = new vector<runnable> ();
Protected Boolean running = false;
Protected Boolean stopped = false;
Protected Boolean paused = false;
Protected Boolean killed = false;
Private ThreadPool pool;
Public Taskmanage (ThreadPool pool)
{
This.pool = pool;
}
public void Puttask (Runnable Task)
{
Tasks.add (Task);
}
public void Puttasks (runnable[] tasks)
{
for (int i = 0; i < tasks.length; i++)
This.tasks.add (Tasks[i]);
}
public void Puttasks (collection<runnable> tasks)
{
This.tasks.addAll (tasks);
}
Protected Runnable Poptask ()
{
if (tasks.size () > 0) return (Runnable) tasks.remove (0);
else return null;
}
public boolean isrunning ()
{
return running;
}
public void Stoptasks ()
{
stopped = true;
}
public void Stoptaskssync ()
{
Stoptasks ();
while (IsRunning ())
{
Try
{
Sleep (5);
}
catch (Interruptedexception e)
{
Taskexception.getresultmessage (e);
}
}
}
public void Pausetasks ()
{
paused = true;
}
public void Pausetaskssync ()
{
Pausetasks ();
while (IsRunning ())
{
Try
{
Sleep (5);
}
catch (Interruptedexception e)
{
Taskexception.getresultmessage (e);
}
}
}
public void Kill ()
{
if (!running) interrupt ();
else killed = true;
}
public void Killsync ()
{
Kill ();
while (IsAlive ())
{
Try
{
Sleep (5);
}
catch (Interruptedexception e)
{
Taskexception.getresultmessage (e);
}
}
}
Public synchronized void Starttasks ()
{
running = true;
This.notify ();
}
Public synchronized void Run ()
{
Try
{
while (true)
{
if (!running | | tasks.size () = 0)
{
Pool.notifyforidlethread ();
This.wait ();
}
Else
{
Runnable task;
while (task = Poptask ())!= null)
{
Task.run ();
if (Stopped)
{
stopped = false;
if (tasks.size () > 0)
{
Tasks.clear ();
System.out.println (Thread.CurrentThread (). GetId () + ": Tasks are stopped");
Break
}
}
if (paused)
{
paused = false;
if (tasks.size () > 0)
{
System.out.println (Thread.CurrentThread (). GetId () + ": Tasks are paused");
Break
}
}
}
running = false;
}
if (killed)
{
killed = false;
Break
}
}
}
catch (Interruptedexception e)
{
Taskexception.getresultmessage (e);
Return
}
}
}
Copy Code code as follows:
Package com.smart.frame.task.autoTask;
Import java.util.Collection;
Import Java.util.Iterator;
Import Java.util.Vector;
/**
* Thread pool
*/
public class ThreadPool
{
protected int maxpoolsize = Taskconfig.maxpoolsize;
protected int initpoolsize = Taskconfig.initpoolsize;
protected vector<taskmanage> threads = new vector<taskmanage> ();
Protected Boolean initialized = false;
protected Boolean hasidlethread = false;
Public ThreadPool ()
{
Super ();
}
Public ThreadPool (int maxpoolsize, int initpoolsize)
{
This.maxpoolsize = maxpoolsize;
This.initpoolsize = initpoolsize;
}
public void Init ()
{
initialized = true;
for (int i = 0; i < initpoolsize; i++)
{
Taskmanage thread = new Taskmanage (this);
Thread.Start ();
Threads.add (thread);
}
}
public void setmaxpoolsize (int maxpoolsize)
{
This.maxpoolsize = maxpoolsize;
if (Maxpoolsize < Getpoolsize ()) setpoolsize (maxpoolsize);
}
/**
* Reset the current number of threads if you need to kill a thread, the thread will not kill it immediately, but wait until something in the thread
* Transaction complete But this method will immediately remove the thread from the thread pool and will not wait for the end of the process
*/
public void setpoolsize (int size)
{
if (!initialized)
{
initpoolsize = size;
Return
}
else if (Size > getpoolsize ())
{
for (int i = getpoolsize (); I < size && i < maxpoolsize; i++)
{
Taskmanage thread = new Taskmanage (this);
Thread.Start ();
Threads.add (thread);
}
}
else if (Size < getpoolsize ())
{
while (Getpoolsize () > Size)
{
Taskmanage th = (taskmanage) threads.remove (0);
Th.kill ();
}
}
}
public int getpoolsize ()
{
return Threads.size ();
}
protected void Notifyforidlethread ()
{
Hasidlethread = true;
}
Protected Boolean waitforidlethread ()
{
Hasidlethread = false;
while (!hasidlethread && getpoolsize () >= maxpoolsize)
{
Try
{
Thread.Sleep (5);
}
catch (Interruptedexception e)
{
Taskexception.getresultmessage (e);
return false;
}
}
return true;
}
public synchronized Taskmanage getidlethread ()
{
while (true)
{
for (iterator<taskmanage> ITR = Threads.iterator (); Itr.hasnext ();)
{
taskmanage th = (taskmanage) itr.next ();
if (!th.isrunning ()) return th;
}
if (Getpoolsize () < maxpoolsize)
{
Taskmanage thread = new Taskmanage (this);
Thread.Start ();
Threads.add (thread);
return thread;
}
if (waitforidlethread () = = false) return null;
}
}
public void Processtask (Runnable Task)
{
taskmanage th = Getidlethread ();
if (th!= null)
{
Th.puttask (Task);
th.starttasks ();
}
}
public void Processtasksinsinglethread (runnable[] tasks)
{
taskmanage th = Getidlethread ();
if (th!= null)
{
Th.puttasks (tasks);
th.starttasks ();
}
}
public void Processtasksinsinglethread (collection<runnable> tasks)
{
Taskmanage th = Getidlethread ();
if (th!= null)
{
Th.puttasks (tasks);
Th.starttasks ();
}
}
}
Copy Code code as follows:
Package com.smart.frame.task.autoTask;
public class Toptask implements Runnable
{
Private ThreadPool pool;
Public Toptask ()
{
Super ();
}
Public Toptask (ThreadPool pool)
{
Super ();
This.pool = pool;
}
@Override
public void Run ()
{
Init ();
Start ();
}
/**
* Initialize validation permissions, parameters, and so on
*/
public void Init ()
{
}
/**
* Start Automatic task
*/
public void Start ()
{
for (int i = 0; i < i++)
{
Pool.processtask (New Beginauto ());
}
}
}
/**
* Implementation Class
*/
Class Beginauto implements Runnable
{
@Override
public void Run ()
{
System.out.println (Thread.CurrentThread (). GetId () + ".... ..."); ".";
}
}