Java使用多線程發送訊息

來源:互聯網
上載者:User

標籤:class   over   複製   ble   線程池   程式   status   submit   integer   

在後台系統管理使用者資訊的時候,經常會用到批量發送提醒訊息,首先想到的有:

     (1)、迴圈發送列表,逐條發送。優點是:簡單,如果發送列表很少,而且沒有什麼耗時的操作,是比較好的一種選擇,缺點是:針對大批量的發送列表,不可取,耗時,程式會出現嚴重的阻塞問題。

     (2)、使用隊列(BlockingQueue),開啟多個線程,分為三個部分。一部分負責處理將發送列表放入隊列;一部分負責從隊列中讀取並發送訊息;第三部分負責監視隊列是否為空白及後續的操作。

     (3)、以下說到的這種模式,使用Future、Callable來返回傳送結果,覺得是一種比較好的方式,很簡單代碼也很詳細,就不介紹了。

代碼如下:

public class PublishMsgTest {    //建立固定大小為100 的線程池    private static ExecutorService service = Executors.newFixedThreadPool(100);        //發送訊息的商務邏輯方法    public int sendMsg(List<Integer> receivers,String content){        long begin = System.nanoTime();        AtomicInteger ai = new AtomicInteger(0);        List<Future<Integer>> list = new ArrayList<>();        //迴圈發送訊息        for(int i=0;i<receivers.size();i++){            Integer receiver = receivers.get(i);            //使用Future,Callable實現發送訊息後返回傳送結果            Future<Integer> future = service.submit(new Callable<Integer>() {                @Override                public Integer call() throws Exception {                    //調用相對比較耗時的發送訊息介面                    Thread.sleep(200);                    //發送訊息                    int resultStatus = sendMsg(receiver,content);                    System.out.println("接收者【"+receiver+"】,發送結果【"+resultStatus+"】");                    return resultStatus;                }            });            list.add(future);        }        System.out.println("-----------------------"+(System.nanoTime()-begin)/1000_000d+"-----------------------");        //迴圈接收發送結果,相當於一個使線程同步的過程,這個過程是比較耗時的        for(int i=0;i<list.size();i++){            try {                int resultStatus = list.get(i).get();                if(resultStatus == 0){//發送成功                    ai.incrementAndGet();                }            } catch (Exception e) {                e.printStackTrace();            }         }        System.out.println("發送訊息結束,耗時:"+(System.nanoTime()-begin)/1000_000d);        return ai.get();    }        public static void main(String[] args){        PublishMsgTest pmt = new PublishMsgTest();        //待接收人        List<Integer> receivers = new ArrayList<Integer>();        for(int i=0;i<1000;i++){            receivers.add(i);        }        String content = "群發訊息_測試代碼";        int successCount = pmt.sendMsg(receivers, content);        System.out.println("共有【"+receivers.size()+"】接收者,發送成功【"+successCount+"】");    }    //完成發送訊息    private int sendMsg(Integer receiver, String content) {        if(receiver%2 == 0){//類比被2整除,即為發送成功            return 0;        }        return 1;    }

 

以上代碼的執行結果:

-----------------------14.786889-----------------------接收者【2】,發送結果【0】接收者【3】,發送結果【1】接收者【4】,發送結果【0】接收者【0】,發送結果【0】接收者【6】,發送結果【0】接收者【7】,發送結果【1】接收者【1】,發送結果【1】接收者【5】,發送結果【1】接收者【10】,發送結果【0】....接收者【994】,發送結果【0】接收者【993】,發送結果【1】接收者【992】,發送結果【0】接收者【996】,發送結果【0】接收者【999】,發送結果【1】接收者【998】,發送結果【0】接收者【997】,發送結果【1】發送訊息結束,耗時:2033.053433共有【1000】接收者,發送成功【500】

 摘自:http://www.cnblogs.com/quanenmin/p/4914620.html

Java使用多線程發送訊息

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.