在实际项目中,io,数据库,网络等等,不可避免会发生未知异常,try catch 可以有效的避免页面崩溃,网上有人说一个页四五个try catch影响效率,这里给出验证:
实例:100个线程,分别循环100次作为实验单位:
package com.example; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicLong; /** * @author xuanyouwu * @email xuanyouwu@163.com * @time 2016-05-06 09:27 */ public class TryCatchStudy { static AtomicLong takeTime; static AtomicLong takeTime1; public static void main(String[] args) throws Exception { ExecutorService executorService = Executors.newCachedThreadPool(); takeTime = new AtomicLong(0); takeTime1 = new AtomicLong(0); for (int j = 0; j <100; j++) { int times = j; executorService.execute(new Runnable() { @Override public void run() { long startTime = System.currentTimeMillis(); for (int i = 0; i <100; i++) { try { execute(); } catch (Exception e) { } /* try { Thread.sleep(100); } catch (Exception e) { }*/ } long endTime = System.currentTimeMillis(); takeTime.addAndGet((endTime - startTime)); if (times == 99) { System.out.println("------->take time:" + takeTime.get()); } } }); } for (int j = 0; j <100; j++) { int times = j; executorService.execute(new Runnable() { @Override public void run() { long startTime1 = System.currentTimeMillis(); for (int i = 0; i <100; i++) { execute(); /* try { Thread.sleep(100); } catch (Exception e) { }*/ } long endTime1 = System.currentTimeMillis(); takeTime1.addAndGet((endTime1 - startTime1)); if (times == 99) { System.out.println("------->take time2:" + takeTime.get()); } } }); } } public static void execute() { int res = (((Integer.MAX_VALUE / 2 / 3 - 1) * 2 / 5) - 10 * 100) / 2; res--; ++res; res = res / 2; } }
运行结果:
------->take time:3
------->take time2:3
结果:没有毫秒级别的差距
开启线程睡眠:
package com.example; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicLong; /** * @author xuanyouwu * @email xuanyouwu@163.com * @time 2016-05-06 09:27 */ public class TryCatchStudy { static AtomicLong takeTime; static AtomicLong takeTime1; public static void main(String[] args) throws Exception { ExecutorService executorService = Executors.newCachedThreadPool(); takeTime = new AtomicLong(0); takeTime1 = new AtomicLong(0); for (int j = 0; j <100; j++) { int times = j; executorService.execute(new Runnable() { @Override public void run() { long startTime = System.currentTimeMillis(); for (int i = 0; i <100; i++) { try { execute(); } catch (Exception e) { } try { Thread.sleep(100); } catch (Exception e) { } } long endTime = System.currentTimeMillis(); takeTime.addAndGet((endTime - startTime)); if (times == 99) { System.out.println("------->take time:" + takeTime.get()); } } }); } for (int j = 0; j <100; j++) { int times = j; executorService.execute(new Runnable() { @Override public void run() { long startTime1 = System.currentTimeMillis(); for (int i = 0; i <100; i++) { execute(); try { Thread.sleep(100); } catch (Exception e) { } } long endTime1 = System.currentTimeMillis(); takeTime1.addAndGet((endTime1 - startTime1)); if (times == 99) { System.out.println("------->take time2:" + takeTime.get()); } } }); } } public static void execute() { int res = (((Integer.MAX_VALUE / 2 / 3 - 1) * 2 / 5) - 10 * 100) / 2; res--; ++res; res = res / 2; } }
------->take time2:601414
------->take time:631501
循环100万次:
package com.example; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicLong; /** * @author xuanyouwu * @email xuanyouwu@163.com * @time 2016-05-06 09:27 */ public class TryCatchStudy { static AtomicLong takeTime; static AtomicLong takeTime1; public static void main(String[] args) throws Exception { ExecutorService executorService = Executors.newCachedThreadPool(); takeTime = new AtomicLong(0); takeTime1 = new AtomicLong(0); for (int j = 0; j <100; j++) { int times = j; executorService.execute(new Runnable() { @Override public void run() { long startTime = System.currentTimeMillis(); for (int i = 0; i <100_0000; i++) { try { execute(); } catch (Exception e) { } } long endTime = System.currentTimeMillis(); takeTime.addAndGet((endTime - startTime)); if (times == 99) { System.out.println("------->take time:" + takeTime.get()); } } }); } for (int j = 0; j <100; j++) { int times = j; executorService.execute(new Runnable() { @Override public void run() { long startTime1 = System.currentTimeMillis(); for (int i = 0; i <100_0000; i++) { execute(); } long endTime1 = System.currentTimeMillis(); takeTime1.addAndGet((endTime1 - startTime1)); if (times == 99) { System.out.println("------->take time2:" + takeTime.get()); } } }); } } public static void execute() { int res = (((Integer.MAX_VALUE / 2 / 3 - 1) * 2 / 5) - 10 * 100) / 2; res--; ++res; res = res / 2; } }
------->take time:48
------->take time2:164
结论 一般应用完全可以忽略try catch带来的效率影响,当百万计的大数据并发,有秒的差距,app可以忽略,后台要适当考虑
论 try catch是否影响性能