只有父类对象本身就是用子类new出来的时候, 才可以在将来被强制转换为子类对象.
public class Test { public void ttt(Integer a){ Function<Integer,String> x = s->{ //com.cc.base.Test@1c20c684 System.out.println(this.toString()); return s.toString(); }; x.apply(a); } }
public static void main(String[] args) throws Exception { Properties properties = new Properties(); properties.put("name","css"); properties.put("pss","ccc"); File file = new File("D:/cc.txt"); OutputStream outputStream = new FileOutputStream(file); properties.store(outputStream,"测试"); }
public static void main(String[] args) throws InterruptedException, ExecutionException, Exception { ExecutorService executorService = Executors.newFixedThreadPool(4); List<Callable<String>> list = new ArrayList<>(); for (int i = 0; i <100; i++) { int j = i; Callable<String> callable = ()->{ ThreadLocalRandom random = ThreadLocalRandom.current(); try { Thread.sleep(random.nextInt(1000)); } catch (InterruptedException e) { return "notok"+j; } return "ok"+j; }; list.add(callable); } List<Future<String>> list2 = executorService.invokeAll(list,1,TimeUnit.SECONDS); for (Future<String> future : list2) { if(!future.isCancelled()){ System.out.println(future.get()); } } Callable<String> callable = ()->{ try { Thread.sleep(3000); } catch (InterruptedException e) { //e.printStackTrace(); return "notok"; } return "ok"; }; ExecutorCompletionService<String> completionService = new ExecutorCompletionService<String>(executorService); completionService.submit(callable); System.out.println(completionService.take().get()); executorService.shutdownNow(); }