/** * @ClassName ReplacePrintRunnable 线程练习 * @Description TODO * @Author Administrator * @Date 2022/4/22 8:47 * @Version 1.0 */ public class ReplacePrintRunnable implements Runnable{ int i = 1; //这里看题意需求是,如果都是数字就可以用一个类,如果是需要交替打印数字和字母等,就要多创建一个类 @Override public void run() { synchronized (this){ while(i<=100){ notifyAll(); System.out.println(Thread.currentThread().getName()+":"+i); i++; try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } }
//测试
/** * @ClassName Test01 线程练习 * @Description TODO * @Author Administrator * @Date 2022/4/22 8:47 * @Version 1.0 */ public class Test01 { public static void main(String[] args) { num re = new num(); Thread thread = new Thread(re); thread.start(); thread.setName("索嗨00002"); Thread thread1 = new Thread(re); thread1.start(); thread1.setName("索嗨00001"); } }