“消息队列”是在消息的传输过程中保存消息的容器。和我们学过的LinkedHashMap,TreeSet等一样,都是容器。既然是容器,就有有自己的特性,就像LinkedHashMap是以键值对存储。存取顺序不变。而消息队列,看到队列就可以知道。这个容器里面的消息是站好队的,一般遵从先进先出原则。
java中已经为我们封装好了很多的消息队列。在java 1.5版本时推出的java.util.concurrent中有很多现成的队列供我们使用。特性繁多,种类齐全。是你居家旅游开发必备QAQ。
下面简单列举这个包中的消息队列
:阻塞队列 BlockingQueue
数组阻塞队列 ArrayBlockingQueue
延迟队列 DelayQueue
链阻塞队列 LinkedBlockingQueue
具有优先级的阻塞队列 PriorityBlockingQueue
同步队列 SynchronousQueue
阻塞双端队列 BlockingDeque
链阻塞双端队列 LinkedBlockingDeque
不同的队列不同的特性决定了队列使用的时机,感兴趣的话你可以详细了解。具体的使用方式我就不赘述了
如下:public static String do_post(String url, List<NameValuePair>name_value_pair) throws IOException {
String body = "{}"
DefaultHttpClient httpclient = new DefaultHttpClient()
try {
HttpPost httpost = new HttpPost(url)
httpost.setEntity(new UrlEncodedFormEntity(name_value_pair, StandardCharsets.UTF_8))
HttpResponse response = httpclient.execute(httpost)
HttpEntity entity = response.getEntity()
body = EntityUtils.toString(entity)
} finally {
httpclient.getConnectionManager().shutdown()
}
return body
}
public static String do_get(String url) throws ClientProtocolException, IOException {
String body = "{}"
DefaultHttpClient httpclient = new DefaultHttpClient()
try {
HttpGet httpget = new HttpGet(url)
HttpResponse response = httpclient.execute(httpget)
HttpEntity entity = response.getEntity()
body = EntityUtils.toString(entity)
} finally {
httpclient.getConnectionManager().shutdown()
}
return body
}