package com.basic; import java.util.concurrent.TimeUnit; /** * @program JavaBooks * @description: å¼å¸¸éæ¾é * @author: mf * @create: 2019/12/27 23:38 */ public class T5 { private int count = 0; public synchronized void m() { System.out.println(Thread.currentThread().getName() + " start... "); while (true) { count++; System.out.println(Thread.currentThread().getName() + " count = " + count); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } if (count == 5) { int i = 1 / 0; // æ¤å¤æåºå¼å¸¸ï¼éå°è¢«éæ¾ï¼è¦æ³ä¸è¢«éæ¾ï¼å¯ä»¥å¨è¿éè¿è¡catchï¼ç¶å让循ç¯ç»§ç» } } } public static void main(String[] args) { T5 t5 = new T5(); new Thread(() -> t5.m(), "t1").start(); try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(() -> t5.m(), "t2").start(); } }