Common thread pool Instance code _java in Java

Source: Internet
Author: User

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 () &gt; 0)


{


Tasks.clear ();


System.out.println (Thread.CurrentThread (). GetId () + ": Tasks are stopped");


Break


}


}


if (paused)


{


paused = false;


if (tasks.size () &gt; 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 &gt; getpoolsize ())


{


for (int i = getpoolsize (); I &lt; size &amp;&amp; i &lt; maxpoolsize; i++)


{


Taskmanage thread = new Taskmanage (this);


Thread.Start ();


Threads.add (thread);


}


}


else if (Size &lt; getpoolsize ())


{


while (Getpoolsize () &gt; 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 &amp;&amp; getpoolsize () &gt;= 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 () + ".... ..."); ".";
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.