前言

以前需要异步执行一个任务时,一般是用Thread或者线程池Executor去创建。如果需要返回值,则是调用Executor.submit获取Future。但是多个线程存在依赖组合,我们又能怎么办?可使用同步组件CountDownLatch、CyclicBarrier等;其实有简单的方法,就是用CompeletableFuture

  • 线程任务的创建
  • 线程任务的串行执行
  • 线程任务的并行执行
  • 处理任务结果和异常
  • 多任务的简单组合
  • 取消执行线程任务
  • 任务结果的获取和完成与否判断

关注公众号,一起交流,微信搜一搜: 潜行前行

1 创建异步线程任务

根据supplier创建CompletableFuture任务

  1. //使用内置线程ForkJoinPool.commonPool(),根据supplier构建执行任务
  2. public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)
  3. //指定自定义线程,根据supplier构建执行任务
  4. public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor)

根据runnable创建CompletableFuture任务

  1. //使用内置线程ForkJoinPool.commonPool(),根据runnable构建执行任务
  2. public static CompletableFuture<Void> runAsync(Runnable runnable)
  3. //指定自定义线程,根据runnable构建执行任务
  4. public static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor)
  • 使用示例
  1. ExecutorService executor = Executors.newSingleThreadExecutor();
  2. CompletableFuture<Void> rFuture = CompletableFuture
  3. .runAsync(() -> System.out.println("hello siting"), executor);
  4. //supplyAsync的使用
  5. CompletableFuture<String> future = CompletableFuture
  6. .supplyAsync(() -> {
  7. System.out.print("hello ");
  8. return "siting";
  9. }, executor);
  10. //阻塞等待,runAsync 的future 无返回值,输出null
  11. System.out.println(rFuture.join());
  12. //阻塞等待
  13. String name = future.join();
  14. System.out.println(name);
  15. executor.shutdown(); // 线程池需要关闭
  16. --------输出结果--------
  17. hello siting
  18. null
  19. hello siting

常量值作为CompletableFuture返回

  1. //有时候是需要构建一个常量的CompletableFuture
  2. public static <U> CompletableFuture<U> completedFuture(U value)

2 线程串行执行

任务完成则运行action,不关心上一个任务的结果,无返回值

  1. public CompletableFuture<Void> thenRun(Runnable action)
  2. public CompletableFuture<Void> thenRunAsync(Runnable action)
  3. public CompletableFuture<Void> thenRunAsync(Runnable action, Executor executor)
  • 使用示例
  1. CompletableFuture<Void> future = CompletableFuture
  2. .supplyAsync(() -> "hello siting", executor)
  3. .thenRunAsync(() -> System.out.println("OK"), executor);
  4. executor.shutdown();
  5. --------输出结果--------
  6. OK

任务完成则运行action,依赖上一个任务的结果,无返回值

  1. public CompletableFuture<Void> thenAccept(Consumer<? super T> action)
  2. public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action)
  3. public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action, Executor executor)
  • 使用示例
  1. ExecutorService executor = Executors.newSingleThreadExecutor();
  2. CompletableFuture<Void> future = CompletableFuture
  3. .supplyAsync(() -> "hello siting", executor)
  4. .thenAcceptAsync(System.out::println, executor);
  5. executor.shutdown();
  6. --------输出结果--------
  7. hello siting

任务完成则运行fn,依赖上一个任务的结果,有返回值

  1. public <U> CompletableFuture<U> thenApply(Function<? super T,? extends U> fn)
  2. public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn)
  3. public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn, Executor executor)
  • 使用示例
  1. ExecutorService executor = Executors.newSingleThreadExecutor();
  2. CompletableFuture<String> future = CompletableFuture
  3. .supplyAsync(() -> "hello world", executor)
  4. .thenApplyAsync(data -> {
  5. System.out.println(data); return "OK";
  6. }, executor);
  7. System.out.println(future.join());
  8. executor.shutdown();
  9. --------输出结果--------
  10. hello world
  11. OK

