class Test { public void method01() { System.out.println("-----------------------01"); } public void method03() { System.out.println("-----------------------------03"); } static boolean task2() { ExecutorDemo e = new ExecutorDemo(); e.asynTask(); System.out.println("------------task2 end-----------"); return true; } public static void main(String[] args) { Test test = new Test(); test.method01(); task2(); test.method03(); } } class ExecutorDemo { private ExecutorService executor = Executors.newFixedThreadPool(1); public void asynTask() { executor.submit(new Runnable() { @Override public void run() { try { Thread.sleep(3000);//方便观察结果 System.out.println("=============="); } catch (InterruptedException e) { e.printStackTrace(); } } }); } }
java 异步方法
最新推荐文章于 2024-06-27 07:30:00 发布