当前位置: 动力学知识库 > 问答 > 编程问答 >

how to stop a process when another is running java android

问题描述:

i am doing an app in android that connecting with servers and downloading some chunks.

now i want to have another one process doing other job. here is my code

class RemindTask extends TimerTask {

public void run() {

flag = true;

System.out.println(" Hello World!");

Receiveeterations reeterations = new Receiveeterations();

int longCounteterations = reeterations.Geteterations();

System.out.println("longCounteterations " + longCounteterations);

long endTime = System.currentTimeMillis() + 10000;

while (System.currentTimeMillis() < endTime) {

}

IntegerResult = 4;

ThroughPut1+=1;

ThroughPut2=0;

//se mia methodo! pou otan oloklirwnetai i diadikasia tha ti kanei false

flag=false;

}

Timer timer = new Timer();

timer.schedule(new RemindTask(), 0,20000);

///////////////////////////////////////////////////////////////////////////////////////////////////

try {

Thread.sleep(10010);

} catch (InterruptedException e2) {

// TODO Auto-generated catch block

e2.printStackTrace();

}

while (!fileparts.isEmpty() ) { //&& !fileparts1.isEmpty() && (flag == false)

String[] myStringArray = new String[IntegerSpeed];

here i have more code with connectios

}

so what i want to do and i don't know, is how when the timer class is triggered the while process will stop. and after the process of timer stops the main method continues.

i try thread.sleep inside timer but it's not doing anything.

网友答案:

all you need is a running flag... maybe that is what you have your flag variable for?

class RemindTask extends TimerTask {

    private boolean runnging = false;

    public void run() {
        running = true;
        [...]
        while (running && System.currentTimeMillis() < endTime) {
            // work
        }

    public void stop() {
        running = false;
        // interrupt the thread if possible
    }
}

now you can call stop() on your running task to make it stop

分享给朋友:
您可能感兴趣的文章:
随机阅读: