Idea线程轮流打印数字,一直到100

package com.zgm.zuoye; /* * 两个线程轮流打印数字,一直到100 * * */ public class Thread1 extends Thread {static int num = 0; static Object obj = new Object(); @Override public void run() { shu(); }private static synchronized void shu() {while (true) { try { Thread.sleep(100); if (num <= 100) {System.out.println("数字:" + num); num++; } else { System.exit(0); }} catch (InterruptedException e) { e.printStackTrace(); }}}public static void main(String[] args) { Thread1 td1 = new Thread1(); td1.start(); //开启线程 Thread1 td2 = new Thread1(); td2.start(); //开启线程}}

    推荐阅读