public class Demo { public static void main(String[] args) { Nun nun=new Nun(); //调用类 Thread h1=new Thread(nun,"线程1"); Thread h2=new Thread(nun,"线程2"); h1.start(); h2.start(); } } class Nun implements Runnable { //继承Runnable接口 int i=0; @Override public void run() { while (true){ synchronized (this){ //同步锁 notify(); if (i<100){ i++; System.out.println(Thread.currentThread().getName()+"--"+i); }else { break; } try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } }
线程轮流输出1-100数字详细讲解
最新推荐文章于 2024-09-07 15:20:01 发布