知识积累

1. JDK版本特性

JDK1.5新特性:

  1. 自动装箱与拆箱
  2. for-each

JDK 1.7 新特性

  1. switch中可以使用字串了

    String s = "test";
    switch (s) {
        case "test" :
               System.out.println("test");
        case "test1" :
               System.out.println("test1");
                break ;
        default :
              System.out.println("break");
             break ;
  2. “<>”这个玩意儿的运用List tempList = new ArrayList<>(); 即泛型实例化类型自动推断。

  3. 自定义自动关闭类

    public interface AutoCloseable {   
        void close() throws Exception;
    }

    java7开始,InputStream,OutputStream实现了自动关闭AutoClosable接口

  4. jdk7之前,你必须用try{}finally{}在try内使用资源,在finally中关闭资源,不管try中的代码是否正常退出或者异常退出。jdk7之后,你可以不必要写finally语句来关闭资源,只要你在try()的括号内部定义要使用的资源。请看例子:

    import java.io.*;
    // Copy from one file to another file character by character.
    // Pre-JDK 7 requires you to close the resources using a finally block.
    public class FileCopyPreJDK7 {
    public static void main(String[] args) {
      BufferedReader in = null;
      BufferedWriter out = null;
      try {
         in  = new BufferedReader(new FileReader("in.txt"));
         out = new BufferedWriter(new FileWriter("out.txt"));
         int charRead;
         while ((charRead = in.read()) != -1) {
            System.out.printf("%c ", (char)charRead);
            out.write(charRead);
         }
      } catch (IOException ex) {
         ex.printStackTrace();
      } finally {            // always close the streams
         try {
            if (in != null) in.close();
            if (out != null) out.close();
         } catch (IOException ex) {
            ex.printStackTrace();
         }
      }
    
      try {
         in.read();   // Trigger IOException: Stream closed
      } catch (IOException ex) {
         ex.printStackTrace();
      }
    }
    }

    jdk7之后

    import java.io.*;
    // Copy from one file to another file character by character.
    // JDK 7 has a try-with-resources statement, which ensures that
    // each resource opened in try() is closed at the end of the statement.
    public class FileCopyJDK7 {
    public static void main(String[] args) {
      try (BufferedReader in  = new BufferedReader(new FileReader("in.txt"));
           BufferedWriter out = new BufferedWriter(new FileWriter("out.txt"))) {
         int charRead;
         while ((charRead = in.read()) != -1) {
            System.out.printf("%c ", (char)charRead);
            out.write(charRead);
         }
      } catch (IOException ex) {
         ex.printStackTrace();
      }
    }
    }
  5. 在try catch异常扑捉中,一个catch可以写多个异常类型,用”|”隔开
    jdk7之前:

    try {
    ......
    } catch(ClassNotFoundException ex) {
    ex.printStackTrace();
    } catch(SQLException ex) {
    ex.printStackTrace();
    }

    dk7例子如下

    try {
       ......
    } catch(ClassNotFoundException | SQLException ex) {
       ex.printStackTrace();
    }

更多参考:
http://www.cnblogs.com/dassmeta/p/5755938.html


2. 多线程

2.1 场景题:三个线程同时start,用什么方法可以保证线程的执行顺序,线程1执行完线程2执行,线程2执行完线程3执行?

  1. 分析1(不可行):已经start( )了的多个线程,再调用join( ),无效。因为start后已经开始抢占式了。如下无效:

    thread1.start();              
    thread2.start();              
    thread3.start();             
    thread1.join(); //无效 
    thread2.join(); //无效
    thread3.join(); //无效         
  2. 分析2(不符合要求):start一个线程后调用join,然后再start其他线程。可行,但本题要求同时启动,略微跑题。

    thread1.start();              
    thread1.join(); 
    thread2.start();              
    thread2.join(); 
    thread3.start(); 
    thread3.join();  
  3. 分析3(符合):在线程中的关键方法run中添加代码,在一个线程的target中用join()方法确保其他线程执行完。如下:

    public static void main(String[] args) {  
    
       final Thread t1=new Thread(new Runnable(){        
            @Override
            public void run() {
                  System.out.println("t1");         
            }
       });
    
      final Thread t2 = new Thread(new Runnable() {     
            @Override
            public void run() {
                try{
                    t1.join();
                }catch(InterruptedException e){
    
                }
                System.out.println("t2");
    
            }
    
      });
      final Thread t3=new Thread(new Runnable(){
        @Override
        public void run() {
            try {
                t2.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("t3");
    
        }
      });
    
      t1.start();
      t2.start();
      t3.start();
    
    }   

    执行的结果一定为:

    t1
    t2
    t3

  4. 分析4(CountDownLatch,可行)。为线程类传入上一个线程的countDwon,和本线程的countDown。在run中执行业务前,先上一个线程计数器.await( ),然后执行业务,之后本线程计数器.countDown( )。
    详情见本人博客:
    http://blog.csdn.net/answer100answer/article/details/77658097

3.集合

3.1 hashmap的原理

java7采用数组+链表
java8采用数组+链表+红黑树的结构(当链表长度大于8时转红黑树)。

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值