美文网首页
多线程的运用

多线程的运用

作者: Lelontar | 来源:发表于2017-04-22 10:33 被阅读22次

1.spring整合线程池


<context:property-placeholder location="classpath:resource/*.properties" />

<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">

<property name="corePoolSize" value="${task.core_pool_size}" />

<property name="maxPoolSize" value="${task.max_pool_size}" />

<property name="queueCapacity" value="${task.queue_capacity}" />

<property name="keepAliveSeconds" value="${task.keep_alive_seconds}" />
</bean>

<task:annotation-driven />


2.关于properties的配置


task.core_pool_size=5
task.max_pool_size=50
task.queue_capacity=1000
task.keep_alive_seconds=60


3.代码的写法


①注入taskExecutor
@Resource(name = "taskExecutor")
private TaskExecutor taskExecutor;
②写一个方法
例如发送邮件
public void sendMail(Message msg, String[] userIds, Attach attach)
throws MessagingException, UnsupportedEncodingException {

    MimeMessage email = javaMailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(email, true, "utf-8");
    // 从哪里发送
    helper.setFrom("");
    // 发送给谁
    helper.setTo("");
    // 发送题目
    helper.setSubject("");

    if (CompusUtil.isNotEmpty(attach)) {

        File[] attFiles = attach.getAttFiles();
        String[] fns = attach.getAttfilename();
        for (int i = 0; i < attFiles.length; i++) {
            File file = attFiles[i];
            String fnsString = fns[i];
            helper.addAttachment(MimeUtility.encodeText(fnsString),
                    new FileSystemResource(fnsString));
        }
    }

    helper.setText(msg.getContent(), true);// 添加消息内容,html设置为true

    javaMailSender.send(email);// 发出邮件
}

③创建一个sendmailThread类调用②中的方法
private class sendMailThread implements Runnable {

    private Message msg;
    private String[] userIds;
    private Attach attach;
    //创建构造方法
    public sendMailThread(Message msg, String[] userIds, Attach attach) {
        super();
        this.msg = msg;
        this.userIds = userIds;
        this.attach = attach;
    }

    @Override
    public void run() {
        
        try {
        //此处调用了sendMail方法
            sendMail(msg,userIds,attach);
        } catch (UnsupportedEncodingException | MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
}

④通过taskExcecutor执行 线程
public int txAddMessage(Message msg,String[] userIds,Attach attach) {

    taskExecutor.execute(new sendMailThread(msg,userIds,attach));
    return 0;
}

相关文章

  • 多线程的运用

    1.spring整合线程池

    多线程的运用

    同步串行队列 同步并发队列 异步串行队列 异步并发队列 队列组 栅栏 队列组和栅栏的组合 信号量 死锁主线程 分析...

  • iOS多线程运用

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • volatile 多线程运用

    Java含两种内在的同步机制:同步块(或方法)和 volatile 变量。这两种机制的提出都是为了实现代码线程的安...

  • Java 多线程 : volatile

    在多线程并发编程中,锁的运用很常见。synchronized 的几种运用方式,相信大部分 Java 程序员已经很熟...

  • iOS多线程知识点总结之: 进程和线程

    最近准备找工作, 所以又把多线程的知识再学习总结一遍, 让自己更好的熟悉和运用 iOS 多线程的相关操作. 进程 ...

  • (七)iOS 开发之多线程编程

    为了编写高效的网络请求模板,开发者必须能够灵活地运用多线程的各种操作。iOS 岗位面试中常问到多线程的知识。iOS...

  • iOS开发 多线程的运用

    在iOS开发上搬了几年砖了,一直在向各位大神学习,这段时间公司项目完工了,整理一下相关技术点,向后来者做个借鉴,沉...

  • iOS 多线程GCD的运用

    理解 串行队列: 先入先出,执行完第一个再执行第二个。并发队列: 先执行第一个,第一个还没执行完时便可以执行第二个...

  • IOS异步获取数据并刷新界面dispatch_async的使用方

    在ios的开发和学习中多线程编程是必须会遇到并用到的,大量的后台运行,异步消息队列,基本都是运用了多线程来实现。 ...

网友评论

      本文标题:多线程的运用

      本文链接:https://www.haomeiwen.com/subject/ptqszttx.html