I hereby claim:
- I am stream1984 on github.
- I am stream_iori (https://keybase.io/stream_iori) on keybase.
- I have a public key ASCB_U5y0eoJWlbBiKf8bm1bMYP36oEhFXibeIZdCLAvxwo
To claim this, I am signing this object:
I hereby claim:
To claim this, I am signing this object:
| import co.paralleluniverse.fibers.Fiber; | |
| import co.paralleluniverse.fibers.SuspendExecution; | |
| import co.paralleluniverse.strands.channels.Channel; | |
| import co.paralleluniverse.strands.channels.Channels; | |
| /** | |
| * Created by stream. | |
| */ | |
| public class Skynet extends Fiber<Void> { |
| public interface AsyncInterface { | |
| //定义一个同步的接口,返回值类型是String | |
| String methodWithReturn(String address, long arg); | |
| } | |
| //接口实现部分 | |
| String methodWithReturn(String address, long arg) { |
| //返回一个Obserable,侦听发给heat-sensor的信息 | |
| Observable<Double> observable = vertx.eventBus(). | |
| <Double>consumer("heat-sensor"). | |
| bodyStream(). | |
| toObservable(); | |
| //这个obserable持续1秒钟,并对数据进行map reduce操作 | |
| observable. | |
| buffer(1, TimeUnit.SECONDS). | |
| map(samples -> samples. |
| final AtomicInteger counter = new AtomicInteger(0); | |
| vertx.eventBus().send("address1", "args", event -> { | |
| //some logic | |
| counter.incrementAndGet(); | |
| next(); | |
| }); | |
| vertx.eventBus().send("address2", "args", event -> { | |
| //some logic | |
| counter.incrementAndGet(); |
| //Optional | |
| Optional<String> fullName = Optional.ofNullable(null); | |
| System.out.println("Full Name is set? " + fullName.isPresent()); //Full Name is set? false | |
| System.out.println("Full Name: " + fullName.orElseGet(() -> "[none]")); //Full Name: [none] | |
| //map()方法转化Optional当前的值并且返回一个新的Optional实例 | |
| System.out.println(fullName.map(s -> "Hey " + s + "!").orElse("Hey Stranger!")); //Hey Stranger! | |
| //方法引用 | |
| public static class Car { | |
| public static Car create(final Supplier<Car> supplier) { | |
| return supplier.get(); | |
| } | |
| public static void collide(final Car car) { | |
| System.out.println("Collided " + car.toString()); | |
| } |
| interface Product { | |
| //默认方法 | |
| default String notRequired() { | |
| return "Default implementation"; | |
| } | |
| String required(); | |
| //静态方法 |
| //简单的Stream | |
| Arrays.asList(1, 2, 3, 4).stream().filter(e -> e % 2 == 0).forEach(e -> System.out.println(e)); | |
| // | |
| List<Integer> list = new ArrayList<>(); | |
| list.add(1); | |
| list.add(2); | |
| list.add(2); |
| //之前使用匿名类 | |
| new Thread(new Runnable() { | |
| @Override | |
| public void run() { | |
| System.out.println("Hello world !"); | |
| } | |
| }).start(); | |
| //现在使用lambda,无参数 | |
| new Thread(() -> System.out.println("Hello world !")).start(); |