您的位置:首页 > 编程语言 > Java开发

Java多线程实践

2014-02-07 17:15 190 查看
1、定义一个线程类,通过HTTP接口推送通知

public class PushNoticeThread extends Thread {
private String uid;
private String number;
private String noticeUrl;

public PushNoticeThread(String uid, String number, String noticeUrl) {
super();
this.uid = uid;
this.number = number;
this.noticeUrl = noticeUrl;
}

@Override
public void run() {
// 推送通知
String url = MessageFormat.format(noticeUrl, new Object[] { uid, number });
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
httpClient.execute(httpGet);
} catch (ClientProtocolException e) {
LOGGER.error("e", e);
} catch (IOException e) {
LOGGER.error("e", e);
}
}
}


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

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


说明:定义的线程池大小为200,线程池来执行通知推送服务。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: