New features Java8 4 fork / join class and Optional

After java8 parallel () and sequential () is switched in parallel with the parallel flow stream
package Stream;

import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;
import java.util.stream.LongStream;

/**
 * @autour zdc
 * @create 2020-03-24-23:53
 */
public class ForkJoinCaculate extends RecursiveTask<Long> {

    private static final long serialVersionUID =1222222L;
    private Long start;
    private long end;
    private static long THESHOLD = 10000L;

    public ForkJoinCaculate(Long start, long end) {
        this.start = start;
        this.end = end;
    }

    @Override
    protected Long compute() {
        long length = end-start;
        if (length<THESHOLD){
            long sum = 0;
            for (long i = start; i <=end ; i++) {
                sum+=i;
            }
            return sum;
        }
        else {
            Long mid = (start+end)/2;
            ForkJoinCaculate left = new ForkJoinCaculate(start, mid);
            left.fork();
            ForkJoinCaculate right = new ForkJoinCaculate(mid + 1, end);
            right.fork();
            return left.join()+right.join();
        }
    }

    public static void main(String[] args) {
        Instant start = Instant.now();

        ForkJoinPool pool = new ForkJoinPool();
        ForkJoinTask<Long> task = new ForkJoinCaculate(0l,1000000000l);
        Long sum = pool.invoke(task);
        System.out.println(sum);

        Instant end = Instant.now();

        System.out.println(Duration.between(start,end).toMillis());

        Instant start1 = Instant.now();
        sum=0l;
        for (Long i = 0l; i <=1000000000l; i++) {
            sum+=i;
        }
        System.out.println(sum);

        END1 the Instant = Instant.now (); 
        System.out.println (Duration.between (Start1, END1) .toMillis ()); 

        // Parallel () and sequential () is switched in parallel with the parallel flow stream after java8 
        Long SUM1 = LongStream .rangeClosed (0, 1000000000) .reduce (0 , Long :: SUM);
         Long . LongStream.rangeClosed SUM2 = (0, 1000000000) .parallel () the reduce (0 , Long :: SUM);
         Long sum3 = LongStream.rangeClosed . (0, 1000000000) .sequential () the reduce (0 , Long :: SUM); 
        System.out.println (SUM2); 

    } 

}

Optional

@Test
     public  void Test3 () { 

        // Optional The vessel is able to quickly lock NullPointerException NullPointerException avoid the trouble 


        Optional The <the Person> OP = Optional.of ( new new the Person ( "ZZZ", 33 is , 445D));
         // Optional the <the Person> Optional.of OP1 = (null); // reported abnormal 

        Optional the <the Person> Optional.empty OP3 = (); // create an empty instance
         // System.out.println (op3.get () ); // reported abnormal 

        // pass over null null construct an object instance is passed over Construction of the complex and the empty 
        Optional the <the Person> OP4 = Optional.ofNullable ( null ); 
        Optional the <the Person> OP5 = Optional.ofNullable ( new newThe Person ( "ZZ", 22,11.3 )); 


        IF (op3.isPresent ()) { 
            System.out.println ( "op3w not empty" ); 
        } the else { 
            System.out.println ( "nulll" ); 
        } 

        // OP3 to return null value given orElse (T T) 
        the Person ZZZ = op3.orElse ( new new the Person ( "ZZZ", 22 is, 22.2 )); 

        // orElseGet (Supplier s) to the return value of s 
        op3. orElseGet (the Person :: new new ); // return a null constructor argument because Supplier interfaces 


        // Map process if its value 
        Optional The <String> op5.map S = (P -> p.getName ()); 

        / / flatMap return Optional
        Optional<String> s1 = op5.flatMap(p -> Optional.of(p.getName()));
    }

Solve null pointer exception:

package Stream;

/**
 * @autour zdc
 * @create 2020-03-25-01:00
 */
public class Dog {
    private String ball;

    public Dog() {
    }

    public Dog(String ball) {
        this.ball = ball;
    }

    public String getBall() {
        return ball;
    }

    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.getBall();
        //发生空指针异常
        System.out.println(dog.getBall().length());
        //解决方法
        if(dog!=null){
            String ball = dog.getBall();
            if(ball!=null)
                System.out.println(ball.length());
        }
    }
}

Optional replaced

package Stream;

import javax.swing.text.html.Option;
import java.util.Optional;

/**
 * @autour zdc
 * @create 2020-03-25-01:00
 */
public class Dog {
    //注意!!!
    private Optional<String> ball= Optional.empty();

    public Dog() {
    }

    public Dog(Optional<String> ball) {
        this.ball = ball;
    }

    public Optional<String> getBall() {
        return ball;
    }

    public static void main(String[] args) {
        Optional<Dog>  dog= Optional.ofNullable(null);
        String s = dog.orElse(new Dog()).getBall().orElse("默认的球");
        System.out.println(s);


    }
}

 

Guess you like

Origin www.cnblogs.com/zdcsmart/p/12563590.html