runnable

Is there a way to make Runnable's run() throw an exception?

江枫思渺然 提交于 2019-11-28 16:47:49
A method I am calling in run() in a class that implements Runnable ) is designed to be throwing an exception. But the Java compiler won't let me do that and suggests that I surround it with try/catch. The problem is that by surrounding it with a try/catch I make that particular run() useless. I do want to throw that exception. If I specify throws for run() itself, the compiler complains that Exception is not compatible with throws clause in Runnable.run() . Ordinarily I'm totally fine with not letting run() throw an exception. But I have unique situation in which I must have that functionality

In a simple to understand explanation, what is Runnable in Java? [closed]

心已入冬 提交于 2019-11-28 15:22:26
What is "runnable" in Java, in layman's terms? I am an AP programming student in high school, whose assignment is to do research, or seek out from others what "runnable" is (we are just getting into OOP, and haven't touched threads yet). opatut A Runnable is basically a type of class (Runnable is an Interface) that can be put into a thread, describing what the thread is supposed to do. The Runnable Interface requires of the class to implement the method run() like so: public class MyRunnableTask implements Runnable { public void run() { // do stuff here } } And then use it like this: Thread t

How to deal with ConcurrentModificationException

霸气de小男生 提交于 2019-11-28 14:31:20
I am getting the a ConcurrentModificationException from my cooldown timer. I use a thread to reduce the values every second like this: public class CoolDownTimer implements Runnable { @Override public void run() { for (String s : playerCooldowns.keySet()) { playerCooldowns.put(s, playerCooldowns.get(s) - 20); if (playerCooldowns.get(s) <= 0) { playerCooldowns.remove(s); } } } } So every second it should reduce every players cooldown by 20, but the problem is that I a getting the CME every couple hours while running the program, especially when lots of people are online. How do I make it so

nested postDelayed / Runnable / Handler Android

元气小坏坏 提交于 2019-11-28 11:41:42
I am trying to use a nested postDelayed because I need to do something after (delayed for) 5 minutes, stop it after (delayed) 30 seconds, do something else, then repeat both events in the cycle again from the start. I just can't seem to get it right. code I have sofar: private long EnabledAfter = 300000; // 5 minutes private long DisabledAfter = 30000; // 30 seconds public void start_timers(){ on_delayed(EnabledAfter); }//end method private void on_delayed(long period_off){ Delayed = new Runnable() { public void run() { something.enable(context); something.enable_else(context, true); off

Gui blocked when starting Thread from button clicked event

妖精的绣舞 提交于 2019-11-28 11:11:22
问题 This may be a basic question but one that I am stuck on. I would like some more understanding on why the GUI is blocked when I start a new thread(runnable) from a button click event? and how do I overcome that? What am I doing wrong? The code below starts a new thread when it is clicked, however I would like to change the background color of a textbox when that button is clicked but I am unable to do that, also the main ui is unresponsive whilst that thread is running, I believed that I was

Why “implements Runnable” is Preferred over “extends Thread”? [duplicate]

佐手、 提交于 2019-11-28 06:20:59
This question already has an answer here: “implements Runnable” vs “extends Thread” in Java 42 answers The Java Thread itself implements a Java Runnable ! and according to most of the experts over Internet, implements Runnable is preferred over extends Thread ! even though we cannot use utilize Runnable in the sense of thread with out the Thread class! Then why do we prefer to implement Runnable over extending Thread since in both cases the actual thread is stated by calling a Thread implemented method (i.e. start() or run() ) although in case of Thread we are not truly "extending" the

Runnable is posted successfully but not run

ⅰ亾dé卋堺 提交于 2019-11-28 05:22:27
In an existing Android project I've encountered the following piece of code (where I inserted the debugging litter) ImageView img = null; public void onCreate(...) { img = (ImageView)findViewById(R.id.image); new Thread() { public void run() { final Bitmap bmp = BitmapFactory.decodeFile("/sdcard/someImage.jpg"); System.out.println("bitmap: "+bmp.toString()+" img: "+img.toString()); if ( !img.post(new Runnable() { public void run() { System.out.println("setting bitmap..."); img.setImageBitmap(bmp); System.out.println("bitmap set."); } }) ) System.out.println("Runnable won't run!"); System.out

SurfaceView shows black screen - Android

微笑、不失礼 提交于 2019-11-28 03:24:58
问题 Basically I want to use SurfaceView for animation. Therefore the class implements Runnable. To experiment, I want to draw a circle. However, it shows only a black screen. I have been trying for days. Really appreciate if someone can help. MainActivity class public class MainActivity extends Activity { private Bitmap Liquid; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature (Window.FEATURE_NO_TITLE); getWindow().setFlags

How to execute a function every two seconds in Java on Android?

余生长醉 提交于 2019-11-28 01:24:46
问题 I'm trying to execute a chunk of Java code in my Android program every two seconds. My code currently looks like this: LinearLayout.postDelayed(new Runnable() { public void run() { //Do stuff here } }, 2000); Unfortunately, this only runs once after two seconds and then never runs again. How could I get it to run every two seconds? Thanks in advance for all your help. 回答1: Try this: LinearLayout.postDelayed(new Runnable() { public void run() { //Do stuff here // assuming LinearLayout is

How to stop this thread in android?

此生再无相见时 提交于 2019-11-27 22:50:09
问题 In my application i am using this thread to rotate the Second hand on the clock. But the Problem is while i close the activity, the thread is still remain run. I want to stop the thread so it can not effect the Bettry of the device. Below is my Activity code where i am rotating the Clock's Second Hand: public class ClockActivity extends Activity implements OnClickListener{ private Button chimeBtn; private ImageView img; private Thread myThread = null; @Override public void onCreate(Bundle