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

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

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

1、添加Spring配置文件

  
<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" />
        
        <property name="queueCapacity" value="25" />
        
        <property name="keepAliveSeconds" value="3000" />
        
        <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("执行完毕了吗!");
    }
}

你可能感兴趣的:(从无到有搭建后台架构及运维架构)