四十三、使用线程池与CountDownLatch完成多线程阻塞

#          使用线程池与CountDownLatch完成多线程阻塞

这篇文章主要是将主线程阻塞,让子线程先跑。

###1、添加Spring配置文件

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
                        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd    
                        http://www.springframework.org/schema/context    
                        http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 

   <context:annotation-config /> 

   <context:component-scan base-package="com.xxxx" /> 

   <!-- 线程池 -->
   <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
         <!-- 核心线程数 -->
        <property name="corePoolSize" value="5" />
        <!-- 最大线程数 -->
        <property name="maxPoolSize" value="10" />
        <!-- 队列最大长度 >=mainExecutor.maxSize -->
        <property name="queueCapacity" value="25" />
        <!-- 线程池维护线程所允许的空闲时间 -->
        <property name="keepAliveSeconds" value="3000" />
        <!-- 线程池对拒绝任务(无线程可用)的处理策略 ThreadPoolExecutor.CallerRunsPolicy策略 ,调用者的线程会执行该任务,如果执行器已关闭,则丢弃.  -->
        <property name="rejectedExecutionHandler">
            <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />
        </property>
    </bean>
 </beans>  
 

###2、线程需要调用的业务类

public class TestShow {

   java.text.SimpleDateFormat dateTimeFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   public void start(String result){
       System.out.println("现在的时间为:"+dateTimeFormat.format(new Date())+"    "+result);

   }
}

###3、实现runnable接口,调用业务类

private class TaskThread implements Runnable{
		
		private CountDownLatch latch;
		java.text.SimpleDateFormat dateTimeFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		private String result;
		private TaskThread(String result,CountDownLatch latch){
			super();
			this.result=result;
			this.latch=latch;
		}
		
		@Override
		public void run() {
			try{
				 for(int i=0;i<10000;i++){
					// dateTimeFormat.format(new Date());
				  }
			  System.out.println("现在的时间为:"+dateTimeFormat.format(new Date())+"    "+result);
			 
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				if(this.latch!=null){
					latch.countDown();
				}
			}
			
		}
		
	}

###4、封装线程池及接口类

@Component
public class ThreadRunnable {

	@Autowired
	private TaskExecutor taskExecutor;
	
	public void executeThread(String result,CountDownLatch latch){
	
		this.taskExecutor.execute(new TaskThread(result,latch));
	}
}

###5、测试类

package threadPoolTaskExecutor;

import java.util.concurrent.CountDownLatch;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.xxxx.thread.ThreadRunnable;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:spring.xml")
public class ThreadRunnableTest extends AbstractJUnit4SpringContextTests{
  
	@Autowired
	ThreadRunnable threadRunnable;
	
	@Test
	public void test() throws InterruptedException{
		CountDownLatch latch=new CountDownLatch(12);
		for(int i=0;i<11;i++){	
			threadRunnable.executeThread("走在大数据的边缘",latch);
		}
		//等待到latch=0才执行
		latch.await();
		System.out.println("执行完毕了吗!");
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值