Common java thread pool instance code

Source: Internet
Author: User

Copy codeThe Code is 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. Policy ();
}

Public synchronized void run ()
{
Try
{
While (true)
{
If (! Running | tasks. size () = 0)
{
Pool. policyforidlethread ();
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 codeThe Code is 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 );
}

/**
* Resetting the current number of threads if a thread needs to be killed, the thread will not be killed immediately, but will wait until something in the thread
* After the transaction processing is completed, this method immediately removes the thread from the thread pool and does not wait until the transaction processing ends.
*/
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 policyforidlethread ()
{
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 codeThe Code is 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 verification permissions, parameters, and the like.
*/
Public void init ()
{

}

/**
* Start an automatic task
*/
Public void start ()
{
For (int I = 0; I <10; 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.