In the background to manage user information, often used to send a notification message in bulk, the first thought is:
(1), Loop send list, send by article. The advantage is: simple, if the sending list is very few, and there is no time-consuming operation, is a better choice, the disadvantage is: for a large number of send list, not advisable, time-consuming, the program will have serious blocking problems.
(2), using the queue (blockingqueue), open multiple threads, divided into three parts. Part is responsible for handling sending lists into the queue; part is responsible for reading from the queue and sending messages; The third part is responsible for monitoring whether the queue is empty and subsequent operations.
(3), the following talk about this mode, using the future, callable to return to send results, feel is a better way, very simple code is also very detailed, not introduced.
The code is as follows:
public class Publishmsgtest {//Create a thread pool with a fixed size of 100 private static executorservice service = Executors.newfixedthreadpoo L (100); Business logic method for sending messages public int sendmsg (list<integer> receivers,string content) {Long begin = System.nanotime (); Atomicinteger ai = new Atomicinteger (0); list<future<integer>> list = new arraylist<> (); Loop send message for (int i=0;i<receivers.size (); i++) {Integer receiver = receivers.get (i); Send a message back with future,callable implementation future<integer> future = Service.submit (new callable<integer> () { @Override public Integer Call () throws Exception {//calls a relatively time-consuming send message interface Thread.Sleep (200); Send Message int resultstatus = sendmsg (receiver,content); SYSTEM.OUT.PRINTLN ("Recipient" "+receiver+" ", send Result" "+resultstatus+" ""); return resultstatus; } }); List.add (future); } System.out.println ("-----------------------" + (System.nanotime ()-begin)/1000_000d+ "-----------------------"); The loop receives the sending result, which is equivalent to the process of synchronizing the thread, which is time consuming for (int i=0;i<list.size (); i++) {try {int resu Ltstatus = List.get (i). get (); if (Resultstatus = = 0) {//Send success ai.incrementandget (); }} catch (Exception e) {e.printstacktrace (); }} System.out.println ("Send message end, time consuming:" + (System.nanotime ()-begin)/1000_000d); return Ai.get (); } public static void Main (string[] args) {publishmsgtest PMT = new Publishmsgtest (); Recipient list<integer> Receivers = new arraylist<integer> (); for (int i=0;i<1000;i++) {receivers.add (i); The String content = "Mass message _ Test code"; int successcount = pmt.sendmsg (receivers, content); SysteM.OUT.PRINTLN ("Total" "+receivers.size () +" "Receiver, send Success" "+successcount+" ""); }//complete send message private int sendmsg (Integer receiver, String content) {if (receiver%2 = = 0) {//simulation is divisible by 2, which is sent successfully return 0; } return 1; }
The execution result of the above code:
-----------------------14.786889-----------------------Receiver "2", send the result "0" receiver "3", send the result "1" receiver "4", send the result "0" receiver "0", send the result "0 "Recipient" 6 ", send the result" 0 "receiver" 7 ", send the result" 1 "receiver" 1 ", send the result" 1 "receiver" 5 ", send the result" 1 "receiver" 10 ", send the result" 0 ".... Receiver "994", send the result "0" receiver "993", send the result "1" receiver "992", send the result "0" receiver "996", send the result "0" receiver "999", send the result "1" receiver "998", send the result "0" receiver "997", send the result " 1 "Send Message end, time: 2033.053433 Total" 1000 "recipient, send success" 500 "
Excerpt from: http://www.cnblogs.com/quanenmin/p/4914620.html
Java uses multithreading to send messages