package thread.state;
import thread.demo01.TestThread4;
public class TestSleep implements Runnable{
int tickerNums=10;
@Override
public void run() {
while (true) {
if(tickerNums<=0){
break;
}
try{
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"--->>Get ticker id : "+tickerNums--);
}
}
public static void main(String[] args) {
TestSleep ticket = new TestSleep();
new Thread(ticket,"学生").start();
new Thread(ticket,"老师").start();
new Thread(ticket,"黄牛").start();
}
}
package thread.state;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestSleep2 {
public static void main(String[] args) {
// 10
// 9
// 8
// 7
// 6
// 5
// 4
// 3
// 2
// 1
// 0
try{
tenDown();
} catch (Exception e) {
e.printStackTrace();
}
// 20:01:35
// 20:01:36
// 20:01:37
// 20:01:38
// 20:01:39
// 20:01:40
// 20:01:41
// 20:01:42
// 20:01:43
Date startTime = new Date(System.currentTimeMillis());
while (true) {
try{
Thread.sleep(1000);
System.out.println(new SimpleDateFormat("HH:mm:ss").format(startTime));
startTime=new Date(System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void tenDown() throws InterruptedException {
int num=10;
while (true) {
Thread.sleep(1000);
System.out.println(num--);
if (num < 0) {
break;
}
}
}
}