How to Pass Arguments to Timertask Run Method

后端 未结 5 734
悲&欢浪女
悲&欢浪女 2021-01-12 21:45

I have a method and I want it to be scheduled for execution in later times. The scheduling time and method\'s arguments depend on user inputs.

I already have tried T

5条回答
  •  有刺的猬
    2021-01-12 22:15

    class thr extends TimerTask{    
    @Override
    public void run(){
        System.out.println("task executed task executed .........." );
    }               
    
    
    class service { 
    private Timer t;    
    public void start(int delay){
        Timer timer=new Timer();
        thr task =new thr();                        
        timer.schedule(task, delay);        
        this.t=timer;
    }   
    public void stop(){
        System.out.println("Cancelling the task scheduller ");
        this.t.cancel();
    }
    }
    
    public class task1 {
      public static void main(String[] args) {
    
        service sv=new service();
        sv.start(1000);
        System.out.println("This will check the asynchronous behaviour ");
        System.out.println("This will check the asynchronous behaviour ");
        System.out.println("This will check the asynchronous behaviour ");
        System.out.println("This will check the asynchronous behaviour ");
        System.out.println("This will check the asynchronous behaviour ");
        sv.stop();
    
      } 
    }
    

    the code above works fine to schedule and reschedule the task you can set and reset the task with a timer function

提交回复
热议问题