import java.io.*; class test { public static void main (String[] args) throws java.lang.Exception { System.out.println("hi"); Cai cai = new Cai(); Thread thread = new Thread(cai); Thread thread2 = new Thread(cai); Thread thread3 = new Thread(cai); thread.setName("线程1"); thread2.setName("线程2"); thread3.setName("线程3"); thread.start(); thread2.start(); thread3.start(); } } class Cai implements Runnable { @Override public synchronized void run() { for (int i = 0; i < 100; i++) { System.out.println(Thread.currentThread().getName()+" : "+i); } } }
加上 thread.join,主线程才会等待这个线程执行完毕
我的可以啊,每次都可以