Spring管理线程

第一种方式:线程对象和线程对象里持有的对象引用都交给Spring管理。

线程定义:

public class MyThread1 extends Thread {
	private User user;

	public void run() {
		for (int i = 0; i < 10; i++) {
			System.out.println("i=" + i + "---->" + user + "--->" + this.getName());
		}
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
}

配置文件内容:

        <bean id="thread" class="com.ec.spring.ioc.thread.MyThread1" <span style="color:#FF0000;">scope="prototype"</span> >
		<property name="user" ref="user"/>
	</bean>
	<bean id="user" class="com.ec.spring.ioc.thread.User" scope="prototype">
	</bean>
测试代码:

public class ThreadTestOne {
	private DefaultListableBeanFactory factory;
	
	@Before
	public void init(){
		factory = new DefaultListableBeanFactory();
		XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
		reader.loadBeanDefinitions("applicationContext-ioc.xml");
	}
	
	@Test
	public void testThread(){
		Thread thread = factory.getBean("thread",MyThread1.class);
		thread.start();
	} 
	
	
	@Test
	public void testPrototype(){
		for(int i=0;i<10;i++){
			User user = factory.getBean("user",User.class);
			System.out.println(user);
		}
	}
}

以上,便可以把线程对象交给了Spring容器管理,且可以通过Spring容器把其它Bean对象注入到线程对象中(如User对象)。需要注意的是线程的作用域要设置成scope="prototype"。


第二种方式,通过Spring的线程池对象ThreadPoolTaskExecutor来管理。

线程定义:

public class MyThread2 implements Runnable {
	private String name;
	
	public MyThread2() {
	}

	public MyThread2(String name){
		this.name = name;
	}

	public void run() {
		for(int i=0 ;i < 10;i++){
			System.out.println("thread = " + name );
		}
	}

}

配置文件:

       <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>  
测试代码:

public class ThreadTestTwo {
	private DefaultListableBeanFactory factory;
	
	@Before
	public void init(){
		factory = new DefaultListableBeanFactory();
		XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
		reader.loadBeanDefinitions("applicationContext-ioc.xml");
	}
	
	@Test
	public void testThreadPool(){
		ThreadPoolTaskExecutor taskExecutor = (ThreadPoolTaskExecutor)factory.getBean("taskExecutor");
		taskExecutor.execute(<span style="font-family:SimHei;color:#FF0000;">new MyThread2("thread1")</span>);
		taskExecutor.execute(<span style="color:#FF0000;">new MyThread2("thread2")</span>);
		taskExecutor.execute(<span style="color:#FF0000;">new MyThread2("thread3")</span>);
	} 
	
}

或者还可以把第二种方式修改如下:

线程代码:

public class MyThread3 implements Runnable {

	public void run() {
		for(int i=0 ;i < 10;i++){
			System.out.println("thread = " + this );
		}
	}

}

配置文件:

       <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>  
	<bean id="thread3" class="com.ec.spring.ioc.thread.MyThread3"></bean>

测试代码:

public class ThreadTestThree {
	private DefaultListableBeanFactory factory;
	
	@Before
	public void init(){
		factory = new DefaultListableBeanFactory();
		XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
		reader.loadBeanDefinitions("applicationContext-ioc.xml");
	}
	
	@Test
	public void testThreadPool(){
		ThreadPoolTaskExecutor taskExecutor = (ThreadPoolTaskExecutor)factory.getBean("taskExecutor");
		
		taskExecutor.execute(<span style="color:#FF0000;"><strong>(MyThread3)factory.getBean("thread3")</strong></span>);
		taskExecutor.execute(<span style="color:#FF0000;"><strong>(MyThread3)factory.getBean("thread3")</strong></span>);
		taskExecutor.execute(<strong><span style="color:#FF0000;">(MyThread3)factory.getBean("thread3")</span></strong>);
	} 
	
}


你可能感兴趣的:(spring,线程)