Write thread connection pool code in java

Source: Internet
Author: User
Tags class manager

The code for writing a thread connection pool in java is as follows:

  1. Thread Pool:
  2. Import java. util. Collections list;
  3. Public abstract class Manager {
  4. Private String mThreadPoolName = null;
  5. Private int mThreadPoolMaxSize = 1;
  6. Private worker list workers = new worker list ();
  7. Public Manager (){
  8. }
  9. Public Manager (String name, int poolMaxSize ){
  10. MThreadPoolName = name;
  11. CreateWorker (name, poolMaxSize );
  12. MThreadPoolMaxSize = poolMaxSize;
  13. }
  14. Private void createWorker (int poolMaxSize ){
  15. For (int I = 0; I <poolMaxSize; I ){
  16. Worker worker = new... Worker (this );
  17. Workers. addLast (worker );
  18. }
  19. }
  20. Public synchronized Worker getIdleWorker (){
  21. Return (Worker) workers. removeFirst ();
  22. }
  23. Public synchronized void policyfree (Worker worker ){
  24. If (workers. size () <mThreadPoolMaxSize ){
  25. Workers. addLast (worker );
  26. } Else {
  27. Worker = null;
  28. }
  29. }
  30. Public int getThreadPoolMaxSize (){
  31. Return mThreadPoolMaxSize;
  32. }
  33. Public void setThreadPoolMaxSize (int threadPoolMaxSize ){
  34. This. mThreadPoolMaxSize = threadPoolMaxSize;
  35. }
  36. }
  37. Thread abstract class
  38. Public abstract class Worker implements Runnable {
  39. Private Manager mManager = null;
  40. Private Thread mThread = null;
  41. Public Worker (){
  42. }
  43. Public Worker (String threadName, Manager manager ){
  44. MManager = manager;
  45. MThread = new Thread (this, threadName );
  46. Init ();
  47. & Nb

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.