`

jdk5.0 特性

    博客分类:
  • Java
阅读更多
package jdk.jdk5;  
  
import java.util.Date;  
import java.util.Iterator;  
import java.util.LinkedList;  
import java.util.List;  
import java.util.Scanner;  
import java.util.concurrent.Callable;  
import java.util.concurrent.ExecutionException;  
import java.util.concurrent.Executors;  
import java.util.concurrent.ScheduledExecutorService;  
import java.util.concurrent.ScheduledFuture;  
import java.util.concurrent.TimeUnit;  
/************************************** 
 * 静态导入(static import) 
 *静态导入可以使被导入类的所有静态变量和静态方法在当前类直接可见,使用这些静态成员无需再给出他们的类名. 
 *import static package.className.staticField/staticMethod; 
**********************************/  
  
public class JDK5 {  
  
    /*************泛型(Generic)******************/  
    /************ 
     * 增强了 java 的类型安全,可以在编译期间对容器内的对象进行类型检查,在运行期不必进行类型的转换。 
     * 而在 j2se5 之前必须在运行期动态进行容器内对象的检查及转换  
     ***********/  
      
    //1.<ItemType>  
    @SuppressWarnings("serial")  
    public class ElementList<Element> extends LinkedList<Element> {  
        public void swap(int i, int j){   
              Element temp = this.get(i);   
              this.set(i, this.get(j));   
              this.set(j, temp);   
        }   
    }  
  
    //2.<T, V>  
    @SuppressWarnings("serial")  
    public class GenericList<T> extends LinkedList<T> {  
        public void swap(int i, int j){   
              T temp = this.get(i);   
              this.set(i, this.get(j));   
              this.set(j, temp);   
        }   
    }  
  
    //3.受限泛型是指类型参数的取值范围是受到限制的 . extends 关键字不仅仅可以用来声明类的继承关系 ,   
    //也可以用来声明类型参数 (type parameter) 的受限关系  
    public class LimitedGeneric<T extends Number> {   
        public double getDoubleValue(T obj){   
            return obj.doubleValue();   
        }   
    }  
  
    //4.泛型的通配符 "?"   
    public static void print(List<?> list){  
        String str="";  
        for(Iterator it=list.iterator();it.hasNext();){  
            str+=it.next();  
        }  
        System.out.println(str);  
    }   
  
    /*********************增加循环*****************************/  
    public static void forCycle(){  
        LinkedList<String> list = new LinkedList<String>();            
        list.add("Hi");   
        list.add("everyone!");   
        for (String s : list){  
             System.out.println(s);   
        }  
    }  
    public static void printMap(Map map) {  
        for (Object key : map.keySet()) {  
            System.out.println(key + ":" + map.get(key));  
        }  
//不用for时  
        Iterator it = map.entrySet().iterator();  
        while (it.hasNext()) {  
            Map.Entry entry = (Map.Entry) it.next();  
            Object key = entry.getKey();  
            Object value = entry.getValue();  
            System.out.println(key + ":" +value);  
        }  
    }  
      
    /*********************可变参数 (Variable Arguments) ******/  
    public static int add(int ... args){   
        int total = 0;      
        for (int i = 0; i < args.length; i++){  
              total += args[i];  
        }  
        return total;   
    }   
  
    /********************枚举类(Enumeration Classes)***********/  
    public enum Colors {Red, Yellow, Blue, Orange, Green, Purple, Brown, Black}   
    public enum Size{   
       SMALL("S"), MEDIUM("M"), LARGE("L"), EXTRA_LARGE("XL");   
       private Size(String abbreviation) { this.abbreviation = abbreviation; } //参数为缩写值  
       public String getAbbreviation() { return abbreviation; }   
       private String abbreviation;   
    }   
    public enum Operation {  
        PLUS   { double eval(double x, double y) { return x + y; } },  
        MINUS  { double eval(double x, double y) { return x - y; } },  
        TIMES  { double eval(double x, double y) { return x * y; } },  
        DIVIDE { double eval(double x, double y) { return x / y; } };  
        // Do arithmetic op represented by this constant  
        abstract double eval(double x, double y);  
    }  
  
    public static void testEnumType(){  
        System.out.println(Colors.Red);  
        //在JDK5.0之前我们只能通过JOptionPane.showInputDialog进行输入,  
        //但在5.0中我们可以通过类Scanner在控制台进行输入操作  
        Scanner in = new Scanner(System.in);    
        System.out.print("Enter a size: (SMALL, MEDIUM, LARGE, EXTRA_LARGE) ");   
        String input = in.next().toUpperCase();   
        Size size = Enum.valueOf(Size.class, input);   
        System.out.println("size=" + size);   
        System.out.println("abbreviation=" + size.getAbbreviation());   
        if (size == Size.EXTRA_LARGE){  
            System.out.println("Good job--you paid attention to the _.");   
        }  
        System.out.println(Operation.PLUS.eval(5.0, 1.25));  
     }   
  
    /****** 
     * 在JDK5.0中引入了StringBuilder类,该类的方法不是同步(synchronized)的, 
     * 这使得它比StringBuffer更加轻量级和有效。  
     *****/  
    public StringBuilder hql=new StringBuilder();  
      
    /*********************格式化I/O(Formatted I/O) ************/  
    public static void formatIO(){  
        int a = 150000, b = 10;   
        float c = 5.0101f, d = 3.14f;   
        System.out.printf("%4d %4d%n", a, b);   
        System.out.printf("%x %x%n", a, b); //十六进制  
        System.out.printf("%3.2f %1.1f%n", c, d);   
        System.out.printf("%1.3e %1.3e%n", c, d*100);   
        System.out.println(new Formatter().format("%08d %s \n", 5,"string").toString());  
  
    }  
      
    /*************java.util.concurrent 线程池*****************/  
    public class TestScheduledThread{  
        /**ScheduledExecutorService提供了按时间安排执行任务的功能,它提供的方法主要有: 
            schedule(task,initDelay):安排所提交的Callable或Runnable任务在initDelay指定的时间后执行。  
            scheduleAtFixedRate():安排所提交的Runnable任务按指定的间隔重复执行  
            scheduleWithFixedDelay():安排所提交的Runnable任务在每次执行完后,等待delay所指定的时间后重复执行。  
        **/  
        @SuppressWarnings("unchecked")  
        public void runScheduledThread() throws InterruptedException,ExecutionException{  
            //初始化一个ScheduledExecutorService对象,这个对象的线程池大小为2。  
            ScheduledExecutorService service=Executors.newScheduledThreadPool(2);  
            Runnable task1=new Runnable(){  
                int count=0;  
                public void run(){  
                   System.out.println(new Date()+" beep"+(++count));  
                }  
            };  
            //直接执行,以后间隔1秒执行一次  
            final ScheduledFuture future1=service.scheduleAtFixedRate(task1,0,1,TimeUnit.SECONDS);  
            final ScheduledFuture future2=service.scheduleAtFixedRate(new Runnable(){  
                public void run(){  
                    System.out.println("future2 runing!");  
                }  
            },1,2,TimeUnit.SECONDS);  
            ScheduledFuture future3=service.schedule(new Callable(){  
                 public String call(){  
                         future1.cancel(true);//取消任务  
                         future2.cancel(true);  
                         return "taskcancelled!";  
                 }  
            },10,TimeUnit.SECONDS);  
            System.out.println(future3.get());  
            service.shutdown();//关闭服务  
        }  
    }  
      
    public static void main(String[] args){  
        JDK5 jdk5 = new JDK5();  
          
/*      JDK5.GenericList<String> list= jdk5.new GenericList<String>(); 
        list.add("hello!"); 
        list.add("world!"); 
        print(list); 
        list.swap(0, 1); 
        print(list); 
         
        JDK5.LimitedGeneric<Integer> number = jdk5.new LimitedGeneric<Integer>(); 
        System.out.println(number.getDoubleValue(2)); 
         
        forCycle(); 
        System.out.println(add(1,2,3,4,5)); 
        formatIO(); 
        testEnumType(); 
*/    
        JDK5.TestScheduledThread test = jdk5.new TestScheduledThread();  
        try {  
            test.runScheduledThread();  
        } catch (InterruptedException e) {  
            e.printStackTrace();  
        } catch (ExecutionException e) {  
            e.printStackTrace();  
        }  
    }  
}  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics