多线程有返回值Callable的使用

//调用方式
Callable<String> clazz = new PushReport(infos.toString(),account);
		 		NewThreadUtil.pushMessage(clazz);

 

package com.aaa.sms.util;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 
* @ClassName: NewThreadUtil 
* @date 2018年3月9日 下午2:16:47 
*
 */
public class NewThreadUtil{
//	Callable<T> clazz;
//	public NewThreadUtil(Callable<T> clazz){
//		this.clazz=clazz;
//	}
//	 Class<T> clazz;  
//
//	    public NewThreadUtil(Class<T> clazz) {  
//	        this.clazz = clazz;  
//	    }
	private static final Logger log=LoggerFactory.getLogger(NewThreadUtil.class);
	private static Integer corePoolSize = 5;
	private static Integer maximumPoolSize = 10;
	private static Long keepAliveTime = 10000L;//10分钟
	private static Integer queueCapacity = 25;//队列最大等待数
	private static  ExecutorService executorService = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime,
			java.util.concurrent.TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(queueCapacity));
	public  static void pushMessage(Callable<String> clazz){
		log.info("推送上行短信:主线程线程名称NewThreadUtil" + Thread.currentThread().getName());
		//Future<String> future 不能写返回值 否则会变成同步
		//多线程情况下,主线程try catch捕获不到子线程的throws exception
		try {
			executorService.submit(clazz);
		} catch (Exception e) {
			log.info("父类处理异常");
			e.printStackTrace();
		}
		
	}
	
	public static void shutDown(){
	 log.info("销毁线程池策略:服务停止停止接收消息");
		executorService.shutdownNow();
	}


	// 正常情况下主方法用try catch能够获取到子方法出现的异常
	 public static void function() throws NumberFormatException{
	 String s = "abc";
	 System.out.println(Double.parseDouble(s));
	 }
	
	 public static void main(String[] args) throws Exception{
	 try {
	 function();
	 } catch (NumberFormatException e) {
	 System.err.println("非数据类型不能转换。");
	 //e.printStackTrace();
	 }
	 }

}

 

package com.aaa.sms.task;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.Callable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.anxinjie.AnXinJieUtil;
import com.credithc.sms.dto.SmsAccountDTO;
import com.credithc.sms.listeners.InitDatasListener;
import com.credithc.sms.service.ISmsAccountService;
import com.credithc.sms.util.ToolSpring;
/**
 * 
* @ClassName: PushReport 
* @date 2018年3月15日 下午5:57:45 
*
 */
public class PushReport implements Callable<String> {
	private static final Logger log=LoggerFactory.getLogger(PushReport.class);
	private String report;
	private String accoutId;
	public PushReport(String report,String accoutId){
		this.report=report;
		this.accoutId=accoutId;
	}
	public PushReport(){
	}
	@Override
	public String call() throws Exception {
		log.info("推送状态:线程名称"+Thread.currentThread().getName());
		ISmsAccountService ismsAccountService=(ISmsAccountService) ToolSpring.getBean("ismsAccountService");
		String url = null;
		SmsAccountDTO smsAccount = new SmsAccountDTO();
		smsAccount.setSmsAccountId(accoutId);
		try {
			SmsAccountDTO accounts = ismsAccountService.qrySmsAccountById(smsAccount);
				if ("1".equals(accounts.getIsPush())) {
					url = InitDatasListener.getPropertie(accounts.getSmsMappingSystem() + "_pushReport_url");
					System.out.println(accoutId+"获取状态请求地址"+url);
					System.out.println(accoutId+"获取状态报告请求内容"+"message="+report);
					String result=AnXinJieUtil.postURL("message="+report, url);
					System.out.println(accoutId+"获取状态返回结果"+result);
					

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "ok";
	}

	
	

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值