日期:2014-05-17 浏览次数:20811 次
public class SendSmsDemo extends Thread { private static SmsHandler smsHandler; private static BlockingQueue<SMS> queue = new LinkedBlockingQueue<SMS>(); private static SendSmsDemo instance = null; private SendSmsDemo() { } public synchronized static SendSmsDemo getInstance(SmsHandler handler) { if (instance == null) { System.out.println("new instatnce..."); instance = new SendSmsDemo(); instance.setDaemon(true); smsHandler = handler; instance.start(); } return instance; } public void add(SMS sms) { queue.add(sms); } @Override public void run() { while(true){ SMS sms=new SMS(); try { sms = queue.take(); } catch (InterruptedException e) { e.printStackTrace(); } smsHandler.sendSMS(sms); } } }
while(true){ SMS sms=new SMS(); try { sms = queue.take(); } catch (InterruptedException e) { e.printStackTrace(); } smsHandler.sendSMS(sms); }
------解决方案--------------------
不太会了