Java多线程实践

Posted on 2014-02-07 17:15  炫谷  阅读(406)  评论(0编辑  收藏  举报

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,线程池来执行通知推送服务。

 

Copyright © 2024 炫谷
Powered by .NET 8.0 on Kubernetes