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

java - Repeat method in short time interval

问题描述:

I have a activity and I have to update the Views (like Images) every second. I tried a few different methods which I found to implement the same thing. But which of the method is most efficient and reduces memory leak?

Here are the different methods -

Method 1

final Handler handler = new Handler();

handler.postDelayed(new Runnable() {

public void run() {

//DO SOMETHING

handler.postDelayed(this, 1000);

}

}, 1000);

Method 2

ScheduledExecutorService scheduleTaskExecutor = Executors.newScheduledThreadPool(5);

scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {

public void run() {

//DO SOMETHING

}

}, 0, 1, TimeUnit.SECONDS);

Method 3

Timer myTimer = new Timer();

myTimer.schedule(new TimerTask() {

@Override

public void run() {

//DO SOMETHING

}

}, 0, 1000);

Which one should I use? I am asking this question because I'm updating my GUI in very short interval of time.

网友答案:

Context

Take into account the fact the Android uses it's own implementation of java.

There is no Java Virtual Machine in the Android platform. Java bytecode is not executed. Instead Java classes are compiled into a proprietary bytecode format and run on Dalvik, a specialized virtual machine (VM) designed specifically for Android. Unlike Java VMs, which are stack machines, the Dalvik VM is a register-based architecture.

Comparison of Java and Android API

So what holds true for the java virtual machine may not necessarily reflect on Android's implementation.

As a rule of thumbs it's better to use whatever the Android API makes available.

Solution

I recommend using the Handler. Especially if you want to update views.

Even the official Android tutorials recommends :

To move data from a background thread to the UI thread, use a Handler that's running on the UI thread.

From the start Timer gets excluded.

There are some disadvantages of using Timer It creates only single thread to execute the tasks and if a task takes too long to run, other tasks suffer. It does not handle exceptions thrown by tasks and thread just terminates, which affects other scheduled tasks and they are never run

From this answer.

And from the Android docs:

A ThreadPoolExecutor (i.e. ScheduledThreadPoolExecutor) is preferable to Timer when multiple worker threads are needed, or when the additional flexibility or capabilities of ThreadPoolExecutor (which this class extends) are required.

When using the Handler i recommend reading through this tutorial if you want to avoid memory leaks.

If it wasn't the case of an UI update a ThreadPoolExecutor would have been better because they provide improved performance when executing large numbers of asynchronous tasks, due to reduced per-task invocation overhead.

网友答案:

If you want to update values or images on your screen you should use the first method. I solved the same issue by using first method.

Method 1

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    public void run() {
        //DO SOMETHING
        handler.postDelayed(this, 1000);
    }
}, 1000);

Alternative

Thread t = new Thread() {

    @Override
    public void run() {
        try {
            while (!isInterrupted()) {
                Thread.sleep(1000);
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                           //update here (images or other values)
                        }
                    });
                }
        } catch (InterruptedException e) {
        }
    }
};
分享给朋友:
您可能感兴趣的文章:
随机阅读: