Closed Linary closed 3 years ago
Runnable runnable = () -> { int errorTimes = 0; for (int i = 0; i < 2; i++) { try { DateUtil.parse("0", "yyyy", "GTM+8"); } catch (ParseException e) { e.printStackTrace(); errorTimes++; } } System.out.println(Thread.currentThread() + " error times: " + errorTimes); }; Thread thread1 = new Thread(runnable, "thread-1"); Thread thread2 = new Thread(runnable, "thread-2"); thread1.start(); thread2.start(); try { thread1.join(); thread2.join(); } catch (InterruptedException e) { e.printStackTrace(); }
The result is:
Thread[thread-1,5,main] error times: 2 Thread[thread-2,5,main] error times: 0
The result is: