Simple implementation and priority setting of JAVA Thread Pool

Source: Internet
Author: User

As we all know, when processing multi-threaded service concurrency, creating a thread requires a lot of system resources. To avoid unnecessary losses, we usually use a thread pool to solve these problems.
The basic principle of the thread pool is to first create and maintain a certain number of threads. When a thread needs to be used, we obtain the thread from the pool and then hand over the task to the thread for processing, after the task is completed, release it back to the pool.
Below, I will provide a very simple implementation model for your reference only.

ThreadPool. java package org. loon. framework. util. test;

Import java. util. Collections list;
Import java. util. List;

/***//**
* <P>
* Title: LoonFramework
* </P>
* <P>
* Description:
* </P>
* <P>
* Copyright: Copyright (c) 2007
* </P>
* <P>
* Company: LoonFramework
* </P>
*
* @ Author chenpeng
* @ Email: ceponline@yahoo.com.cn
* @ Version 0.1
*/
Public class ThreadPool ...{

Private static ThreadPool instance = null;

// Low priority
Public static final int PRIORITY_LOW = 0;

// Normal
Public static final int PRIORITY_NORMAL = 1;

// High
Public static final int PRIORITY_HIGH = 2;

// Used to save idle connections
Private List [] _ idxThreads;

// Close
Private boolean _ shutdown = false;

// Number of threads
Private int _ threadCount = 0;

// Whether the debug information is output
Private boolean _ debug = false;

/***//**
* Returns the ThreadPool instance.
*
* @ Return
*/
Public static ThreadPool getInstance ()...{
If (instance = null )...{
Instance = new ThreadPool ();
}
Return instance;
}

// Initialize the thread list
Private ThreadPool ()...{
This. _ idxThreads = new List []... {new queue List (), new queue List (),
New partition list ()};
This. _ threadCount = 0;
}

/***//**
* Synchronization method. After the task is completed, the resource is put back into the thread pool.
* @ Param repool
*/
Protected synchronized void repool (Pooled repool )...{
If (this. _ shutdown )...{
If (this. _ debug )...{
System. out. println ("ThreadPool. repool (): Resetting ...... ");
}
// Priority Determination
Switch (repool. getPriority ())...{
Case Thread. MIN_PRIORITY:
This. _ idxThreads [PRIORITY_LOW]. add (repool );
Break;
Case Thread. NORM_PRIORITY:
This. _ idxThreads [PRIORITY_NORMAL]. add (repool );
Break;
Case Thread. MAX_PRIORITY:
This. _ idxThreads [PRIORITY_HIGH]. add (repool );
Break;
Default:
Throw new IllegalStateException ("No such level ");
}
// Notify all threads
Policyall ();

} Else ...{
If (this. _ debug )...{
System. out. println ("ThreadPool. repool (): canceling ...... ");
}
Repool. shutDown ();
}
If (this. _ debug )...{
System. out. println ("ThreadPool. repool (): Done ");
}
}
Public void setDebug (boolean debug )...{
This. _ debug = debug;
}
Public synchronized void shutDown ()...{
This. _ shutdown = true;
If (this. _ debug )...{
System. out. println ("ThreadPool. shutDown (): close ...... ");
}
For (int index = 0; index <= PRIORITY_NORMAL; index ++ )...{
List threads = this. _ idxThreads [index];
For (int threadIndex = 0; threadIndex <threads. size (); threadIndex ++ )...{
Pooled idleThread = (Pooled) threads. get (threadIndex );
IdleThread. shutDown ();
}
}
Policyall ();
}

/***//**
* Start a thread with a specified priority.
* @ Param target
* @ Param priority
*/
Public synchronized void start (Runnable target, int priority )...{
Pooled thread = null;
List idleList = this. _ idxThreads [priority];
Int idleSize = idleList. size ();

If (idleSize> 0 )...{
Int lastIndex = idleSize-1;
& N

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.