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

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
而后面一种方式 就不会出现这个错误
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值