Document directory
- Printthread. Java
- Appconfig. Java
- App. Java
- Output:
- Printtask. Java
- Spring-Config.xml
- App1.java
- Output:
- Printtask2.java
- Appconfig. Java
- App2.java
- Output:
Spring thread pool development practices
Author: chszs, reprinted with note.
Author blog homepage: http://blog.csdn.net/chszs
This article provides three examples of spring multi-threaded development, from simple to deep, because the examples are clear at a glance, so there is not much explanation. You can see it at a glance.
Prerequisites:
1) create a Java project in eclipse. I named it springthreaddemo.
2) jar packages required by the project:
Start.
Note: The project source code has been hosted on GitHub, address: https://github.com/chszs/SpringThreadDemo
Example 1: Spring is combined with Java threads.
Create a simple Java thread by inheriting the thread, and then use @ component to let the spring container manage this thread. The bean range must be prototype. Therefore, each request returns a new instance, run each separate thread.
Printthread. Java
package com.chszs.thread;import org.springframework.stereotype.Component;import org.springframework.context.annotation.Scope;@Component@Scope("prototype")public class PrintThread extends Thread{ @Override public void run(){ System.out.println(getName() + " is running."); try{ Thread.sleep(5000); }catch(InterruptedException e){ e.printStackTrace(); } System.out.println(getName() + " is running again."); }}
Appconfig. Java
package com.chszs.config;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScan(basePackages="com.chszs.thread")public class AppConfig {}
App. Java
package com.chszs;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import com.chszs.config.AppConfig;import com.chszs.thread.PrintThread;public class App { public static void main(String[] args){ ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class); PrintThread printThread1 = (PrintThread)ctx.getBean("printThread"); printThread1.setName("Thread 1"); PrintThread printThread2 = (PrintThread)ctx.getBean("printThread"); printThread2.setName("Thread 2"); PrintThread printThread3 = (PrintThread)ctx.getBean("printThread"); printThread3.setName("Thread 3"); PrintThread printThread4 = (PrintThread)ctx.getBean("printThread"); printThread4.setName("Thread 4"); PrintThread printThread5 = (PrintThread)ctx.getBean("printThread"); printThread5.setName("Thread 5"); printThread1.start(); printThread2.start(); printThread3.start(); printThread4.start(); printThread5.start(); }}
Output:
Thread 1 is running.
Thread 2 is running.
Thread 4 is running.
Thread 5 is running.
Thread 3 is running.
Thread 2 is running again.
Thread 1 is running again.
Thread 5 is running again.
Thread 4 is running again.
Thread 3 is running again.
Example 2: Spring thread pool is combined with non-spring managed bean.
Use spring's threadpooltaskexecutor class to create a thread pool. The execution thread does not need to be managed by the spring container.
Printtask. Java
package com.chszs.thread;public class PrintTask implements Runnable{ String name; public PrintTask(String name){ this.name = name; } @Override public void run() { System.out.println(name + " is running."); try{ Thread.sleep(5000); }catch(InterruptedException e){ e.printStackTrace(); } System.out.println(name + " is running again."); } }
Spring-Config.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <property name="corePoolSize" value="5" /> <property name="maxPoolSize" value="10" /> <property name="WaitForTasksToCompleteOnShutdown" value="true" /> </bean></beans>
Note the location of the spring configuration file ,:
App1.java
Package COM. chszs; import Org. springframework. context. applicationcontext; import Org. springframework. context. support. classpathxmlapplicationcontext; import Org. springframework. scheduling. concurrent. threadpooltaskexecutor; import COM. chszs. thread. printtask; public class app1 {public static void main (string [] ARGs) {applicationcontext CTX = new classpathxmlapplicationcontext ("Resources/Spring-Config.xml"); threadpooltaskexecutor taskexecutor = (threadpooltaskexecutor) CTX. getbean ("taskexecutor"); taskexecutor.exe cute (New printtask ("thread 1"); taskexecutor.exe cute (New printtask ("thread 2 ")); taskexecutor.exe cute (New printtask ("thread 3"); taskexecutor.exe cute (New printtask ("thread 4"); taskexecutor.exe cute (New printtask ("thread 5 ")); // check the active thread. If the number of active threads is 0, the thread pool for (;) {int COUNT = taskexecutor is closed. getactivecount (); system. out. println ("active threads:" + count); try {thread. sleep (1000);} catch (interruptedexception e) {e. printstacktrace ();} If (COUNT = 0) {taskexecutor. shutdown (); break ;}}}}
Output:
Thread 1 is running.
Thread 2 is running.
Thread 3 is running.
Thread 4 is running.
Active threads: 4
Thread 5 is running.
Active threads: 5
Active threads: 5
Active threads: 5
Active threads: 5
Active threads: 5
Thread 4 is running again.
Thread 2 is running again.
Thread 3 is running again.
Thread 1 is running again.
Thread 5 is running again.
Active threads: 0
Author: chszs, reprinted with note. Blog homepage: http://blog.csdn.net/chszs
Example 3: Spring thread pool is combined with spring-managed bean.
In this example, the threadpooltaskexecutor class is still used, and the @ component annotation is used to declare the managed bean of spring.
In the following example, printtask2 is a spring-managed bean, and the code is simplified using @ autowired annotations.
Printtask2.java
package com.chszs.thread;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Component;@Component@Scope("prototype")public class PrintTask2 implements Runnable { String name; public void setName(String name) { this.name = name; } @Override public void run(){ System.out.println(name + " is running."); try{ Thread.sleep(5000); }catch(InterruptedException e){ e.printStackTrace(); } System.out.println(name + " is running again."); }}
Appconfig. Java
package com.chszs.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;@Configuration@ComponentScan(basePackages="com.chszs.thread")public class AppConfig { @Bean public ThreadPoolTaskExecutor taskExecutor(){ ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor(); pool.setCorePoolSize(5); pool.setMaxPoolSize(10); pool.setWaitForTasksToCompleteOnShutdown(true); return pool; }}
App2.java
package com.chszs;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import com.chszs.config.AppConfig;import com.chszs.thread.PrintTask2;public class App2 { public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class); ThreadPoolTaskExecutor taskExecutor = (ThreadPoolTaskExecutor)ctx.getBean("taskExecutor"); PrintTask2 printTask1 = (PrintTask2)ctx.getBean("printTask2"); printTask1.setName("Thread 1"); taskExecutor.execute(printTask1); PrintTask2 printTask2 = (PrintTask2)ctx.getBean("printTask2"); printTask2.setName("Thread 2"); taskExecutor.execute(printTask2); PrintTask2 printTask3 = (PrintTask2)ctx.getBean("printTask2"); printTask3.setName("Thread 3"); taskExecutor.execute(printTask3); for(;;){ int count = taskExecutor.getActiveCount(); System.out.println("Active Threads : " + count); try{ Thread.sleep(1000); }catch(InterruptedException e){ e.printStackTrace(); } if(count==0){ taskExecutor.shutdown(); break; } } }}
Output:
Thread 1 is running.
Thread 2 is running.
Active threads: 2
Thread 3 is running.
Active threads: 3
Active threads: 3
Active threads: 3
Active threads: 3
Thread 1 is running again.
Thread 2 is running again.
Thread 3 is running again.
Active threads: 1
Active threads: 0
From these three simple examples, have you discovered the strong multithreading of the Spring framework !!