My thread pool code

xiaoxiao2021-03-06  42

(1) Managing the maximum minimum number of thread pools according to the XML file (2) to the thread pool to prevent the thread until the thread is not activated; (3) Get free thread number;

1. Configure XML (Listen.xml) Yes:

?

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

???????

10

?????

???????????

100

???????

???????????

5

?

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

?

Second, for the JavaBean of ConsumeThreadPoolpara:

Import java.io. *; public class consumethreadPoolpara imports serializable {? private int minpools ;? private int carethread;

? Public int getMinPools () {??? return minPools ;?}? Public int getMaxPools () {??? return maxPools ;?}? Public int getCheckThreadPeriod () {??? return checkThreadPeriod ;?}? Public void setMinPools ( int minPools) {??? this.minPools = minPools ;?}? public void setMaxPools (int maxPools) {??? this.maxPools = maxPools ;?}? public void setCheckThreadPeriod (int checkThreadPeriod) {??? this.checkThreadPeriod = checkThreadPeriod ;?}? public String toString () {??? return minPools "" maxPools "" checkThreadPeriod ;?}? public ConsumeThreadPoolPara () {?}? public static void main (String [] args) {?? ? Consumethreadpoolparapthreadpool1 = new consumethreadpoolpara () ;?}

}

Third, parse the XML program code (Generate ConsumeThreadPoolpara): Use the JDOM parsing: import org.jdom. *; Import org.jdom.Input.saxbuilder; import java.io. *; Import java.util. *;

