Threads-Multithreaded thread pool

Source: Internet
Author: User

Recently in-depth knowledge of multithreading, about multi-threading, we commonly used in the development of the thread pool to deal with a variety of business logic

For example, a large set of objects, want to quickly insert into the database, we can iterate, or we can use the thread pool in the same way to insert

Or a calling interface, each of which calls an interface over and over again, or you can use multithreading to invoke an interface

The following code can handle this problem

Package Com.demo;

Import java.util.LinkedList;

Import java.util.List;

Import Java.util.concurrent.ExecutorService;

Import java.util.concurrent.Executors;

Import Java.util.concurrent.LinkedBlockingQueue;

Import Java.util.concurrent.ThreadPoolExecutor;

Import Java.util.concurrent.TimeUnit;

public class ThreadPool2 {

public static void Main (string[] args) {


* Number of thread pools


int corepoolsize = 10;


* Maximum number of thread pools


int maximumpoolsize = 100;

Executorservice ThreadPool = new Threadpoolexecutor (corepoolsize,maximumpoolsize, timeunit.seconds,new Linkedblockingqueue<runnable> ());

Executorservice ThreadPool =executors.newfixedthreadpool (4);

String dao = "DAO";

list<string> list = new linkedlist<string> ();

for (int i = 0; i < i++) {

List.add (i + "");


Task task = null;

for (String str:list) {

task = new Task (DAO, str);

Threadpool.execute (Task);


Threadpool.shutdown ();



Class Task implements Runnable {


* Inject DAO


Private String DAO;


* Inject Beans


Private String Bean;

Public Task (String dao, string bean) {

This.dao = DAO;

This.bean = Bean;


public void Run () {

try {

TimeUnit.SECONDS.sleep (1);

SYSTEM.OUT.PRINTLN ("Thread Name:" +thread.currentthread () + "--thread activation number-" +thread.activecount () + "---");

SYSTEM.OUT.PRINTLN (dao + "--insert into the database---" + bean);

} catch (Exception e) {

E.printstacktrace ();




Threads-Multithreaded thread pool

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: 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.