使用定时器吧。- import java.util.Timer;
- import java.util.TimerTask;
- public class TraditionalTimerTest {
-
- //private static int count = 0;
- public static void main(String []args){
- /* new Timer().schedule(new TimerTask() {
-
- @Override
- public void run() {
- System.out.println("Bombing");
- }
- }, 10000,3000);
- */
- /*class myTimerTask extends TimerTask{
-
-
- @Override
- public void run() {
- count = (count+1)%2;
- // TODO Auto-generated method stub
- System.out.println("Bombing1");
- new Timer().schedule(new TimerTask() {
-
- @Override
- public void run() {
- // TODO Auto-generated method stub
- System.out.println("Bombing2");
- }
- }new myTimerTask(), 2000+2000*count);
- }
-
- }
-
-
- new Timer().schedule(new myTimerTask(), 2000);*/
-
-
-
- class myTimerTask1 extends TimerTask{
- @Override
- public void run() {
- // TODO Auto-generated method stub
- System.out.println("Bombing");
- new Timer().schedule(new TimerTask(){
- @Override
- public void run() {
- System.out.println("Bombing");
- // TODO Auto-generated method stub
- new Timer().schedule(new myTimerTask1(), 2000);
- }
-
- }, 4000);
- }
- }
-
- new Timer().schedule(new myTimerTask1(), 2000);
-
-
- Thread thread = new Thread(new Runnable() {
-
-
-
- @Override
- public void run() {
-
- while(true){
-
- // System.out.println(new Date().getSeconds());
- try {
- Thread.sleep(1000l);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- }
- }
- });
-
- thread.start();
- }
- }
复制代码 |