java 中多线程和锁的使用以及获取多线程执行结果
多线程一:原生的写法 关键词 implements 实现 Runnable 类
run() 方法
注意点 : 创建类的实例 InterfaceController inter=new InterfaceController(); 的时候,不要放在循环中 , 如果放在循环中 run 方法中的lock 是起不到作用的,
正确写法 看下面的例子
@Controller @RequestMapping("/interface") public class InterfaceController implements Runnable{ private Lock lock = new ReentrantLock(); private List<LoaneePriorityModel> listPage; int k = 1; public void setlistPage(List<LoaneePriorityModel> listPage) { this.listPage = listPage; } public void setk(int k) { this.k = k; } private Lock lock2 = new ReentrantLock(); /* 类Runnable 的具体实现方法 * @see java.lang.Runnable#run() */ public void run(){ lock2.lock(); try{ if(k==1) { log.info(MessageFormat.format("k==1睡眠5秒:{0}","")); Thread.sleep(5000);// 睡眠100毫秒 } List<LoaneePriorityModel> aa=listPage; log.info(MessageFormat.format("输出数据集条数:{0}", aa.size())); } catch(Exception ex) { log.info(MessageFormat.format("多线程异常信息:{0}",ex)); } finally{ lock2.unlock(); } } @RequestMapping(value = "updateLoaneePriorityNew", method = RequestMethod.POST) @ResponseBody public int updateLoaneePriorityNew(HttpServletRequest request, @RequestBody String requestBody) { lock.lock(); try { JSONArray json = JSONArray.fromObject(requestBody); log.info(MessageFormat.format("更改借款工单优先级 ,接收到的josn字符串:{0}", json)); System.out.println(MessageFormat.format( "更改借款工单优先级 ,接收到的josn字符串:{0}", json)); List<LoaneePriorityModel> persons = (List<LoaneePriorityModel>) JSONArray .toCollection(json, LoaneePriorityModel.class); InterfaceController inter=new InterfaceController(); for (int i = 0; i <= persons.size(); i++) { // 写修改优先级的主体方法 int f=2; //定义每组的数量 if(i==f*k-1&&i!= persons.size()) { log.info(MessageFormat.format("-----f*k等于:{0}", f*k)); listPage= persons.subList(0, f*k); log.info(MessageFormat.format("输入数据集条数:{0}", listPage.size())); inter.setlistPage(listPage); inter.setk(k); Thread t = new Thread(inter); Thread.sleep(5000);// 睡眠100毫秒 t.start(); k++; } if(i== persons.size()) { log.info(MessageFormat.format("=====f*(k-1)等于:{0}", f*(k-1))); listPage= persons.subList(f*(k-1), persons.size()); //InterfaceController inter=new InterfaceController(); log.info(MessageFormat.format("输入数据集条数:{0}", listPage.size())); inter.setlistPage(listPage); Thread t = new Thread(inter); inter.setk(k); t.start(); k++; } } } catch (Exception ex) { log.info(MessageFormat.format("更改借款工单优先级 ,出现异常:{0}", ex)); System.out.println(MessageFormat.format("更改借款工单优先级 ,出现异常:{0}", ex)); } finally { lock.unlock(); } return k; } }
//-----------------------------------------------------------------------------------------------------------
多线程二:不需要继承 实现 Runnable 接口的方式
ExecutorService 需要引用包 package java.util.concurrent;
ExecutorService threadPool = Executors.newFixedThreadPool(ConstantUtil.THREAD_POOL_SIZE); threadPool.submit(new Callable<String>() { @Override public String call() throws Exception { batchStartProcessCancel(list,user); return null; } });
多线程三:执行多线程并获取多线程执行的结果
CommResultMsg: 是自定义实体对象
List<Future<CommResultMsg>> listFuture=new ArrayList<Future<CommResultMsg>>(); //声明多线程 ExecutorService threadPool = Executors.newFixedThreadPool(ConstantUtil.THREAD_POOL_SIZE); //-----------------------------多线程 Future<CommResultMsg> future = threadPool.submit(new Callable<CommResultMsg>() { @Override public CommResultMsg call() throws Exception { //TimeUnit.SECONDS.sleep(2); CommResultMsg commsg= uploadInitialAssetsFile(list2,final_user,final_ftpPrefix,num2); return commsg; } }); //-----------------------------多线程 listFuture.add(future);