runnable

The difference between the Runnable and Callable interfaces in Java

僤鯓⒐⒋嵵緔 提交于 2019-11-26 09:22:50
What is the difference between using the Runnable and Callable interfaces when designing a concurrent thread in Java, why would you choose one over the other? Jorge Ferreira See explanation here . The Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread. A Runnable, however, does not return a result and cannot throw a checked exception. Stephen C What are the differences in the applications of Runnable and Callable . Is the difference only with the return parameter present in Callable ? Basically, yes. See

Runnable with a parameter? [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-11-26 09:16:04
问题 This question already has an answer here: How can I pass a parameter to a Java Thread? 18 answers I have a need for a \"Runnable that accepts a parameter\" although I know that such runnable doesn\'t really exist. This may point to fundamental flaw in the design of my app and/or a mental block in my tired brain, so I am hoping to find here some advice on how to accomplish something like the following, without violating fundamental OO principles: private Runnable mOneShotTask = new Runnable

Android update TextView in Thread and Runnable

喜夏-厌秋 提交于 2019-11-26 08:08:42
问题 I want to make a simple timer in Android that updates a TextView every second. It simply counts seconds like in Minesweeper. The problem is when i ignore the tvTime.setText(...) (make it //tvTime.setText(...), in LogCat will be printed the following number every second. But when i want to set this number to a TextView (created in another Thread), the program crashes. Does anyone have an idea how to solve this easily? Here\'s the code (method is called on startup): private void

Android: How do I stop Runnable?

断了今生、忘了曾经 提交于 2019-11-26 05:25:48
问题 I tried this way: private Runnable changeColor = new Runnable() { private boolean killMe=false; public void run() { //some work if(!killMe) color_changer.postDelayed(changeColor, 150); } public void kill(){ killMe=true; } }; but I can\'t access kill() method! 回答1: Instead implement your own thread.kill() mechanism, using existing API provided by the SDK. Manage your thread creation within a threadpool, and use Future.cancel() to kill the running thread: ExecutorService executorService =

removeCallbacks not stopping runnable

送分小仙女□ 提交于 2019-11-26 04:48:54
问题 I am calling from a method: myHandler.postDelayed(mMyRunnableHide, 6000); which calls: public Runnable mMyRunnableHide = new Runnable() { public void run() { mTextDisplay.setText(\"\"); DisplayX(); } }; if a button on screen is clicked I want to stop the runnable: Button next = (Button) findViewById(R.id.Breaction); next.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { myHandler.removeCallbacks(mMyRunnableHide); mTextDisplay.setText(\"\"); DisplayX(); } }); }

What's the difference between Thread start() and Runnable run()

你离开我真会死。 提交于 2019-11-25 21:53:11
问题 Say we have these two Runnables: class R1 implements Runnable { public void run() { … } … } class R2 implements Runnable { public void run() { … } … } Then what\'s the difference between this: public static void main() { R1 r1 = new R1(); R2 r2 = new R2(); r1.run(); r2.run(); } And this: public static void main() { R1 r1 = new R1(); R2 r2 = new R2(); Thread t1 = new Thread(r1); Thread t2 = new Thread(r2); t1.start(); t2.start(); } 回答1: First example: No multiple threads. Both execute in

“implements Runnable” vs “extends Thread” in Java

你说的曾经没有我的故事 提交于 2019-11-25 21:36:35
问题 From what time I\'ve spent with threads in Java, I\'ve found these two ways to write threads: With implements Runnable : public class MyRunnable implements Runnable { public void run() { //Code } } //Started with a \"new Thread(new MyRunnable()).start()\" call Or, with extends Thread : public class MyThread extends Thread { public MyThread() { super(\"MyThread\"); } public void run() { //Code } } //Started with a \"new MyThread().start()\" call Is there any significant difference in these two