thenCompose - 任务完成则运行fn,依赖上一个任务的结果,有返回值

  • 类似thenApply(区别是thenCompose的返回值是CompletionStage,thenApply则是返回 U),提供该方法为了和其他CompletableFuture任务更好地配套组合使用
  1. public <U> CompletableFuture<U> thenCompose(Function<? super T, ? extends CompletionStage<U>> fn)
  2. public <U> CompletableFuture<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn)
  3. public <U> CompletableFuture<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn,
  4. Executor executor)
  • 使用示例
  1. //第一个异步任务,常量任务
  2. CompletableFuture<String> f = CompletableFuture.completedFuture("OK");
  3. //第二个异步任务
  4. ExecutorService executor = Executors.newSingleThreadExecutor();
  5. CompletableFuture<String> future = CompletableFuture
  6. .supplyAsync(() -> "hello world", executor)
  7. .thenComposeAsync(data -> {
  8. System.out.println(data); return f; //使用第一个任务作为返回
  9. }, executor);
  10. System.out.println(future.join());
  11. executor.shutdown();
  12. --------输出结果--------
  13. hello world
  14. OK

3 线程并行执行

两个CompletableFuture并行执行完,然后执行action,不依赖上两个任务的结果,无返回值

  1. public CompletableFuture<Void> runAfterBoth(CompletionStage<?> other, Runnable action)
  2. public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action)
  3. public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action, Executor executor)
  • 使用示例
  1. //第一个异步任务,常量任务
  2. CompletableFuture<String> first = CompletableFuture.completedFuture("hello world");
  3. ExecutorService executor = Executors.newSingleThreadExecutor();
  4. CompletableFuture<Void> future = CompletableFuture
  5. //第二个异步任务
  6. .supplyAsync(() -> "hello siting", executor)
  7. // () -> System.out.println("OK") 是第三个任务
  8. .runAfterBothAsync(first, () -> System.out.println("OK"), executor);
  9. executor.shutdown();
  10. --------输出结果--------
  11. OK

两个CompletableFuture并行执行完,然后执行action,依赖上两个任务的结果,无返回值

  1. //第一个任务完成再运行other,fn再依赖消费两个任务的结果,无返回值
  2. public <U> CompletableFuture<Void> thenAcceptBoth(CompletionStage<? extends U> other,
  3. BiConsumer<? super T, ? super U> action)
  4. //两个任务异步完成,fn再依赖消费两个任务的结果,无返回值
  5. public <U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStage<? extends U> other,
  6. BiConsumer<? super T, ? super U> action)
  7. //两个任务异步完成(第二个任务用指定线程池执行),fn再依赖消费两个任务的结果,无返回值
  8. public <U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStage<? extends U> other,
  9. BiConsumer<? super T, ? super U> action, Executor executor)
  • 使用示例
  1. //第一个异步任务,常量任务
  2. CompletableFuture<String> first = CompletableFuture.completedFuture("hello world");
  3. ExecutorService executor = Executors.newSingleThreadExecutor();
  4. CompletableFuture<Void> future = CompletableFuture
  5. //第二个异步任务
  6. .supplyAsync(() -> "hello siting", executor)
  7. // (w, s) -> System.out.println(s) 是第三个任务
  8. .thenAcceptBothAsync(first, (s, w) -> System.out.println(s), executor);
  9. executor.shutdown();
  10. --------输出结果--------
  11. hello siting

两个CompletableFuture并行执行完,然后执行action,依赖上两个任务的结果,有返回值

  1. //第一个任务完成再运行other,fn再依赖消费两个任务的结果,有返回值
  2. public <U,V> CompletableFuture<V> thenCombine(CompletionStage<? extends U> other,
  3. BiFunction<? super T,? super U,? extends V> fn)
  4. //两个任务异步完成,fn再依赖消费两个任务的结果,有返回值
  5. public <U,V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other,
  6. BiFunction<? super T,? super U,? extends V> fn)
  7. //两个任务异步完成(第二个任务用指定线程池执行),fn再依赖消费两个任务的结果,有返回值
  8. public <U,V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other,
  9. BiFunction<? super T,? super U,? extends V> fn, Executor executor)
  • 使用示例
  1. //第一个异步任务,常量任务
  2. CompletableFuture<String> first = CompletableFuture.completedFuture("hello world");
  3. ExecutorService executor = Executors.newSingleThreadExecutor();
  4. CompletableFuture<String> future = CompletableFuture
  5. //第二个异步任务
  6. .supplyAsync(() -> "hello siting", executor)
  7. // (w, s) -> System.out.println(s) 是第三个任务
  8. .thenCombineAsync(first, (s, w) -> {
  9. System.out.println(s);
  10. return "OK";
  11. }, executor);
  12. System.out.println(future.join());
  13. executor.shutdown();
  14. --------输出结果--------
  15. hello siting
  16. OK

4 线程并行执行,谁先执行完则谁触发下一任务(二者选其最快)

上一个任务或者other任务完成, 运行action,不依赖前一任务的结果,无返回值

  1. public CompletableFuture<Void> runAfterEither(CompletionStage<?> other, Runnable action)
  2. public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action)
  3. public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other,
  4. Runnable action, Executor executor)
  • 使用示例
  1. //第一个异步任务,休眠1秒,保证最晚执行晚
  2. CompletableFuture<String> first = CompletableFuture.supplyAsync(()->{
  3. try{ Thread.sleep(1000); }catch (Exception e){}
  4. System.out.println("hello world");
  5. return "hello world";
  6. });
  7. ExecutorService executor = Executors.newSingleThreadExecutor();
  8. CompletableFuture<Void> future = CompletableFuture
  9. //第二个异步任务
  10. .supplyAsync(() ->{
  11. System.out.println("hello siting");
  12. return "hello siting";
  13. } , executor)
  14. //() -> System.out.println("OK") 是第三个任务
  15. .runAfterEitherAsync(first, () -> System.out.println("OK") , executor);
  16. executor.shutdown();
  17. --------输出结果--------
  18. hello siting
  19. OK

上一个任务或者other任务完成, 运行action,依赖最先完成任务的结果,无返回值

  1. public CompletableFuture<Void> acceptEither(CompletionStage<? extends T> other,
  2. Consumer<? super T> action)
  3. public CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other,
  4. Consumer<? super T> action, Executor executor)
  5. public CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other,
  6. Consumer<? super T> action, Executor executor)
  • 使用示例
  1. //第一个异步任务,休眠1秒,保证最晚执行晚
  2. CompletableFuture<String> first = CompletableFuture.supplyAsync(()->{
  3. try{ Thread.sleep(1000); }catch (Exception e){}
  4. return "hello world";
  5. });
  6. ExecutorService executor = Executors.newSingleThreadExecutor();
  7. CompletableFuture<Void> future = CompletableFuture
  8. //第二个异步任务
  9. .supplyAsync(() -> "hello siting", executor)
  10. // data -> System.out.println(data) 是第三个任务
  11. .acceptEitherAsync(first, data -> System.out.println(data) , executor);
  12. executor.shutdown();
  13. --------输出结果--------
  14. hello siting

上一个任务或者other任务完成, 运行fn,依赖最先完成任务的结果,有返回值

  1. public <U> CompletableFuture<U> applyToEither(CompletionStage<? extends T> other,
  2. Function<? super T, U> fn)
  3. public <U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other,
  4. Function<? super T, U> fn)
  5. public <U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other,
  6. Function<? super T, U> fn, Executor executor)
  • 使用示例
  1. //第一个异步任务,休眠1秒,保证最晚执行晚
  2. CompletableFuture<String> first = CompletableFuture.supplyAsync(()->{
  3. try{ Thread.sleep(1000); }catch (Exception e){}
  4. return "hello world";
  5. });
  6. ExecutorService executor = Executors.newSingleThreadExecutor();
  7. CompletableFuture<String> future = CompletableFuture
  8. //第二个异步任务
  9. .supplyAsync(() -> "hello siting", executor)
  10. // data -> System.out.println(data) 是第三个任务
  11. .applyToEitherAsync(first, data -> {
  12. System.out.println(data);
  13. return "OK";
  14. } , executor);
  15. System.out.println(future);
  16. executor.shutdown();
  17. --------输出结果--------
  18. hello siting
  19. OK

