多线程 发短信 帮助
有个项目中用到发短信的功能,要求能短信群发,然后由另一线程去执行短信发送功能实现如下:
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); }
[解决办法]
不太会了