Android app

My question: I have successfully set two Activities

that run on the respective threads, but the problem is that I cannot set some delay between these two threads, i.e. i need to execute t1 (low priority) first and t2 say 30 seconds later. And I also want to pause t1 when t2 starts executing.

Code:

final Thread t1 = new Thread() {
    public void run(){
        try{
            sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally{
            Intent First=new Intent("com.example.realtimeassignment.Tasky1");
            startActivity(First);
        }   
    }
};

Thread t2 = new Thread() {
    public void run() {
        try{
            sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally{
            Intent Second=new Intent("com.example.realtimeassignment.Tasky2");
            startActivity(Second);
        }

        handler1.postDelayed(this, 60000);                               
    }
};

t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(Thread.MAX_PRIORITY);

t1.start();
//I need some delay here
t2.start(); 

      

+3


source to share


1 answer


So, here's your task:

final Handler handler - new Handler();
TimerTask task = new TimerTask() {
     public void run() {
           handler.post(new Runnable() {
                 public void run(){
                     t2.start(); // after 30 sec 2d thread will be executed
                     t1.sleep(?) // ? = how long time?
                 }
           });
     }
};

      

Here is the timer:

Timer timer = new Timer();
timer.schedule(task, 30000L);//30 000 = 30sec

      

If you want to stop Timer

use this code:



if(timer != null){
   timer.cancel();
   timer.purge();
}

      

Is this what you are looking for?

And pause thread

, you can set as sleep(TIME);

TIME = run duration thread

# 2.

UPDATE

final Thread t1 = new Thread() {
    public void run(){
        try{
            sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally{
            Intent First=new Intent("com.example.realtimeassignment.Tasky1");
            startActivity(First);
        }   
    }
};

Thread t2 = new Thread() {
    public void run() {
        try{
            sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally{
            Intent Second=new Intent("com.example.realtimeassignment.Tasky2");
            startActivity(Second);
        }
        handler1.postDelayed(this, 60000);                               
    }
};

t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(Thread.MAX_PRIORITY);

t1.start();
//I need some delay here
final Handler handler - new Handler();
TimerTask task = new TimerTask() {
    public void run() {
       handler.post(new Runnable() {
            public void run(){
               t2.start(); // after 30 sec 2d thread will be executed
               t1.sleep(?) // ? = how long time?
            }
       });
    }
};
Timer timer = new Timer();
timer.schedule(task, 30000L);

      

+1


source







All Articles