5 处理任务结果或者异常

exceptionally-处理异常

  1. public CompletableFuture<T> exceptionally(Function<Throwable, ? extends T> fn)
  • 如果之前的处理环节有异常问题,则会触发exceptionally的调用相当于 try...catch
  • 使用示例
  1. CompletableFuture<Integer> first = CompletableFuture
  2. .supplyAsync(() -> {
  3. if (true) {
  4. throw new RuntimeException("main error!");
  5. }
  6. return "hello world";
  7. })
  8. .thenApply(data -> 1)
  9. .exceptionally(e -> {
  10. e.printStackTrace(); // 异常捕捉处理,前面两个处理环节的日常都能捕获
  11. return 0;
  12. });

handle-任务完成或者异常时运行fn,返回值为fn的返回

  • 相比exceptionally而言,即可处理上一环节的异常也可以处理其正常返回值
  1. public <U> CompletableFuture<U> handle(BiFunction<? super T, Throwable, ? extends U> fn)
  2. public <U> CompletableFuture<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn)
  3. public <U> CompletableFuture<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn,
  4. Executor executor)
  • 使用示例
  1. CompletableFuture<Integer> first = CompletableFuture
  2. .supplyAsync(() -> {
  3. if (true) { throw new RuntimeException("main error!"); }
  4. return "hello world";
  5. })
  6. .thenApply(data -> 1)
  7. .handleAsync((data,e) -> {
  8. e.printStackTrace(); // 异常捕捉处理
  9. return data;
  10. });
  11. System.out.println(first.join());
  12. --------输出结果--------
  13. java.util.concurrent.CompletionException: java.lang.RuntimeException: main error!
  14. ... 5 more
  15. null

whenComplete-任务完成或者异常时运行action,有返回值

  • whenComplete与handle的区别在于,它不参与返回结果的处理,把它当成监听器即可
  • 即使异常被处理,在CompletableFuture外层,异常也会再次复现
  • 使用whenCompleteAsync时,返回结果则需要考虑多线程操作问题,毕竟会出现两个线程同时操作一个结果
  1. public CompletableFuture<T> whenComplete(BiConsumer<? super T, ? super Throwable> action)
  2. public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action)
  3. public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action,
  4. Executor executor)
  • 使用示例
  1. CompletableFuture<AtomicBoolean> first = CompletableFuture
  2. .supplyAsync(() -> {
  3. if (true) { throw new RuntimeException("main error!"); }
  4. return "hello world";
  5. })
  6. .thenApply(data -> new AtomicBoolean(false))
  7. .whenCompleteAsync((data,e) -> {
  8. //异常捕捉处理, 但是异常还是会在外层复现
  9. System.out.println(e.getMessage());
  10. });
  11. first.join();
  12. --------输出结果--------
  13. java.lang.RuntimeException: main error!
  14. Exception in thread "main" java.util.concurrent.CompletionException: java.lang.RuntimeException: main error!
  15. ... 5 more

6 多个任务的简单组合

  1. public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs)
  2. public static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs)



  • 使用示例
  1. CompletableFuture<Void> future = CompletableFuture
  2. .allOf(CompletableFuture.completedFuture("A"),
  3. CompletableFuture.completedFuture("B"));
  4. //全部任务都需要执行完
  5. future.join();
  6. CompletableFuture<Object> future2 = CompletableFuture
  7. .anyOf(CompletableFuture.completedFuture("C"),
  8. CompletableFuture.completedFuture("D"));
  9. //其中一个任务行完即可
  10. future2.join();

8 取消执行线程任务

  1. // mayInterruptIfRunning 无影响;如果任务未完成,则返回异常
  2. public boolean cancel(boolean mayInterruptIfRunning)
  3. //任务是否取消
  4. public boolean isCancelled()
  • 使用示例
  1. CompletableFuture<Integer> future = CompletableFuture
  2. .supplyAsync(() -> {
  3. try { Thread.sleep(1000); } catch (Exception e) { }
  4. return "hello world";
  5. })
  6. .thenApply(data -> 1);
  7. System.out.println("任务取消前:" + future.isCancelled());
  8. // 如果任务未完成,则返回异常,需要对使用exceptionally,handle 对结果处理
  9. future.cancel(true);
  10. System.out.println("任务取消后:" + future.isCancelled());
  11. future = future.exceptionally(e -> {
  12. e.printStackTrace();
  13. return 0;
  14. });
  15. System.out.println(future.join());
  16. --------输出结果--------
  17. 任务取消前:false
  18. 任务取消后:true
  19. java.util.concurrent.CancellationException
  20. at java.util.concurrent.CompletableFuture.cancel(CompletableFuture.java:2276)
  21. at Test.main(Test.java:25)
  22. 0

9 任务的获取和完成与否判断

  1. // 任务是否执行完成
  2. public boolean isDone()
  3. //阻塞等待 获取返回值
  4. public T join()
  5. // 阻塞等待 获取返回值,区别是get需要返回受检异常
  6. public T get()
  7. //等待阻塞一段时间,并获取返回值
  8. public T get(long timeout, TimeUnit unit)
  9. //未完成则返回指定value
  10. public T getNow(T valueIfAbsent)
  11. //未完成,使用value作为任务执行的结果,任务结束。需要future.get获取
  12. public boolean complete(T value)
  13. //未完成,则是异常调用,返回异常结果,任务结束
  14. public boolean completeExceptionally(Throwable ex)
  15. //判断任务是否因发生异常结束的
  16. public boolean isCompletedExceptionally()
  17. //强制地将返回值设置为value,无论该之前任务是否完成;类似complete
  18. public void obtrudeValue(T value)
  19. //强制地让异常抛出,异常返回,无论该之前任务是否完成;类似completeExceptionally
  20. public void obtrudeException(Throwable ex)
  • 使用示例
  1. CompletableFuture<Integer> future = CompletableFuture
  2. .supplyAsync(() -> {
  3. try { Thread.sleep(1000); } catch (Exception e) { }
  4. return "hello world";
  5. })
  6. .thenApply(data -> 1);
  7. System.out.println("任务完成前:" + future.isDone());
  8. future.complete(10);
  9. System.out.println("任务完成后:" + future.join());
  10. --------输出结果--------
  11. 任务完成前:false
  12. 任务完成后:10

欢迎指正文中错误

基础篇:异步编程不会?我教你啊!CompeletableFuture的更多相关文章

  1. C#基础系列——异步编程初探:async和await

    前言:前面有篇从应用层面上面介绍了下多线程的几种用法,有博友就说到了async, await等新语法.确实,没有异步的多线程是单调的.乏味的,async和await是出现在C#5.0之后,它的出现给了 ...

  2. java基础篇---网络编程(TCP程序设计)

    TCP程序设计 在Java中使用Socket(即套接字)完成TCP程序的开发,使用此类可以方便的建立可靠地,双向的,持续的,点对点的通讯连接. 在Socket的程序开发中,服务器端使用serverSo ...

  3. java基础篇---网络编程(IP与URL)

    一:IP与InetAddress 在Java中支持网络通讯程序的开发,主要提供了两种通讯协议:TCP协议,UDP协议 可靠地连接传输,使用三方握手的方式完成通讯 不可靠的连接传输,传输的时候接受方不一 ...

  4. Scala基础篇-函数式编程的重要特性

    1.纯函数 表示函数无副作用(状态变化). 2.引用透明性 表示对相同输入,总是得到相同输出. 3.函数是一等公民 函数与变量.对象.类是同一等级.表示可以把函数当做参数传入另一个函数,或者作为函数的 ...

  5. java基础篇---网络编程(UDP程序设计)

    UDP程序设计 在TCP的索引操作都必须建立可靠地连接,这样一来肯定会浪费大量的系统性能,为了减少这种开销,在网络中又提供了另外一种传输协议---UDP,不可靠的连接,这种协议在各个聊天工具中被广泛的 ...

  6. Java 基础篇之编程基础

    基本数据类型 java 是强类型语言,在 java 中存储的数据都是有类型的,而且必须在编译时就确定其类型. 基本数据类型变量存储的是数据本身,而引用类型变量存的是数据的空间地址. 基本类型转换 自动 ...

  7. 温故知新,CSharp遇见异步编程(Async/Await),聊聊异步编程最佳做法

    什么是异步编程(Async/Await) Async/Await本质上是通过编译器实现的语法糖,它让我们能够轻松的写出简洁.易懂.易维护的异步代码. Async/Await是C# 5引入的关键字,用以 ...

  8. 深入浅出node(4) 异步编程

    一)函数式编程基础 二)异步编程的优势和难点 2.1 优势 2.2 难点 2.2.1 异常处理 2.2.2 函数嵌套过深 2.2.3 阻塞 2.2.4 多线程编程 2.2.5 异步转同步 三)异步编程 ...

  9. Promise和异步编程

    前面的话 JS有很多强大的功能,其中一个是它可以轻松地搞定异步编程.作为一门为Web而生的语言,它从一开始就需要能够响应异步的用户交互,如点击和按键操作等.Node.js用回调函数代替了事件,使异步编 ...

  10. 【读书笔记】【深入理解ES6】#11-Promise与异步编程

    异步编程的背景知识 JavaScript 引擎是基于单线程(Single-threaded)实际循环的概念构建的,同一时刻只允许一个代码块在执行. 所以需要跟踪即将运行的代码,那些代码被放在一个任务队 ...

