packagecom.seven.threadSleep;importjava.text.SimpleDateFormat;importjava.util.Date;//Thread.sleep(1000);线程休眠,参数1000是一秒钟publicclassThreadSleep{publicstaticvoidmain(String[] args)throwsException{//模拟倒计时int num=100;// while (true){// System.out.println(num--);// Thread.sleep(1000);// if(num<=0)break;// }//打印系统当前时间Date date =newDate(System.currentTimeMillis());while(true){if(num<=0)break;System.out.println(newSimpleDateFormat("HH:mm:ss").format(date));Thread.sleep(1000);
date =newDate(System.currentTimeMillis());
num--;}}}
packagecom.seven.threadSleep;//线程join,其他线程都先阻塞,当前join的线程先执行完成,其他线程才能继续执行publicclassThreadJoinimplementsRunnable{@Overridepublicvoidrun(){for(int i =0; i <100; i++){System.out.println(Thread.currentThread().getName()+"--"+i);}}publicstaticvoidmain(String[] args)throwsException{Thread thread =newThread(newThreadJoin(),"AAA");
thread.start();for(int i =0; i <100; i++){System.out.println("main---"+i);if(i==50) thread.join();}}}