public class Test94 { public static void main(String[] args) { Window94 w1=new Window94(); Window94 w2=new Window94(); Window94 w3=new Window94(); //设置线程 w1.setName("线程1"); w2.setName("线程2"); w3.setName("线程3"); //启动线程 w1.start(); w2.start(); w3.start(); } }
//回顾复习过往知识点 public class Window94 extends Thread{ private static int age=500; static Object object=new Object(); @Override public void run() { while (true){ synchronized (object){ if (age<550){ try { Thread.sleep(30); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+":"+age); try { Thread.sleep(30); } catch (InterruptedException e) { e.printStackTrace(); } age++; }else { break; } } } } }