随机推荐

  1. Java_流相关

    java.io包中重要的5个类3个接口 类名 说明 File 文件类 InputStream 字节流输入 OutputStream 字节流输出 Reader 字符输入流 Writer 字符输出流 Cl ...

  2. 通过阿里镜像网站制作iso文件安装CentOS6.9

    基于网络安装 创建kickstart文件的方式: 1.复制模板/root/anaconda-ks.cfg,而后使用vim编辑配置 2.使用system-config-kickstart来生成,建议使用 ...

  3. 全排列算法--递归实现(Java)

    求一个n阶行列式,一个比较简单的方法就是使用全排列的方法,那么简述以下全排列算法的递归实现. 首先举一个简单的例子说明算法的原理,既然是递归,首先说明一下出口条件.以[1, 2]为例 首先展示一下主要 ...

  4. 关于ubuntu出现的一些问题的解决方法

    1. (1)现象: dpkg: 处理软件包 linux-image-4.15.0-36-generic (--configure)时出错: 子进程 已安装 post-installation 脚本 返 ...

  5. 1.1 Prism安装

    Prism框架有很多安装包,即便用了很长一段时间,也可能会不知道如何安装框架.细心分析包的依赖关系,发现所有包均依赖与依赖注入扩展插件,以使用Unity为例,Prism.Unity依赖Prism.Wp ...

  6. 我叫Mongo,干了「查询终结篇」,值得您拥有

    这是mongo第三篇"查终结篇",后续会连续更新5篇 mongodb的文章总结上会有一系列的文章,顺序是先学会怎么用,在学会怎么用好,戒急戒躁,循序渐进,跟着我一起来探索交流. 通 ...

  7. div 内元素的垂直居中

    小主今天偷点懒利用上班时间整理一下 div 内元素的居中(不论垂直还是水平通用)问题的解决方法: 本文的中心是利用 css 中的 display属性:通常的方便的是使用 Flex/Grid 属性,今天 ...

  8. ado.net 连接数据库

    一.用SqlConnection连接SQL Server 1..加入命名空间 using System.Data.SqlClient; 2.连接数据库 SqlConnection myConnecti ...

  9. python_socket登陆验证_明文

    client.py import socket import struct sk=socket.socket() sk.connect(('127.0.0.1',9005)) while True: ...

  10. linux命令使用 cut/sort/uniq

    我记得之前去XX网面试的那个面试题是这样的:有个apache.log 文件文本内容如下:======================[niewj@centSvr ~]$ cat apache.log  ...