thread-sleep

Thread.Sleep alternative in Java

蓝咒 提交于 2020-01-03 10:57:54
问题 I've been told that using Thread.Sleep() is a bad solution at times that one would want to make some time interval within a loop of actions in a synchronized method. On the other hand, I have two different threads which are active throughout the running time of my program and also one shared object and when I use Object.wait(long) in that shared object, it causes my GUI to freeze for some time. what would be a better solution for this problem? Update This portion of the code is including one

Sleeping less than a second in OCaml

拥有回忆 提交于 2020-01-01 08:17:08
问题 The Unix.sleep function can suspend the program for whole seconds, but how can you suspend it for less than a second? 回答1: The classical Unix solution for this is to use select() with no file descriptors: let minisleep (sec: float) = ignore (Unix.select [] [] [] sec) 回答2: The Thread.delay function pauses the thread for the given number of seconds, but it takes a float, allowing you to pause the thread for less than a second. 回答3: from Unix module val sleepf : float -> unit Stop execution for

Stop code until a condition is met

亡梦爱人 提交于 2020-01-01 06:17:16
问题 How can you create a function or component etc that will stop all running code until a condition is met? For example the same way as a JOptionPane will do, if I have this for example: JOptionPane.showInputDialog(null, "Hello", "Title", 1); Within a function etc and then print to the console afterwards it it will not print until I close the JOptionPane. I am guessing this component has some sort of thread setup built in to do this but how could I duplicate that with my own functions? So say

Stop code until a condition is met

落花浮王杯 提交于 2020-01-01 06:17:02
问题 How can you create a function or component etc that will stop all running code until a condition is met? For example the same way as a JOptionPane will do, if I have this for example: JOptionPane.showInputDialog(null, "Hello", "Title", 1); Within a function etc and then print to the console afterwards it it will not print until I close the JOptionPane. I am guessing this component has some sort of thread setup built in to do this but how could I duplicate that with my own functions? So say

Override Thread.sleep()

断了今生、忘了曾经 提交于 2019-12-30 10:07:30
问题 We have few classes which extends a base class. We noticed that we use quit a few sleeps method and we wanted to log when a sleep occurs. Is there a way to override the Thread.sleep method in which I can add some custom logic ( ie logging) and then just call the actual Thread.sleep()? This way I wouldn't have to change all the places where Thread.sleep is being used in my bases classes. I'm open to other options as well. 回答1: You cannot override Thread.sleep method, you cannot instrument or

Should I always use Task.Delay instead of Thread.Sleep? [duplicate]

筅森魡賤 提交于 2019-12-30 10:05:04
问题 This question already has answers here : When to use Task.Delay, when to use Thread.Sleep? (4 answers) Closed 4 years ago . I have recently seen several recommendations stating that Thread.Sleep should never be used in production code (most recently in this SO question). Many of these advocate for using Task.Delay instead. Most of the explanations I've found use UI applications as examples, since the advantages to Task.Delay are obvious (not blocking the UI). In my case, I am using Thread

Making a Thread to Sleep for 30 minutes

穿精又带淫゛_ 提交于 2019-12-30 08:05:22
问题 I want to make my thread to wait for 30 minutes. Are there any problems of doing this? 回答1: You can make your thread sleep for 30 minutes like this: Thread.sleep(30 * // minutes to sleep 60 * // seconds to a minute 1000); // milliseconds to a second Using Thread.sleep is not inherently bad. Simply explained, it just tells the thread scheduler to preempt the thread. Thread.sleep is bad when it is incorrectly used. Sleeping without releasing (shared) resources : If your thread is sleeping with

Making a Thread to Sleep for 30 minutes

流过昼夜 提交于 2019-12-30 08:04:58
问题 I want to make my thread to wait for 30 minutes. Are there any problems of doing this? 回答1: You can make your thread sleep for 30 minutes like this: Thread.sleep(30 * // minutes to sleep 60 * // seconds to a minute 1000); // milliseconds to a second Using Thread.sleep is not inherently bad. Simply explained, it just tells the thread scheduler to preempt the thread. Thread.sleep is bad when it is incorrectly used. Sleeping without releasing (shared) resources : If your thread is sleeping with

Thread sleep inside of actionPerformed method

£可爱£侵袭症+ 提交于 2019-12-29 08:55:12
问题 First of all I want to say I'm aware this aproach is wrong so I'm asking this question because of pure curiousity. Lets say I have a swing application like this: import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class ThreadSleeping { JFrame frame = new JFrame();

Thread sleep and precise timing [duplicate]

不羁的心 提交于 2019-12-29 06:54:09
问题 This question already has answers here : How accurate is Thread.sleep? (3 answers) Closed 3 years ago . I am making a code where I want definite precision in my timing. I use a robot to make some actions, then I use Thread.sleep(some_time) for some_time to be elapsed between the actions. But I don't get best results because as I search it, sleep is not accurate. What would be the best way to accomplish this? I mean simulate Thread.sleep with other methods. 回答1: Timing in modern OSes is never