Sunday, August 18, 2013

run a function after fixed time

import java.util.Timer;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TimerTest
{
   
   
   
    public static void main(String[] args)
    {
//        Timer timer = new Timer();
//        timer.schedule(new MyTask(), 100);
//       
//       
//       
//        new Thread( new MyThread() ).start();
//////       
//        for(int k=0; k<5; k++ ){
//           
//            try
//            {
//                System.out.println("main() k="+k);
//                Thread.sleep(100);
//            }
//            catch (InterruptedException e)
//            {
//                e.printStackTrace();
//            }
//        }
//       
//        //creates thread pool of size 2
//        ScheduledThreadPoolExecutor threadPool = new ScheduledThreadPoolExecutor(2);
//        threadPool.schedule(new MyTask1(), 1, TimeUnit.MILLISECONDS);
//        threadPool.schedule(new MyTask2(), 1, TimeUnit.MILLISECONDS);
       
        ScheduledExecutorService scheduleTaskExecutor;
        scheduleTaskExecutor= Executors.newScheduledThreadPool(5);

        // This schedule a task to run every 10 minutes:
        scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
          public void run() {
            // Parsing RSS feed:
              System.out.println("scheduleTaskExecutor run() i=");

            // If you need update UI, simply do this:
          
          }
        }, 0, 5, TimeUnit.SECONDS);
       
    }

}

class MyThread implements Runnable
{
    @Override
    public void run()
    {
        try
        {
            for(int i=0; i<5; i++ )
            {
                Thread.sleep(300);
                System.out.println("My thread run() i="+i);
            }
           
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
       
       
    }
}

class MyTask extends java.util.TimerTask {
    public void run()
    {
        for(int i=0; i<5; i++ )
        {
            try {
                System.out.println("Timer run() i="+i);
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           
        }
       
    }
}

class MyTask1 implements Runnable
{
   public void run()
   {
       try
        {
            for(int i=0; i<5; i++ )
            {
                Thread.sleep(300);
                System.out.println("----Sche 1 is running");
            }
           
        }
        catch (InterruptedException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
     
   }
}
class MyTask2 implements Runnable
{
   public void run()
   {
     
      try
        {
            for(int i=0; i<5; i++ )
            {
                Thread.sleep(300);
                System.out.println(">>>>>Sche 2 is running");
            }
           
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
   }
}

No comments:

Post a Comment