ThreadPoolExecutor中的JavaApns?

rjee0c15  于 11个月前  发布在  Java
关注(0)|答案(1)|浏览(91)

为了让一个ApnsService保持活跃,我创建了一个ThreadPoolExecutor来保持线程发送推送。下面是worker的代码:

public static class PushWorker implements Runnable{
        
        private static final Logger log = Logger.getLogger(PushWorker.class);
        
        private String token;
        private String alert;
        private int badge;
        private String sound;
        private ApnsService service;
        
        public PushWorker(ApnsService srv, String t, String a, int b, String s){
            token = t;
            alert = a;
            badge = b;
            sound = s;
            service = srv;
        }
        @Override
        public void run() {

            
            PayloadBuilder payloadBuilder = APNS.newPayload();
            payloadBuilder.sound(sound);
            payloadBuilder.badge(badge);

            payloadBuilder.alertBody(alert);
            

            String payload = payloadBuilder.build();
                        service.push(token, payload);

        }
    }

字符串
当我创建一个worker并在Threadpool中执行它时:

ThreadPoolExecutor pool = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS,queue);

pool.execute(worker);


程序终止于:PayloadBuilder payloadBuilder = APNS.newPayload();,没有任何错误或异常。但是,如果按照以下方式,它可以工作:

PushWorker worker = new PushWorker(service, token, alert, badge, null);

worker.run();


我不知道为什么线程在Threadpool中终止.有人有同样的问题吗?

krugob8w

krugob8w1#

尝试使用

try {
    Future result = pool.submit(worker)
    result.get()

} catch (ExecutionException e) {
    // actual exception in e.getCause()
}

字符串
相反

相关问题