Why android run PerformClick as not new Thread - java

In java and android world, to run now Thread we execute it like this
Thread tr = new Thread(new Runnable() {
#Override
public void run() {
todo();
}
});
tr.start();
so tr is new Thread, but in android api PerformClick implements Runnable interface and is called normally by execute run method
private static void handleCallback(Message message) {
message.callback.run();
}
callback is PerformClick
private final class PerformClick implements Runnable {
#Override
public void run() {
performClick();
}
}
I am trying understand why for this case it is not new Thread
Thanks

Related

run timerTask on not ui thread

I got timerTask which calls every 500ms. Every time it calls it freeze my ui thread for a moment. My question is how to make it runs in not UI thread?
mTimer.schedule(keeper, 0, 500);
private class PlayerKeeper extends TimerTask {
#Override
public void run() {
new Thread(new Runnable() {
#Override
public void run() {}
});

Synchronized, lock and wait blocking main UI thread

I have made a simple TaskManager trying to manage a Runnable queue that is needed for my project. However, with a simple scenario, adding a new Runnable blocks the calling thread (main UI thread).
It happens when you add a new task while a current task is not finished.
You can find below a scenario that reproduces it.
I don't clearly understand why, and how I could prevent this.
This is the task manager class :
public class TaskManager {
private Queue<Runnable> executionQueue;
private final Object lock = new Object();
public TaskManager() {
executionQueue = new LinkedList<>();
startListening();
}
public void executeAsyncWithCompl(Runnable runnable, CompletionHandler completionHandler) {
Runnable runnableWithCompl = new RunnableWithCompl(runnable, completionHandler);
executeRunnable(runnableWithCompl);
}
private void executeRunnable(Runnable runnable) {
synchronized (lock) {
executionQueue.add(runnable);
lock.notifyAll();
}
}
public void release() {
synchronized (lock) {
lock.notify();
}
}
private void startListening() {
Thread executionThread = new Thread(new Runnable() {
#Override
public void run() {
listenTasks();
}
});
executionThread.start();
}
private void listenTasks() {
synchronized (lock) {
while (true) {
try {
if(executionQueue.isEmpty()) {
lock.wait();
}
Runnable runnable = executionQueue.poll();
runnable.run();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
}
}
Here is the RunnableWithCompl class :
public class RunnableWithCompl implements Runnable {
private CompletionHandler completionHandler;
private Runnable runnable;
public RunnableWithCompl(Runnable runnable, CompletionHandler completionHandler) {
this.runnable = runnable;
this.completionHandler = completionHandler;
}
#Override
public void run() {
runnable.run();
if(completionHandler != null)
completionHandler.onFinish();
}
}
And the CompletionHandler interface :
public interface CompletionHandler {
void onFinish();
}
The scenario. Let's say you have an Activity with a spinner (to show UI is not blocked), and a button to trigger long tasks.
private TaskManager taskManager;
public void init() {
taskManager = new TaskManager();
launchLongTask();
}
private void onButtonClick() {
launchLongTask() ;
}
private void launchLongTask() {
Runnable longTask = new Runnable() {
#Override
public void run() {
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Log.d(TAG, "Launching long task");
taskManager.executeAsyncWithCompl(longTask, new CompletionHandler() {
#Override
public void onFinish() {
Log.d(TAG, "Long task finished");
}
});
}
The problem is in your startListening() implementation.
It holds the monitor to lock while it is executing tasks which means no other method can obtain the monitor while it is doing work.
This means release() and executeRunnable(...) will block until there are no more runnables queued.
It also means the thread might block if the thread running startListening() is notified before other threads, because it means those threads cannot continue until it releases the monitor.

Cannot name background thread, Void error

I am trying to name my thread, I have this code
public void DownloadFromUrl(final String fileName) { //this is the downloader method
new Thread(new Runnable() {
public void run() {
Looper.prepare();
...
but when I try to name it like this
public void DownloadFromUrl(final String fileName) { //this is the downloader method
Thread t1 = new Thread(new Runnable() {
public void run() {
Looper.prepare();
...
it just says
Required: Java.lang.Thread
Found: Void
Maybe you called the start method on the thread. This returns void.
Try this instead.
Thread t1 = new Thread(new Runnable() {
public void run() {
Looper.prepare();
...
}
t1.start();
But I agree with the other answer, you probably should use somethine else other than threads.
try to use AsynkTask for downloading instead of thread
Look as this AsynkTask

C# using something similar to Java Handler?

In Java, there is a way where you set a Handler and a Runnable so you can return from a function and get that Runnable thing to run later.
What is the equivalent of this tasks in C#?
private Runnable mShowContentRunnable = new Runnable() {
#Override
public void run() {
}
};
private void obtainData() {
mainActivity.getActionBar().setHomeButtonEnabled(false);
mHandler = new Handler();
mHandler.postDelayed(mShowContentRunnable, 375);
}

Andengine - handler.postDelayed doesn't work

I want to do a simple action in my game after one second. I have my GameScene class which extends Andengine's Scene.
public class GameScene extends Scene{
//(...)
Handler delayHandler;
public GameScene(){
Looper.prepare();
delayHandler = new Handler();
}
//(...)
public void sphereTouched(){
//(...)
delayHandler.postDelayed(new Runnable() {
public void run(){
Log.d("DEB","postDelayed test");
}
}, 1000);
}
}
When sphereTouched function is called operation from postDelayed doesn't run. Others operations from that function work properly. Have I missed something?
use this code for handler.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// your code
}
}, 1000);

Categories