java线程学习
//线程池创建,使用线程池提交任务,线程使用Callable接口,线程执行有返回值,等待所有子线程执行完成再继续执行主线程
public static void main(String[] args) { int corePoolSize = 3; int maximumPoolSize = Runtime.getRuntime().availableProcessors() * 2; BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(512); RejectedExecutionHandler policy = new ThreadPoolExecutor.DiscardPolicy(); //什么也不做,直接忽略 ExecutorService threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, 0, TimeUnit.SECONDS, queue, policy); submitTisks(gatherDate, hbaseConfig, corePoolSize, threadPoolExecutor); } /** * 提交任务 * @param gatherDate * @param hbaseConfig * @param corePoolSize * @param threadPoolExecutor */ private static void submitTisks(String gatherDate, Configuration hbaseConfig, int corePoolSize, ExecutorService threadPoolExecutor) { List<Future<Boolean>> taskFutureList = new ArrayList<>(); // 提交任务,任务的执行由线程池去调用执行并管理。 Future<Boolean> gpsfuture = threadPoolExecutor.submit(new GPSTask(hbaseConfig, gatherDate, xikangTidList, rootPath)); // 这里获取结果任务的Future,并放到list中,供所有任务提交完后,通过每个任务的Future判断执行状态和结果。 taskFutureList.add(gpsfuture); int done = 0; //完成任务的数量 while (!taskFutureList.isEmpty()) { Iterator<Future<Boolean>> iter = taskFutureList.iterator(); while (iter.hasNext()) { Future<Boolean> fut = iter.next(); if (fut.isDone()) { try{ Boolean flag = fut.get(); if (flag){ done++;} }catch (Exception e){ e.printStackTrace(); } iter.remove(); } } } } /************************************使用Callable接口**************************************/ static class GPSTask implements Callable<Boolean> { Configuration config; String recordDate; List<Long> xkDatanoList; String rootPath; Thread currentThread; public GPSTask(Configuration config, String recordDate, List<Long> xkDatanoList,String rootPath) { this.config = config; this.recordDate = recordDate; this.xkDatanoList = xkDatanoList; this.rootPath = rootPath; } @Override public Boolean call() throws Exception { this.currentThread = Thread.currentThread(); return FindDataUtil.getData4HbaseGPSSQ(config, recordDate, xkDatanoList,rootPath,currentThread); } }
//线程创建,主线程等待子线执行完成后再执行(针对子线程只有1个的时候),子线程使用Thread
public static void main(String[] args) { GPSRunTask gpsRunTask1 = new GPSRunTask(hbaseConfig, gatherDate, xikangTidList, rootPath,gpsPath); GPSRunTask gpsRunTask2 = new GPSRunTask(hbaseConfig, gatherDate, xikangTidList, rootPath,gpsPath); gpsRunTask1.start () ; gpsRunTask2.start () ; try { gpsRunTask1.join(); gpsRunTask2.join(); } catch (InterruptedException e) { e.printStackTrace(); } // 等待已提交的任务全部结束 不再接受新的任务 System.out.println("主线程再次执行"); } static class GPSRunTask extends Thread { Configuration config; String recordDate; List<Long> xkDatanoList; String rootPath; String gpsPath; Thread currentThread; public GPSRunTask(Configuration config, String recordDate, List<Long> xkDatanoList, String rootPath, String gpsPath) { this.config = config; this.recordDate = recordDate; this.xkDatanoList = xkDatanoList; this.rootPath = rootPath; this.gpsPath = gpsPath; } public void run(){ this.currentThread = Thread.currentThread(); synchronized (currentThread) { System.out.println("线程:"+currentThread.getName()); try { Thread.sleep(10000L); System.out.println(recordDate); }catch (InterruptedException e){ e.printStackTrace(); } } }
//使用倒数计数器收集所有线程执行结果的状态,(此种方法也适用于使用 ExecutorService summit 的任务的执行)