jdk7 新特性测试-1 -(demo来源于网上)

时间:2022-01-27 23:26:30
package jdk7;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Jdk7Test {

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
System.out.println(switchString("Monday"));

//抛出异常
catchTest();
}

//Switch语句支持string类型  
public static String switchString(String dayOfWeekArg){
String typeOfDay; 
        switch (dayOfWeekArg) { 
            case "Monday": 
                typeOfDay = "Start of work week"; 
                break; 
            case "Tuesday": 
            case "Wednesday": 
            case "Thursday": 
                typeOfDay = "Midweek"; 
                break; 
            case "Friday": 
                typeOfDay = "End of work week"; 
                break; 
            case "Saturday": 
            case "Sunday": 
                typeOfDay = "Weekend"; 
                break; 
            default: 
                throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg); 
        } 
        return typeOfDay; 
}

//1.4 catch
public static void catchTest( ) throws Exception {
String zipFileName = "";
try { 
    System.out.println("error");
    if(true){
    throw new IOException();
    }else{
    throw new SQLException();
    }
} catch (IOException|SQLException ex) { 
    throw ex; 

}

//1.6 泛型实例的创建可以通过类型推断来简化 可以去掉后面new部分的泛型类型,只用<>就可以了
public static void listTest( ) throws Exception {
List<String> list = new ArrayList<>(); 
list.add("A"); 
}


}

fork-join

package jdk7;

import java.util.Date;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import java.util.concurrent.RecursiveTask;



//最大的增强,充分利用多核特性,将大问题分解成各个子问题,
//由多个cpu可以同时解决多个子问题,最后合并结果,继承RecursiveTask,
//实现compute方法,然后调用fork计算,最后用join合并结果

//算法很简单,在100个数以内的我们直接迭代计算和值。否则采用Fork/Join框架分治计算。
public  class PlusPlus extends RecursiveTask<Long>{ 
    private static final int THRESHOLE=100; 
    private int start; 
    private int end; 
    public PlusPlus(int start,int end){ 
        this.start=start; 
        this.end=end; 
    } 
     
    @Override 
    protected Long compute() { 
        // TODO Auto-generated method stub 
    Long sum=0l; 
        if(end-start<THRESHOLE){ 
            for(int i=start;i<=end;i++){ 
                sum+=i; 
            } 
        }else{ 
            int middle=(start+end)/2; 
            PlusPlus left=new PlusPlus(start, middle); 
            PlusPlus right=new PlusPlus(middle+1, end); 
             
            left.fork(); 
            right.fork(); 
            sum=left.join()+right.join(); 
        } 
        return sum; 
    } 
   
    public Long sumall(){
    long sum=0;
    for(int i=start;i<=end;i++){
    sum = sum+ i;
    }
    return sum;
    }
     
    public static void main(String args[]) throws InterruptedException, ExecutionException{ 
    long t1 = new Date().getTime();
   
        ForkJoinPool pool=new ForkJoinPool(); 
        Future<Long> result=pool.submit(new PlusPlus(1, 800000)); 
       
        long t2 = new Date().getTime();
        long sum = new PlusPlus(1, 800000).sumall(); 
        long t3 = new Date().getTime();
        System.out.println(result.get()+"耗时:"+(t2-t1)); 
        System.out.println(sum+"耗时:"+(t3-t2)); 
//        System.err.println(10001*5000); 
    } 
     



320000400000耗时:2
320000400000耗时:3

注:
使用fork 有个局限   800000 变成 1000000时候 出现  OutOfMemory
而后面一种方式 就不会出现这个错误