sleep

Multithreading, when to yield versus sleep

≡放荡痞女 提交于 2019-12-31 09:04:48
问题 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. 回答1: 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

Change JButton color in 500ms

邮差的信 提交于 2019-12-31 07:42:57
问题 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) {}

Change JButton color in 500ms

一笑奈何 提交于 2019-12-31 07:42:16
问题 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) {}

When are parameters calculated, when having concatenated call: obj.F1().F2().F3( sin(x) )?

对着背影说爱祢 提交于 2019-12-31 06:09:31
问题 I use the streaming operators (e.g. operator<<(const char*) ) for logging. In my unit tests I have a test like the following: MyLogger oLogger; oLogger << "charly"; oLogger << "foo" << sleep( 10 ) << "bar"; oLogger << "marcus"; First I executed a second thread, which should log at the same time. I was wondering, why between "foo" and "bar" no other log-output was generated. So I printed in each operator the current time . I expected something like this: 50 sec 137051 usec charly 50 sec 137930

Wait() & Sleep() Not Working As Thought

牧云@^-^@ 提交于 2019-12-31 05:26:15
问题 Using JavaFX 8.0 and JRE 1.8.0 u45 in IntelliJ, I created a basic pixel editor app. I have two windows(stages). One is a 32x128 matrix of Circle Objects placed in a Grid Pane and the other, a Tools widow; I have one Controller.java . Using the mouse, the Tools window provides tools to draw lines, rectangles, etc. and a menu for Setup, Files and Playlists. The Files menu presents the user with Open, SaveAs and Delete. To view a saved pixel art file, the user clicks Open and via the

Paho's MQTT client disconnects after the device locks

限于喜欢 提交于 2019-12-31 05:03:45
问题 I'm currently working on a small "Panic Button" app supposed to operate in a medical facility. As one of the project's assumptions is to be Internet-independent, I've decided to go for MQTT with a server set up in the local WLAN. I've implemented the Paho Android Service and it works pretty good. Up to a certain point. Once I lock the device and turn off the sreen, exactly after one minute the client gets disconnected. As I've set MQTT options to KeepAlive interval of 30s, this must be caused

Sleep() in Win32 makes program unresponsive

China☆狼群 提交于 2019-12-31 03:46:07
问题 Basically exactly what the title says. I would like to update the text that a button contains every 1 second when the user presses that particular button. I have noted that when the program doesn't have focus it works alright and the text refreshes correctly but when I am hovering over the program or when I am trying to click on it's menu Windows inform me that the program is unresponsive and asks me if I want it terminated. When the loop finishes the program returns to its normal state. Also

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

Undefined reference to “sleep” but I did include <unistd.h>

。_饼干妹妹 提交于 2019-12-30 04:05:26
问题 Well, this must be a silly one. Here below is a can-not-be-simpler code in C. It can not compile saying "undefined reference to sleep". But I think I include all system header I need... #include <stdio.h> #include <unistd.h> int main() { printf("Test starts.\n"); sleep(1); printf("Test ends.\n"); return 1; } 回答1: Try this at the top: #ifdef __unix__ # include <unistd.h> #elif defined _WIN32 # include <windows.h> #define sleep(x) Sleep(1000 * (x)) #endif This code will allow you to use two

Delay execution 1 second

强颜欢笑 提交于 2019-12-29 08:42:05
问题 So I am trying to program a simple tick-based game. I write in C++ on a linux machine. The code below illustrates what I'm trying to accomplish. for (unsigned int i = 0; i < 40; ++i) { functioncall(); sleep(1000); // wait 1 second for the next function call } Well, this doesn't work. It seems that it sleeps for 40 seconds, then prints out whatever the result is from the function call. I also tried creating a new function called delay, and it looked like this: void delay(int seconds) { time_t