thread-sleep

Best Method to pause a Thread?

若如初见. 提交于 2019-12-13 17:16:50
问题 I have a Runnable class which performs an operation continuously in its run method using an infinite while loop. Sometimes a user might want to pause/resume the operation. What is the best method to pause the Thread. I have two thoughts: FIRST class Foo extends Runnable { @Override public void run() { while(true) { if(pauseFlag) try{ Thread.sleep(); } catch(InterrruptedException ie) {//...} } } SECOND class Foo extends Runnable { @Override public void run() { while(true) { //pause if user

How do I use a while loop with an onTouch function? (Android)

跟風遠走 提交于 2019-12-13 04:30:11
问题 Well, I have been looking up answers, but none have worked for me. I have a button that when it is HELD DOWN, is suppose to continue to move an image across the screen. But for some reason, it just keeps freezing my kindle when I tap the button. I think the thread is part of the problem, but i'm unsure. Here is the code: package com.evilsea.darkages; import android.os.Bundle; import android.app.Activity; import android.graphics.Color; import android.view.MotionEvent; import android.view.View;

JavaGame character is not moving accurately while drawing?

北慕城南 提交于 2019-12-13 03:43:34
问题 for personal practice, I am remaking Flappy bird for desktop with Java, I have managed to get all the pillars generating, screen, bg moving done, but now I have one problem, which is performance. I sometimes feel that the game isn't moving as fast, and sometimes gets stuck for 0.5 secs or something, but that's not the case, when I move my bird, it's moving a bit weird, looks like its moving too much forward & then to the back, watch the gif in MP4 format: http://gyazo.com

How to prevent Thread.Sleep in onDraw affecting other Activities?

徘徊边缘 提交于 2019-12-13 03:27:18
问题 I have a custom View class which is drawing images. The problem is that when I use Thread.sleep(200); in the onDraw method, it also affects the XML elements that are in the same activity. So I have to wait 200 miliseconds untill something happens. MainPage extends Activity implements OnClickListener{ onCreate(...){ RelativeLayout rl = (RelativeLayout) findViewById(R.id.main_rl); rl.addView(new CustomView(this)); } onClick(View v){ switch(v.getId){ ... }; } } CustomView extends View{ onDraw(){

Thread.sleep(millisecond) precision [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-12-13 02:41:06
问题 This question already has answers here : How accurate is Thread.sleep? (3 answers) Closed 3 years ago . Thread.sleep(500) will suspends current thread for at least 500 milliseconds i know it may be little more than 500 but it never less than that. Now 1 millisecond=1000000 nanoseconds I want to suspend current thread for 500 milliseconds i.e.=500*1000000 nanoseconds But when i run the following code it sometimes sleep less than specified value in nanoseconds. Why is this? and how to sleep for

Image not drawing after sleep java

北城以北 提交于 2019-12-13 01:02:26
问题 I am trying to have a background image draw then have a character draw over it. My code was working until I added sleep do I didn't get 1500 fps. package sylvyrfysh.screen; import javax.swing.*; import java.awt.*; import game.infos.Information; public class ImageLoadDraw extends JFrame{ /** * */ private static final long serialVersionUID = 1L; public static void main(){ DisplayMode dm=new DisplayMode(Information.sX,Information.sY,16,DisplayMode.REFRESH_RATE_UNKNOWN); ImageLoadDraw i = new

Splash Screen Alpha Animation in Android

妖精的绣舞 提交于 2019-12-12 21:55:06
问题 I want to display a splash screen animation where an image fades in and then fades out. I want the second activity to load after the image has faded out. Fade-in time (1000 ms) Wait (1000 ms) Fade-out time (1000 ms) Wait (1000 ms) Load second Activity How do I go about this? The code I'm currently using is: import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.animation.AccelerateInterpolator; import android.view.animation.AlphaAnimation;

Java Thread.sleep leaking threads?

隐身守侯 提交于 2019-12-12 12:16:44
问题 So I inherited a bit of code that's waiting for communication from a network source. While it's waiting for more data from the network socket, Thread.sleep(10) is called. This appears to be causing a thread leak, as reported by jconsole and my thread dump here (there are hundreds of entries for Thread-68, Thread-385, etc... but I shortened for brevity): Wed Jan 18 09:14:40 PST 2012 2012-01-18 09:14:50 Full thread dump Java HotSpot(TM) 64-Bit Server VM (20.0-b11 mixed mode): "Thread-69" daemon

Is it okay to use Thread.sleep() in a loop in Java, to do something at regular intervals?

半城伤御伤魂 提交于 2019-12-12 10:30:54
问题 I have read some threads that said that calling Thread.sleep() in a loop is problematic and is a serious performance issue. But in some cases it seems the most natural thing to do. For example if I want my application to do something every 3 minutes (lets say it's an autosave ) public void startAutosaveLoop(){ stop = false; new Thread(new Runnable() { @Override public void run() { while (!stop){ Thread.sleep(T*1000); if (!stop){ // do something } } } }).start(); } Is there a better way to

How to avoid Thread.sleep() in a for loop from interrupting the UI Thread?

纵饮孤独 提交于 2019-12-12 06:59:49
问题 I have the following pseudo code to clarify my problem and a solution. My original posting and detailed results are on Stack Overflow at: Wait() & Sleep() Not Working As Thought. public class PixelArtSlideShow { // called with click of Menu item. create List<File> of each selected pixelArtFile for (File pixelArtFile : List<File>) { call displayFiles(pixelArtFile); TimeUnits.SECONDS.sleep(5); } } public static void displayFiles(File pixelArtFile) { for (loop array rows) for (loop array columns