sleep

Multithreading, when to yield versus sleep

不羁岁月 提交于 2019-12-02 19:31:39
To clarify terminology, yield is when thread gives up its time slice. My platform of interest is POSIX threads, but I think the question is general. Suppose I have consumer/producer pattern. If I want to throttle either consumer or producer, which is better to use, sleep or yield? I am mostly interested in efficiency of using either function. The "right" way to code a producer / consumer is to have the consumer wait for the producer's data. You can achieve this by using a synchronization object such as a Mutex. The consumer will Wait on the mutex, which blocks it from executing until data is

What happens to my app when my Mac goes to sleep?

梦想的初衷 提交于 2019-12-02 19:20:05
When Mac OS X goes to sleep, due to closing a laptop or selecting "Sleep" from the Apple menu, how does it suspend an executing process? I suppose non-windowed processes are simply suspended at an arbitrary point of execution. Is that also true for Cocoa apps, or does the OS wait until control returns to the run loop dispatcher, and goes to sleep in a "known" location? Does any modern OS do that, or is it usually safe enough to simply suspend an app no matter what it is doing? I'm curious, because allowing sleep to occur at any moment means, from the app's perspective, the system clock could

Thread Sleep and Windows Services

倖福魔咒の 提交于 2019-12-02 18:37:01
I'm working on a Windows Service that's having some issues with Thread.Sleep() so I figured I would try to use a timer instead as this question recommends: Using Thread.Sleep() in a Windows Service Thing is it's not entirely clear to me how one might implement this. I believe this is the way but I just wanted to make sure: '' Inside The Service Object Dim closingGate As System.Threading.AutoResetEvent Protected Overrides Sub OnStart(ByVal args() As String) Dim worker As New Threading.Thread(AddressOf Work) worker.Start() End Sub Protected Sub Work() Dim Program = New MyProgram() closingGate =

powershell mouse move does not prevent idle mode

寵の児 提交于 2019-12-02 18:20:24
Before I start, here is my very first little code I wrote in PowerShell :) [System.Windows.Forms.Cursor]::Position = ` New-Object System.Drawing.Point($pos.X, ($pos.Y - 1)) [System.Windows.Forms.Cursor]::Position = ` New-Object System.Drawing.Point($pos.X, $pos.Y) What do I want to achieve? Well, I want to move the mouse cursor every 4 minutes to prevent the screensaver from appearing (every second in the code above for testing). The code does really move the mouse every time one pixel up and then down immediately. The thing is, the screensaver (or idle mode of windows) is still appearing. Now

Why are all of my threads sleeping using sleep()?

故事扮演 提交于 2019-12-02 18:11:48
问题 I saw the following piece of code regarding threading in Linux on the web. But when I run it, all the threads seem to sleep instead of just the main thread. Why? Also, without sleep(5), the statement-"Thread created successfully" runs 3 times instead of 2? Can someone please explain this behavior? Thanks Compiled using: gcc -pthread check.c and my o/p: First thread processingn Thread created successfullyn Second thread processingn Thread created successfullyn The first two lines are printed

Java Swing Restart Timer After Operation

你离开我真会死。 提交于 2019-12-02 17:44:11
问题 I need my timer to restart or at least add another delay after a certain line of code is performed. private static class ButtonHandler implements ActionListener { public void actionPerformed (ActionEvent e) { final JButton button = (JButton)e.getSource(); Timer timer = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) { String tc = random(); them.setText("They chose: " + tc + "!"); if (button == rock) { whoWins("rock", tc); } else if (button == paper) { whoWins

Dim iPhone Screen, but don't let it sleep

别等时光非礼了梦想. 提交于 2019-12-02 17:32:42
Update https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIScreen_Class/index.html#//apple_ref/occ/instp/UIScreen/brightness That's the Apple Doc for controlling screen brightness. Below is the original question. I have found by using Google that I can disable the iPhone going to sleep in an application by using: application.idleTimerDisabled = YES; so the code looks like this: - (void)applicationDidFinishLaunching:(UIApplication *)application { // Override point for customization after application launch // This disables the autosleep I added this to TEST, // delete later

Thread.sleep() implementation

你说的曾经没有我的故事 提交于 2019-12-02 16:16:37
Today I had an interview on which I asked candidate quite usual and basic question about the difference between Thread.sleep() and Object.wait() . I expected him to answer something like like this , but he said these methods basically are the same thing, and most likely Thread.sleep is using Object.wait() inside it, but sleep itself doesn't require external lock. This is not exactly a correct answer, because in JDK 1.6 this method have following signature. public static native void sleep(long millis) throws InterruptedException; But my second thought was that it's not that ridiculous. It's

Java Sleep not working in loop [closed]

倖福魔咒の 提交于 2019-12-02 15:57:56
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . What I want to do in my java program is, that when I press the button it displays text in textfield in intervals of time. i.e I press the button then a jFrame pops up and there is a label which shows text like: 1st second:"1st" then a time lag of say 1 sec then 2nd thing: "2nd" I am a newbie and I

Change JButton color in 500ms

只谈情不闲聊 提交于 2019-12-02 13:26:48
My task is to make a Button change his color every 500ms from red to black, when pressing it. This should start and stop by every push on the Button. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Button extends JButton{ public Button() { setBackground(Color.red); addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { change ^= true; while(change) { setBackground(Color.black); try { Thread.sleep(500); } catch (InterruptedException ex) {} setBackground(Color.red); } } }); } boolean change = false; } This Code doesnt work for me, I hope