public class ParseConfig {static Hashtable Listens = null ;? static ConnPara connpara = null ;? static ConsumeThreadPoolPara consumeThreadPoolPara = null ;? private static String configxml = "listen.xml"?;

? static {??? getconsumethreadpoolpara ();? // Get the parameter of consumer thread pool?}

? / ** ?? * Load document ?? * @return returned to root knots ?? * @Throws jdomException ?? * /? Public static element loadDocument () throws jdomexception {??? Saxbuilder Parser = new Saxbuilder (); / / New builder ??? Try {????? Document Document = PARSER.BUILD (ConfigXML); ????? Element Root = Document.getrootElement (); ????? Return root; ??? } catch (jdomException e) {????? logger.error ("listen.xml file format illegally!"); ????? throw new jdomexception (); ???}?}? public static consumethreadpoolpara getConsumeThreadPoolpara ( ) {??? f (consumethreadpoolpara == null) {?????? Element root = loadDocument (); ??????? Element consumeThreadPool = root.getchild ("consumethreadpool "); ??????? IF (consumethreadpool! = null) {// representative database configuration ????????? consumethreadPoolpara = new consumethreadpoolpara (); ????????? ELEMENT minPools = consumeThreadPool.getChild ( "minPools"); ????????? consumeThreadPoolPara.setMinPools (Integer.parseInt (minPools.getTextTrim ())); ????????? Element maxPools = consumeThreadPool. GetChild ("maxpools"); ????????? consumethreadpoolpara.setma xPools (Integer.parseInt (maxPools.getTextTrim ())); ????????? Element checkThreadPeriod = consumeThreadPool.getChild ( "checkThreadPeriod"); ????????? consumeThreadPoolPara.setCheckThreadPeriod (Integer. PARSEINT (CheckthReadperiod.GettextTrim ())); ???????} ?????} ????? Catch (JDOMEXCEPTION E) {?????} ???} ??? Return ConsumeThreadPoolpara ;?}}

Fourth, thread pool source code: Import java.util. *;

/ **? *

Title: Thread pool

? *

Description: Acquisition Consumer Module

? *

Copyright: CopyRight (C) 2004

? *

COMPANY:

? * @Author Zhang Rongbin

? * @version 1.0

? * /

public class ThreadPool {private static int minPools = 10;? // minimum number of connection pool private static int maxPools = 100;?? // maximum number of connection pool private static int checkThreadPeriod = 5; // check the connection cell cycle ArrayList m_ThreadList? ; // Work thread list? LinkedList m_runlist = null ;? // Working task list? INT TOTALTHREAD = 0 ;? // Tripgsal number? Static int freetreadcount = 0 ;? // Unused thread number? Private Java .util.Timer timer = null ;? // timer static Object o = new Object () ;? static {// first initialize the thread pool parameters ??? ConsumeThreadPoolPara consumeThreadPoolPara = ParseConfig.getConsumeThreadPoolPara ();?? ??? if (consumeThreadPoolPara = null!) {????? minPools = consumeThreadPoolPara.getMinPools (); ????? maxPools = consumeThreadPoolPara.getMaxPools (); ????? checkThreadPeriod = consumeThreadPoolPara.getCheckThreadPeriod () * 60 * 1000 ; ???}?}? public void setMinPools (int minPools) {??? this.minpools = minpools; @} (int maxPools) {??? this.maxpools = maxpools;?}? public void SetCheckthreadPeriod (int checkthreadperiod) {??? this.checkthreadperiod = CheckthreadPeriod;?}? Publi c threadpool () {

??? m_threadlist = new arraylist (); ??? m_runlist = new linkedList (); ??? for (int i = 0; i

??????? workerthread temp = new workerthread ();

??????? TotalthRead = TotalthRead 1;

??????? m_threadlist.add (temp);

??????? Temp.start ();

??????? try {

????????? thread.sleep (100);

???????} catch (Exception E) {

???????}

???}

??? Timer = new Timer (TRUE) ;? // Start Timer

??? Timer.Schedule (New CheckthReadtask (this), 0, CheckthreadPeriod;

?

? / ** ?? * When there is a work that starts the thread pool thread ?? * 1. When the idle thread is 0, see if the total thread is less than the maximum number of thread pools, a new thread Otherwise, Sleep until there is a free thread; ?? * 2. When the idle thread is not 0, then throw the task to the idle thread to complete ?? * @Param Work ?? * /? Public synchronized void Run (String Work) ? {????????? f (freethreadcount == 0) {??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ???????? workerthread temp = new workerthread ();

??????????????????? TOTALTHREAD = TotalthRead 1;

??????????????????? m_threadlist.add (TEMP);

??????????????????? Temp.start ();

?????????????????? synchronized (m_runlist) {

????????????????????? M_Runlist.Add (Work);

?????????????????????? M_Runlist.Notify ();

???????????????????}

???????????????????????????????????????????????????????????????????????????????????????????} else {

??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? {

????????????????????? try {

??????????????????????? thread.sleep (200);

?????????????????????}

????????????????????? Catch (InterruptedException E) {

?????????????????????}

???????????????????}

?????????????????? synchronized (m_runlist) {

????????????????????? M_Runlist.Add (Work);

?????????????????????? M_Runlist.Notify ();

???????????????????}

??????????????????}

?????????} else {

??????????? synchronized (m_runlist) {

????????????? M_Runlist.Add (Work);

????????????? m_runlist.notify ();

???????????}

?????????}

?

? / ** ?? * Check all the thread validity ?? * /? Public synchronized void checkAllthreads () {

??? Iterator lthreadItem = m_threadlist.iterator ();

??? while (LTHREADITERATOR.hasnext ()) {// is traversed one by one ????? Workerthread LTestthread = (Workerthread) LthreadItemrator.next ();

????? if (! (ltestthread.isalive ())))) {// If you are in an inactive state ??????? ltestthread = new workerthread (); // Re-generate a thread ???? ??????????????????} ???}?}

? / ** ?? * Print debugging information ?? * /? Public void printdebuginfo () {??????? system.out.println ("TOTALTHREAD =" TOTALTHREAD); ??????? system .out.println ("m_threadlist.size () =" m_threadlist.size ());} ????? / ** ?????? * ?????? *

Title: Work thread class

?????? * @Author Zhang Rongbin

?????? * @version 1.0

?????? * /

Class workerthread extends thread {

?????????? boolean running = true;

?????????? String Work;

??????????? public void run () {????????????? while (Running) {?????????????? Synchronized (o) {?????????????????? FreethreadCount ; ?????????????????? ????? synchronized (m_runlist) {???????????????????????????????????????????????????????????????????????????????? ??????????? try {???????????????????????? m_runlist.wait (); ??????? ??????????????????????????????????????????????????????????????????????????????} Catch (InterruptedException) e) {????????????????????????????????} ?????? ??????????? synchronized (o) {???????????????????frethreadCount -; ??????????? ??????} ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ?? IF (work == null) return; ???????????????}

?? // Get working at Work, here Work can be replaced with your own work class ?????????????????}}

} ??????? / ** ???????? * ???????? *

Title: Timer mobilized task

???????? * @Author Zhang Rongbin

???????? * @version 1.0

???????? * /

??????? class checkthreadtask extends Timertask {

??????????????????priVate static boolean isrunning = false;

?????????priVate threadpool pool;

????????? public checkthreadtask (threadpool pool) {??????????? this.pool = pool; ?????????} ??????? ?? public void run () {??????????? if (! isrunning)? {????????????? isrunning = true; ??????? ?????? pool.checkAllthreads (); ????????????? isrunning = false; ???????????} ???????? } ???????}

转载请注明原文地址:https://www.9cbs.com/read-118548.html

New Post(0)