线程池:其实就是一个容纳多个线程的容器,其中的线程可以反复使用,省去了频繁创建线程对象的操作,无需反复创建线程而消耗过多的资源
如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了,这样频繁创建线程就会大大降低系统的效率,因为频繁创建线程和销毁线程需要时间线程池:JDK1.5之后提供的
java.util.concurrent.Executors是线程池的工厂类,用来生成线程池
Executors类中的静态方法:
static ExecutorService newFixedThreadPool(int nThreads)创建一个可重用固定线程数的线程池参数:int nThreads:创建线程池中的线程数量
返回值:ExecutorService接口返回的是ExecutorService接口的实现类对象,我们可以使用ExecutorService接口接收(面向接口编程)
java.util.concurrent.Executors:线程池接口 sumbit(Runnale task)用来从线程池中获取线程,调用start方法,执行线程任务 void shutdown() 关闭销毁线程池的方法线程池的使用步骤 1.使用线程池的工厂类Executors里边提供的静态方法newFixedThreadPool生产一个指定数量的线程池 2.创建一个类,实现Runnable接口,重写run方法,设置线程任务 3.调用ExecutorService中的方法sumbit,传递线程任务(实现类),开启线程,执行run方法 4.调用ExecutorService中的方法shutdown销毁线程池(不建议使用)package com.shopping.test;public class RunnableImplements implements Runnable{ //2. 创建一个类,实现Runnable接口,重写run方法,设置线程任务 @Override public void run() { System.out.println(Thread.currentThread().getName() + "创建一个新的线程"); }}
package com.shopping.test;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class ThreadPool { public static void main(String[] args) { //1. 使用线程池的工厂类Executors里边提供的静态方法`newFixedThreadPool`生产一个指定数量的线程池 ExecutorService es = Executors.newFixedThreadPool(2); //3. 调用`ExecutorService`中的方法`sumbit`,传递线程任务(实现类),开启线程,执行run方法 es.submit(new RunnableImplements()); //线程池会一直开启,除非调用shutdown方法 //使用完线程之后,会自动把线程归还给任务(实现类),线程可以继续使用 es.submit(new RunnableImplements()); es.submit(new RunnableImplements()); //4. 调用`ExecutorService`中的方法shutdown销毁线程池(不建议使用) es.shutdown(); }}
输出:
pool-1-thread-1创建一个新的线程
pool-1-thread-1创建一个新的线程
pool-1-thread-2创建一个新的线程
"C:\Program Files\jdk1.7.0_51\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.2.4\lib\idea_rt.jar=51850:C:\Program Files\JetBrains\IntelliJ IDEA 2018.2.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\jdk1.7.0_51\jre\lib\charsets.jar;C:\Program Files\jdk1.7.0_51\jre\lib\deploy.jar;C:\Program Files\jdk1.7.0_51\jre\lib\ext\access-bridge-64.jar;C:\Program Files\jdk1.7.0_51\jre\lib\ext\dnsns.jar;C:\Program Files\jdk1.7.0_51\jre\lib\ext\jaccess.jar;C:\Program Files\jdk1.7.0_51\jre\lib\ext\localedata.jar;C:\Program Files\jdk1.7.0_51\jre\lib\ext\sunec.jar;C:\Program Files\jdk1.7.0_51\jre\lib\ext\sunjce_provider.jar;C:\Program Files\jdk1.7.0_51\jre\lib\ext\sunmscapi.jar;C:\Program Files\jdk1.7.0_51\jre\lib\ext\zipfs.jar;C:\Program Files\jdk1.7.0_51\jre\lib\javaws.jar;C:\Program Files\jdk1.7.0_51\jre\lib\jce.jar;C:\Program Files\jdk1.7.0_51\jre\lib\jfr.jar;C:\Program Files\jdk1.7.0_51\jre\lib\jfxrt.jar;C:\Program Files\jdk1.7.0_51\jre\lib\jsse.jar;C:\Program Files\jdk1.7.0_51\jre\lib\management-agent.jar;C:\Program Files\jdk1.7.0_51\jre\lib\plugin.jar;C:\Program Files\jdk1.7.0_51\jre\lib\resources.jar;C:\Program Files\jdk1.7.0_51\jre\lib\rt.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\classes;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\jdom.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\json.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\jstl.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\mail.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\qrcode.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\spring.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\javacsv.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\json-lib.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\RXTXcomm.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\aspectjrt.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\cxf-2.4.2.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\htmllexer.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\junit-4.4.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\spring-tx.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\activation.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\gson-2.2.4.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\hibernate3.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\htmlparser.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\jta-1.0.1B.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\okio-1.6.0.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\spring-aop.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\spring-jms.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\spring-orm.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\spring-web.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\thumbelina.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\antlr-2.7.6.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\cglib-2.1.3.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\dom4j-1.6.1.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\jedis-2.4.2.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\jedis-2.5.2.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\jsoup-1.6.1.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\servlet-api.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\spring-core.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\spring-jdbc.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\spring-test.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\jcifs-1.3.17.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\log4j-1.2.15.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\neethi-3.0.1.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\okhttp-2.5.0.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\okhttp-3.2.0.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\sitecapturer.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\spring-beans.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\velocity-1.7.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\wsdl4j-1.6.2.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\easydbo-0.9.1.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\ezmorph-1.0.6.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\filterbuilder.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\imageinfo-1.9.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\javax.jms-1.1.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\spring-webmvc.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\xstream-1.3.1.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\httpcore-4.4.4.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\httpmime-4.5.2.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\mimepull-1.9.3.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\Qrcode_swetake.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\spring-context.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\xmlbeans-2.3.0.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\IKAnalyzer3.2.8.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\joda-time-2.9.9.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\json_simple-1.1.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\slf4j-api-1.6.1.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\slf4j-api-1.7.5.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\bcprov-jdk14-138.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\commons-io-1.3.2.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\commons-lang-2.5.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\commons-pool-1.3.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\httpclient-4.5.2.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\javassist-3.3.ga.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\jaxen-1.1-beta-4.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\jersey-guava-2.9.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\nutz-1.b.40-jdk5.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\quartz-all-1.6.1.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\velocity-1.7-dep.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\commons-codec-1.4.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\commons-codec-1.6.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\commons-lang3-3.1.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\commons-pool2-2.2.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\easyjf-jweb-0.9.0.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\ehcache-web-2.0.4.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\hamcrest-core-1.3.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\hk2-api-2.3.0-b05.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\jersey-client-2.9.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\jersey-common-2.9.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\jersey-server-2.9.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\lucene-core-3.5.0.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\poi-3.13-20150929.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\slf4j-jdk14-1.6.1.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\activemq-web-5.6.0.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\common-annotations.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\commons-dbcp-1.2.1.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\ehcache-core-2.4.6.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\hibernate-3.2.2.ga.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\jackson-core-2.3.2.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\jackson-core-2.5.0.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\jpush-client-3.2.7.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\xmlschema-core-2.0.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\activemq-core-5.6.0.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\activemq-jaas-5.6.0.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\commons-lang3-3.3.2.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\hk2-utils-2.3.0-b05.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\javassist-3.18.1-GA.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\javax.ws.rs-api-2.0.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\lucene-memory-3.5.0.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\okio-1.3.0-SNAPSHOT.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\rest-java-sdk-1.0.5.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\activemq-camel-5.6.0.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\activemq-pool-5.13.2.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\aspectjweaver-1.6.12.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\commons-digester-1.6.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\javax.servlet-5.1.12.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\lucene-queries-3.5.0.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\qiniu-java-sdk-7.0.7.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\activemq-protobuf-1.1.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\alipay-sdk-java-3.0.1.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\commons-logging-1.0.4.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\commons-logging-1.1.3.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\hibernate-annotations.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\hk2-locator-2.3.0-b05.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\activemq-console-5.6.0.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\jackson-databind-2.3.2.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\jackson-databind-2.5.0.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\javax.inject-2.3.0-b05.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\lucene-analyzers-3.5.0.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\urlrewritefilter-4.0.3.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\avalon-logkit-2.0.dev-0.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\commons-collections-1.2.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\hibernate-entitymanager.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\javax.persistence_2.0.0.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\mongo-java-driver-3.8.0.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\poi-ooxml-3.13-20150929.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\commons-fileupload-1.2.1.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\commons-httpclient-3.0.1.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\ehcache-terracotta-2.4.6.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\jackson-jaxrs-base-2.3.2.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\javax.annotation-api-1.2.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\lucene-highlighter-3.5.0.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\jackson-annotations-2.3.2.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\jackson-annotations-2.5.0.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\aliyun-java-sdk-core-3.2.3.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\aliyun-java-sdk-live-2.6.0.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\jersey-media-multipart-2.9.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\mysql-connector-java-5.0.5.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\spring-security-core-2.0.4.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\validation-api-1.1.0.Final.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\osgi-resource-locator-1.0.1.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\poi-ooxml-schemas-3.12-beta1.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\hibernate-commons-annotations.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\jersey-media-json-jackson-2.9.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\mysql-connector-java-5.1.37-bin.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\spring-data-redis-1.3.0.RELEASE.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\spring-data-redis-1.6.0.RELEASE.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\aopalliance-repackaged-2.3.0-b05.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\spring-security-core-tiger-2.0.4.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\jackson-jaxrs-json-provider-2.3.2.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\jackson-module-jaxb-annotations-2.3.2.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\aliyun-java-sdk-dysmsapi-1.0.0-SANPSHOT.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\geronimo-j2ee-management_1.1_spec-1.0.1.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\jboss-archive-browsing-5.0.0alpha-200607201-119.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\com.springsource.org.apache.commons.beanutils-1.7.0.jar;F:\workspacewyf\shopping_backup\WebRoot\WEB-INF\lib\com.springsource.org.apache.velocity.tools.view-2.0.0.beta3.jar" com.shopping.test.ThreadPoolpool-1-thread-1创建一个新的线程pool-1-thread-1创建一个新的线程pool-1-thread-2创建一个新的线程
Process finished with exit code 0