信不信用定时器还能实现第四种???(开玩笑的,开发绝不能用定时器实现线程)
- package junit.test;
- import java.util.Date;
- import java.util.Timer;
- import java.util.TimerTask;
- import java.util.concurrent.atomic.AtomicInteger;
- public class TestTimer {
- public static void main(String[] args) {
- final AtomicInteger i=new AtomicInteger(100);
-
- new Timer().schedule(new TimerTask() {
- @Override
- public void run() {
- while(i.intValue()>0){
- System.out.println(Thread.currentThread().getName()
- +":"+i.getAndDecrement());
- }
- }
- }, new Date());
-
- new Timer().schedule(new TimerTask() {
- @Override
- public void run() {
- while(i.intValue()>0){
- System.out.println(Thread.currentThread().getName()
- +":"+i.getAndDecrement());
- }
- }
- }, new Date());
-
- while(i.intValue()>0){
- System.out.println(Thread.currentThread().getName()
- +":"+i.getAndDecrement());
- }
- }
- }
复制代码
|