zoukankan      html  css  js  c++  java
  • Java多线程实践

    1、定义一个线程类,通过HTTP接口推送通知

     1 public class PushNoticeThread extends Thread {
     2     private String uid;
     3     private String number;
     4     private String noticeUrl;
     5 
     6     public PushNoticeThread(String uid, String number, String noticeUrl) {
     7         super();
     8         this.uid = uid;
     9         this.number = number;
    10         this.noticeUrl = noticeUrl;
    11     }
    12 
    13     @Override
    14     public void run() {
    15         // 推送通知
    16         String url = MessageFormat.format(noticeUrl, new Object[] { uid, number });
    17         HttpClient httpClient = new DefaultHttpClient();
    18         HttpGet httpGet = new HttpGet(url);
    19         try {
    20             httpClient.execute(httpGet);
    21         } catch (ClientProtocolException e) {
    22             LOGGER.error("e", e);
    23         } catch (IOException e) {
    24             LOGGER.error("e", e);
    25         }
    26     }
    27 }

    2、在Servcie里面调用该线程类,多线程执行该服务

    1 private ExecutorService pushPool = Executors.newFixedThreadPool(200);
    2 public void pushNotice(String uids, String noticeUrl) {
    3     Thread pushThread = new PushNoticeThread(uids, beMentNums, noticeUrl);
    4        pushPool.execute(pushThread);
    5 }

    说明:定义的线程池大小为200,线程池来执行通知推送服务。

  • 相关阅读:
    SpringBoot构建RESTful API
    Zynq7000系列之芯片系统结构概述
    FPGA编程技巧系列之按键边沿检测
    异常处理规范
    接口定义规范
    工具类编写规范
    第三个月
    测试计算器心得
    2015年三月
    第一份工作
  • 原文地址:https://www.cnblogs.com/enshrineZither/p/3539724.html
Copyright © 2011-2022 走看看