In the actual project, IO, database, network and so on, will inevitably occur unknown exception, try catch can effectively avoid page crashes, online someone said a page four or five try catch affect efficiency, here is the validation:
Example: 100 threads, looping 100 times as an experimental unit:
Package Com.example;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import java.util.concurrent.atomic.atomiclong;/** * @author Xuanyouwu * @email [email protected] * @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.newcachedthre Adpool (); Taketime = new Atomiclong (0); takeTime1 = new Atomiclong (0); for (int j = 0; J <; J + +) {int times = j; Executorservice.execute (New Runnable () {@Override public void run () {Lo ng startTime = System.currenttimemillis (); for (int i = 0; i <; i++) {try {execute (); } catch (Exception e) {}/* try { Thread.Sleep (100); } catch (Exception e) {}*/} long endTime = System.currentti Memillis (); Taketime.addandget ((endtime-starttime)); if (times = =) {System.out.println ("------->take Time:" + taketime.get ()); } } }); } for (int j = 0; J <; J + +) {int times = j; Executorservice.execute (New Runnable () {@Override public void run () {Lo ng startTime1 = System.currenttimemillis (); for (int i = 0; i <; i++) {execute (); /* Try {thread.sleep (100); } catch (Exception e) {}*/} long endTime1 = System.cuRrenttimemillis (); Taketime1.addandget ((endtime1-starttime1)); if (times = =) {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; }}
Operation Result:
------->take Time:3
------->take Time2:3
Result: no millisecond-level gap
Turn on thread sleep:
Package Com.example;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import java.util.concurrent.atomic.atomiclong;/** * @author Xuanyouwu * @email [email protected] * @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.newcachedthre Adpool (); Taketime = new Atomiclong (0); takeTime1 = new Atomiclong (0); for (int j = 0; J <; J + +) {int times = j; Executorservice.execute (New Runnable () {@Override public void run () {Lo ng startTime = System.currenttimemillis (); for (int i = 0; i <; i++) {try {execute (); } catch (Exception e) {} try { Thread.Sleep (100); } catch (Exception e) {}} long EndTime = System.currenttime Millis (); Taketime.addandget ((endtime-starttime)); if (times = =) {System.out.println ("------->take Time:" + taketime.get ()); } } }); } for (int j = 0; J <; J + +) {int times = j; Executorservice.execute (New Runnable () {@Override public void run () {Lo ng startTime1 = System.currenttimemillis (); for (int i = 0; i <; i++) {execute (); try {thread.sleep (100); } catch (Exception e) {}} long endTime1 = System.currenttimEmillis (); Taketime1.addandget ((endtime1-starttime1)); if (times = =) {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; }}
Operation Result:
------->take time2:601414
------->take time:631501
Loop 1 million times:
Package Com.example;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import java.util.concurrent.atomic.atomiclong;/** * @author Xuanyouwu * @email [email protected] * @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.newcachedthre Adpool (); Taketime = new Atomiclong (0); takeTime1 = new Atomiclong (0); for (int j = 0; J <; J + +) {int times = j; Executorservice.execute (New Runnable () {@Override public void run () {Lo ng 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 = =) {System.out.println ("------->take Time:" + taketime.get ()); } } }); } for (int j = 0; J <; J + +) {int times = j; Executorservice.execute (New Runnable () {@Override public void run () {Lo ng startTime1 = System.currenttimemillis (); for (int i = 0; i < 100_0000; i++) {execute (); } Long endTime1 = System.currenttimemillis (); Taketime1.addandget ((endtime1-starttime1)); if (times = =) {System.out.println ("------->take time2:" + taketime.get ()); } } }); }} public staticvoid execute () {int res = (((INTEGER.MAX_VALUE/2/3-1) * 2/5)-10 * 100)/2; res--; ++res; res = RES/2; }}
Operation Result:
------->take time:48
------->take time2:164
Conclusion The general application can completely ignore the efficiency effect of Try catch, when millions of big data concurrency, there is a second gap, the app can be ignored, the background should be considered properly
On whether try catch affects performance