thread-sleep

JProgressBar not working properly

佐手、 提交于 2019-11-28 13:40:08
问题 So my JProgressBar I have set up doesn't work the way I want it. So whenever I run the program it just goes from 0 to 100 instantly. I tried using a ProgressMonitor , a Task, and tried a SwingWorker but nothing I tried works. Here is my program: int max = 10; for (int i = 0; i <= max; i++) { final int progress = (int)Math.round( 100.0 * ((double)i / (double)max) ); EventQueue.invokeLater(new Runnable() { @Override public void run() { try { Thread.sleep(1000); } catch (InterruptedException ex)

Delay is not working in java graphics

拟墨画扇 提交于 2019-11-28 13:11:30
This is a code for drawing points on calculated locations by Bresenham's algorithm: public void drawBresenhamPoints(Graphics2D g2, List<Point> bresenham) throws InterruptedException { Graphics2D g = (Graphics2D) g2; if(bresenham == null) return; g.setColor(Color.DARK_GRAY); for(int i = 0; i < bresenham.size(); i = i+20) { int x = bresenham.get(i).x - pointWidth1/2; int y = bresenham.get(i).y - pointWidth1/2; int ovalW = pointWidth1; int ovalH = pointWidth1; g.fillOval(x, y, ovalW, ovalH); // delay try { Thread.sleep(10); } catch(Throwable e) { System.out.println(e.getMessage()); } } } The list

how to update a jLabel every time with a while loop with a delay

怎甘沉沦 提交于 2019-11-28 11:44:50
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { int count = jSlider1.getValue(); int delay = jSlider2.getValue(); int valueOfSlider = jSlider2.getValue(); int valueOfSlider2 = jSlider1.getValue(); while (count > 0) { count--; String count2 = ""+count; jLabel3.setText(count2); try {Thread.sleep(delay); } catch (InterruptedException ie) { } } It will eventually show the final number on the jLabel but it does not incrementally update the number. any help Swing is single-threaded . Therefore, long-running tasks should never take place in the EDT . This includes sleeping.

Dynamically adding rows to JTable - Why do they appear at once?

て烟熏妆下的殇ゞ 提交于 2019-11-28 10:25:10
问题 In the example, I'm seeking to add a table to my GUI and then dynamically add rows to it (to show the progress). What I don't understand is why all the rows are appearing at once. I mean, the the table's changing, isn't it? Can someone please give me an explanation? import java.awt.Component; public class Main { public static void main(String[] args) { // Show GUI java.awt.EventQueue.invokeLater(new Runnable() { public void run() { GUI gui = new GUI(); gui.setVisible(true); DefaultTableModel

How to suspend a java thread for a small period of time, like 100 nanoseconds?

大城市里の小女人 提交于 2019-11-28 08:57:53
I know Thread.sleep() can make a java thread suspend for a while, like certain milliseconds and certain nanoseconds. But the problem is the invocation of this function also causes overhead. For example, if I want a thread to suspend for 100 nanoseconds, and I call Thread.sleep(0, 100) . The whole cost for this process is invocation_cost + 100 nanosceonds , which may be much larger the what I want. How could I avoid this problem, and achieve my purpose? The reason I need this is that I want to do simulation offline. I profiled the execution time of a task; Now I want to simulate this execution

calling Thread.sleep() from synchronized context in Java

▼魔方 西西 提交于 2019-11-28 07:41:45
I have read that Thread.sleep() will pause the currently running thread for the time specified after which it goes back to runnable state waiting for it's turn to run. Also, if called from synchronized context, sleep() doesn't release the lock it holds. So I was wondering when it will release the lock. If the thread, put on sleep, never gets the chance to run so it will always keep the lock with itself and then how other threads get to enter synchronized methods/block. I am not sure if I am asking valid question. But please help me out. So I was wondering when it will release the lock. It will

Simple animation using Thread.sleep() in ActionListener

人走茶凉 提交于 2019-11-28 02:16:33
I'm having trouble with this code I am using to create a roulette wheel. The goal is to spin the wheel when I click the "SPIN!" button. I have done this by creating a for loop that should change the status of the wheel from true to false, which changes the orientation. This, when done fast enough, should create the illusion of movement. THE PROBLEM I AM HAVING : is that my wheel is only repainting after the whole for loop is done, despite my placement of the repaint(). So, it only spins one tick. Here is some sample code of my ActionListener: public class spinListener implements ActionListener

Would looping Thread.Sleep() be bad for performance when used to pause a thread?

微笑、不失礼 提交于 2019-11-28 01:18:31
问题 There is (or there has been) a lot of talk about wether it's good or bad to use the Thread.Sleep() method. From what I understand it is mainly to be used for debugging purposes. Now I wonder: is it bad to use for my specific purpose, that is, constantly looping it to be able to pause/resume the thread? I do this because I want to pause a thread that performs I/O operations and be able to resume it in a simple way. The I/O operations are basically just writing blocks of 4096 bytes to a file

Do sleep functions sleep all threads or just the one who call it?

本小妞迷上赌 提交于 2019-11-28 00:30:27
问题 I am programming with pthread on linux(Centos)? I wanna to threads sleep a short time to wait for something. I am trying to use sleep(), nanosleep(), or usleep() or maybe something can do that. I want to ask that: Do sleep functions sleep all threads or just the one who call it? Any advices or references would be appreciate. void *start_routine () { /* I just call sleep functions here */ sleep (1); /* sleep all threads or just the one who call it? what about nanosleep(), usleep(), actually I

Android sleep() without blocking UI

99封情书 提交于 2019-11-27 14:40:48
问题 For my new Android application I need a function, that timeout my application for 3 Seconds. I tried the function "sleep()" like this: seekBar1.setProgress(50); // Set something for my SeekBar try{ Thread.sleep(3000); // Wait for 3 Seconds } catch (Exception e){ System.out.println("Error: "+e); // Catch the exception } button.setEnabled(true); // Enable my button It seems to work, but if I was running the application it does it like this: Wait for 3 Seconds, set progress and enable button. I