转载请标明出处: http://xuantan.iteye.com/blog/1867971
- 编写代码的过程中,我们常常会抽象出来一些工具类及工具方法(静态类、静态方法)。
- 下面请看一段代码:
public class BaiduMessagePushAPI { public static void messagePush(String baiduUserId,MessagePOJO messagePOJO,String msgKeys,String baiduChannelId,Integer messageType) { //序列化 String messages = JSON.toJSONString(messagePOJO); messages = jsonencode(messages); long timestamp = System.currentTimeMillis()/1000; Map<String,Object> params = new HashMap<String, Object>(); params.put("method", METHOD); params.put("apikey", API_KEY); ... ResultObject resultObject; try { resultObject = HttpClientUtils.sendPost(URL, params); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } }
- 以上便是一个很常见的工具方法,我们无需关心其具体内容,总之大概意思就是发起一个Post请求,而且不关注这次请求的结果究竟是什么。
- 遇到以上这种业务场景,我认为采用开辟一个新线程来处理这段业务更为合适。当提到线程,我们不约而同的又会联想到线程池。
- 下面这段代码就是基于以上的需求和代码重构而成,其中包括了几个技术点:1、单例模式 2、多线程 3、线程池
public class BaiduMessagePushAPI { private volatile static BaiduMessagePushAPI singleInstance; private BaiduMessagePushAPI() { } public static BaiduMessagePushAPI getInstance() { if (singleInstance == null) { synchronized (BaiduMessagePushAPI.class) { if (singleInstance == null) { singleInstance = new BaiduMessagePushAPI(); } } } return singleInstance; } ... public void messagePush(String baiduUserId,MessagePOJO messagePOJO,String msgKeys,String baiduChannelId,Integer messageType) { //线程池工具方法 ThreadPoolUtils.execute(new messagePushThread(baiduUserId, messagePOJO, msgKeys, baiduChannelId, messageType)); } private class messagePushThread implements Runnable { String baiduUserId; MessagePOJO messagePOJO; String msgKeys; String baiduChannelId; Integer messageType; public messagePushThread(String baiduUserId,MessagePOJO messagePOJO,String msgKeys,String baiduChannelId,Integer messageType){ this.baiduUserId = baiduUserId; this.messagePOJO = messagePOJO; this.msgKeys = msgKeys; this.baiduChannelId = baiduChannelId; this.messageType = messageType; } @Override public void run() { //序列化 String messages = JSON.toJSONString(messagePOJO); messages = jsonencode(messages); long timestamp = System.currentTimeMillis()/1000; Map<String,Object> params = new HashMap<String, Object>(); params.put("method", METHOD); params.put("apikey", API_KEY); .... try { resultObject = HttpClientUtils.sendPost(URL, params); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } }
根据网友的反馈,意识到自己确实犯了一些错误,因此特来修改:
其实上面的业务场景,只需要把线程类变为静态的即可,并且放在静态方法中调用,无需采用单例模式这样绕圈子的方案,修改后代码如下:
public class BaiduMessagePushAPI { public static void messagePush(String baiduUserId,MessagePOJO messagePOJO,String msgKeys,String baiduChannelId,Integer messageType) { ThreadPoolUtils.execute(new messagePushThread(baiduUserId, messagePOJO, msgKeys, baiduChannelId, messageType)); } private static class messagePushThread implements Runnable { String baiduUserId; MessagePOJO messagePOJO; String msgKeys; String baiduChannelId; Integer messageType; public messagePushThread(String baiduUserId,MessagePOJO messagePOJO,String msgKeys,String baiduChannelId,Integer messageType){ this.baiduUserId = baiduUserId; this.messagePOJO = messagePOJO; this.msgKeys = msgKeys; this.baiduChannelId = baiduChannelId; this.messageType = messageType; } @Override public void run() { //序列化 String messages = JSON.toJSONString(messagePOJO); messages = jsonencode(messages); long timestamp = System.currentTimeMillis()/1000; Map<String,Object> params = new HashMap<String, Object>(); params.put("method", METHOD); params.put("apikey", API_KEY); ...... ResultObject resultObject; try { resultObject = HttpClientUtils.sendPost(URL, params); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } }