wait

Running wait() on a Thread instance from within main() in Java

大憨熊 提交于 2019-12-19 16:57:06
问题 I am playing around with the timed version of wait() in java.lang.Object and have observed that it acts differently in two different scenarios. Scenario1: Using the default definition of run() in Thread public static void main (String[] args) throws InterruptedException { Thread t = new Thread(); t.start(); System.out.print("X"); synchronized(t) { t.wait(10000);} System.out.print("Y"); } Questions on scenario1: I am experiencing a delay between X and Y. Is this because I am calling wait()

Python - wait on a condition without high cpu usage

柔情痞子 提交于 2019-12-19 12:01:59
问题 In this case, say I wanted to wait on a condition to happen, that may happen at any random time. while True: if condition: #Do Whatever else: pass As you can see, pass will just happen until the condition is True. But while the condition isn't True the cpu is being pegged with pass causing higher cpu usage, when I simply just want it to wait until the condition occurs. How may I do this? 回答1: See Busy_loop#Busy-waiting_alternatives: Most operating systems and threading libraries provide a

Making a function to wait an event before returning?

江枫思渺然 提交于 2019-12-19 10:27:33
问题 function myFunction() { wait(); //what I put there? return; } myFunction(); //this is an event; when its triggered I want function to resume onSomething = function() { myFunction.resume(); //what I put there? } Its just a local experience. Note that while(!resume) won't work because that would prevent the event onSomething to happen. 回答1: So, with a generator it would look like so: function test() { // first part of function yield; // second part of function yield; } var gen = test(); //

Thread Synchronization - How to execute threads alternatively

巧了我就是萌 提交于 2019-12-19 08:52:46
问题 I have been trying to solve a problem involving thread communication using wait() and notify(). Basically i have 2 threads T1 and T2 and i want them to be executed in the following order T1 , T2, T1, T2 ..... How can i achieve that? Actual Problem: There are 2 threads T1 - which prints odd numbers (say 1 - 100) and T2 - which prints even numbers (1 - 100). Now, the output should be 1, 2, 3, 4 , 5 , .... 100 回答1: You describe a Producer-Consumer pattern. It's java implementations described in

Bash script kill background (grand)children on Ctrl+C

感情迁移 提交于 2019-12-19 08:15:02
问题 I have a Bash script (Bash 3.2, Mac OS X 10.8) that invokes multiple Python scripts in parallel in order to better utilize multiple cores. Each Python script takes a really long time to complete. The problem is that if I hit Ctrl+C in the middle of the Bash script, the Python scripts do not actually get killed. How can I write the Bash script so that killing it will also kill all its background children? Here's my original "reduced test case". Unfortunately I seem to have reduced it so much

Bash script kill background (grand)children on Ctrl+C

亡梦爱人 提交于 2019-12-19 08:14:21
问题 I have a Bash script (Bash 3.2, Mac OS X 10.8) that invokes multiple Python scripts in parallel in order to better utilize multiple cores. Each Python script takes a really long time to complete. The problem is that if I hit Ctrl+C in the middle of the Bash script, the Python scripts do not actually get killed. How can I write the Bash script so that killing it will also kill all its background children? Here's my original "reduced test case". Unfortunately I seem to have reduced it so much

wait for each jQuery

喜你入骨 提交于 2019-12-19 06:25:00
问题 I'm trying to make a div fade in/out that's within an each statement. The problem is that next item is called before the fade in/out is complete. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js" type="text/javascript"></script> <div id='one'>one</div> <div id='two'>two</div> <div id='three'>three</div> <script> $.each([ "one", "two", "three"], function() { console.log( 'start - ' + this ); animate( this ); console.log( 'end - ' + this ); }); function animate( id )

Python time.sleep vs busy wait accuracy

余生颓废 提交于 2019-12-19 05:54:10
问题 I was playing around with the time.sleep function from python's standard library and found it inadequate for sub-ms delays. From testing I found it to actually wait 1.1-1.2 ms for a 1ms wait. Implementing a busy wait got the accuracy to within 1%. I used: def busy_wait(dt): current_time = time.time() while (time.time() < current_time+dt): pass and could get down to 0.0001 seconds before breaking 1% accuracy. The main questions I have are: Why is the sleep function so inaccurate (possibly a C

Start-Process -wait doesn't work when script is launched from command prompt opened with runas or as a scheduled task

北战南征 提交于 2019-12-19 05:47:25
问题 I've got a script that's I want to run as a scheduled task, but it's not doing the thing it's supposed to. I'm trying to call an executable with Start-Process and the -Wait switch before continuing. Line is Start-Process -FilePath "C:\Pfx Engagement\Admin\Utilities\Backup Restore\BackupRestoreUtil.exe" -ArgumentList "/f "$backup_directory "" -Wait If I call it from a command prompt, ie: powershell .\script.ps1 it works. It runs the command and waits for it to finish before moving on. There's

How to write a loop in jQuery which waits for each function to complete before continuing the loop

余生长醉 提交于 2019-12-19 05:18:08
问题 Please forgive me if this is an obvious one. I have an unknown amount of elements on a page, which I need to loop through one at a time and do stuff to. However, I need the loop to pause until the functions used on the elements have completed, and then continue on to the next iteration. I tried doing this in a $.each loop, but it fired off the commands far to quickly and finished without waiting for them to complete. Any ideas? $('elem').each(function(){ $(this).fadeIn().wait(5000).fadeOut();