Sleep if no action in given amount of time

Hello, I am trying to make my program sleep if no command is entered and come out of sleep if a command is entered. Once a command is entered it then has to go back into sleep again and so on.
This is part of a bigger program shown in a different thread - if I can get this to work I will rewrite the program so it has these properties.
At the moment it asks for input and does not display sleep every 3 seconds like it should, please give any hints on how I would make this work.
import java.util.*;
public class AIPet2
     public void sleep()
          Scanner scan = new Scanner(System.in);
          String command = scan.nextLine();
          while(command.equals(null))
               System.out.println();
               System.out.print("Sleeping");
               for(int i = 0; i < 6; i++)
                    System.out.print(".");
                    try
                         Thread.sleep(200);
                    catch(Exception e)
                         System.err.println("Caught Exception: " + e.getMessage());
               System.out.println();
               try
                    Thread.sleep(3000);
               catch(Exception e)
                    System.err.println("Caught Exception: " + e.getMessage());
               System.out.println();
     public static void main(String[] args)
          AIPet2 pet = new AIPet2();
          pet.sleep();
}

Ok this is what I have so far although when give input, it gives a run time error of invalidthreadstate: Please can you say what this means.
import java.util.*;
public class AIPet2
     public void input()
          while(true)
               Scanner scan = new Scanner(System.in);
               String command = scan.nextLine();
               backgroundThread.start();
     Thread backgroundThread = new Thread(new Runnable()
          public void run()
              while(true)
                    System.out.println();
                    System.out.print("Sleeping");
                    for(int i = 0; i < 6; i++)
                         System.out.print(".");
                         try
                              Thread.sleep(200);
                         catch(Exception e)
                              System.err.println("Caught Exception: " + e.getMessage());
                    System.out.println();
                    try
                         Thread.sleep(3000);
                    catch(Exception e)
                         System.err.println("Caught Exception: " + e.getMessage());
                    System.out.println();
     public static void main(String[] args)
          AIPet2 pet = new AIPet2();
          pet.input();
}

Similar Messages

  • How to generate a continuous sine wave for a given amount of time

    I am trying to output a continuous sine wave for a speaker, but I am having difficulty making it continuous rather than looping the signal for a given amount of time, which causes discontinuities. There is a VI in the Labview examples titles Continous Generation, but it produces a square wave. Is there a way to adapt this VI such that the amplitude and frequency can be used as inputs to create a sine wave?

    I found a VI that ships with labview(7.0)called continuous sound output. the output of this is a sine wave or you can change it to whatever you want.
    Sorry cannot help more
    Joe
    Joe.
    "NOTHING IS EVER EASY"

  • Sound cuts out after any given amount of time, only way to turn back on is to reboot.

    For reference, I am using a fully up-to-date mid-2012 model rMBP. Attached to my MacBook via USB I also have an M-Audio KeyStation 49i, which is where I have my headphones plugged in (probably not related in any way, but just noting this).
    I first encountered this issue while using Chrome and listening to music on SoundCloud with Garageband open after the last Mavericks update yesterday. I initially thought it was a Chrome bug, and in doing some research, I found that other Chrome users were having the same issue as me, but it was related to PepperFlash (a version of the standard Flash player), and disabling it in Chrome Canary fixed the issue for them.
    Unfortunately, it did not fix the issue for me, but I have refrained from using Chrome since then and stuck to Safari to ensure that it's not two issues with the same outcome.
    Then I stumbled across all of the Mavericks users that are having sound problems; anything from crackling/popping to not working at all. It seemed, though, that everyone that was having the sound cut out for them entirely were not only having this happen after their laptops came back from sleep mode (which is not the case for me), but were able to fix it by restarting Coreaudio[d] or -- if they had been using the headphone jack on their Macs -- they just had to plug it in again and take it back out. Neither of these worked or were applicable to my situation. Some were able to get it working again by resetting the PRAM/NVRAM, and a few others seemed to be able to bypass the issue by using Soundflower, but neither of these solutions worked for me either. The only way I can get sound to work again is to restart my computer entirely.
    After doing so, I am only able to listen to audio for a certain amount of time. It seems to vary depending on how much memory the task is taking up. Like I'll be able to listen to a track or two on SoundCloud, or attempt to record into Garageband for five minutes or so. If I'm in a Garageband project that already has a lot of tracks, I'll be lucky if I can even get through a few seconds before it cuts out entirely. There are very quiet crackling/pop sounds every once and awhile, but it is hard for me to distinguish if that is just the natural state of the hardware I am using or if it is related to background system sounds.
    I'm at wit's end trying to figure out what the problem is, mostly because there are so many others with similar issues and varying hardware/software setups that it's hard to track down exactly what my own situation is.
    I'd like to know where I should be exporting any error logs from so that someone can take a look at this issue.
    Thank you.

    After doing some additional testing, I was able to get the sound working again without rebooting by:
    1. Unplugging the MIDI device.
    2. Resetting the audio input and output to "built-in".
    3. Plugging back in the MIDI device.
    4. Changing the audio input and output back to the MIDI device.
    Still, though, the sound only lasts for a little while, and there doesn't seem to be anything indicating how or why it stops.

  • Request: script to stream music for given amount of time

    I'm looking for a bash script (or anything that will get the job done) that will:
    -stream music from my favorite internet radio station (ie: di.fm)
    -play stream for a given time in increments of 1hr (ie: 1 hr, 3 hrs, etc)
    -at the end of the given time, fade out the music then terminate stream after fade out is complete.
    -terminate script (or application that is performing these tasks)
    Ideally, the script will take in the internet radio station and time as arguments. I have Rhythmbox which I currently use to stream.  I don't care if the script opens applications for use in this process.  Can anyone help me with this?  Or at least get me pointed in the right direction?
    PS: I plan on using this as my music to listen to while I'm trying to go to sleep, hence the time limit and fade out.  If the music stopped abruptly I would most likely wake up.
    Thanks guys.

    I know in mplayer, you can do most of that.
    For example, running this from console will play Lounging Sound radio for exactly one hour;
    mplayer -endpos 01:00:00 http://67.159.44.146:8002/
    I'm not sure about the fade out effect, you might have to Google for that information. I'd recommend the 'mplayer-svn-nogui' package in AUR (or check out the one here) if you decide to go this route.
    Oh, you might want to check out MPD too... it could possibly do something like this.
    Last edited by Pnevma (2008-07-22 12:56:30)

  • How to get number of digits in given amount

    Hi Experts,
    How to get number of digits in given amount.
    Regards
    Mohit

    Check this :
    data: v_data1 type p decimals 3 value '12.860',
          v_char1 type char20,
          v_char2 type char20,
          v_char3 type char20,
          len type i.
    write v_data1 to v_char1.
    split v_char1 at '.' into v_char2 v_char3.
    len = strlen( v_char3 ).
    write:/ v_char2,
          / v_char3, " this holds the digit value
          / len.     " number digits

  • Computer hibernates automatically after sleeping for X amount of time.

    I recently bought a VPS and have been sleeping my old personal server when it's not in use.  However, I find that after putting it to sleep, it'll actually go into hibernate after some amount of time.   I would prefer this *not* to happen so that I could easily wake the machine with WOL.  Was hoping someone could point me in the right direction as to what might be causing this.
    I have the acpid and cpufreq daemons which might affect this.  Both of these are setup with the default configurations.  I have laptop mode installed but *not* in the daemons array/running.
    In order to put the machine to sleep, I am simply using "pm-suspend".   Does the default pm-suspend script cause the machine to hibernate after a specified time?  Or, perhaps a script that comes with laptop-mode that is being run on suspend?
    Appreciate the help in tracking this down!

    You can do it with a time schedule in System Preferences > Energy Saver > Schedule.. button.
    Regards

  • Hi! i just bought the iPad mini 3 today with a case that supports the automatic sleep/wake lid action but i could not find the setting for the sleep/wake lid action in my settings.

    the cover i bought is from the brand called iPearl and it supports the auto sleep/wake lid action but i cant find the settings for it in my settings>general. now my iPad wont sleep when i close the lid. how do i find the option for this action in my settings on my ipad?

    The setting should only appear with the cover installed. Try settings> general> IPad cover lock/unlock> on. Thanks go to Diavonex and Chris CA from this earlier thread.
    The sleep/wake function would not show in general to turn it on. I have a Targus case but the function will not work. What's going on?

  • Click event on a label doesn't work after a certain amount of time?

    I'm having a weird kinda a problem with my label.....I had given a click event on the label but the problem here is that...it won't take any event after a certain amount of time....i mean..it takes the event if you click on it when it is loaded but after sometime it won't take any event...
    And there's one more thing which is that...i've some images too on the page but the images doesn't show up until and unless my mouse gains focus on the page...i.e I've to move my mouse over the page once then only i'm able to see the image...
    Any help on how to solve this problem.....

    The problem is that...the project size is too big and i really don't know where to upload the project.....
    Any help and still thanks for the reply...
    Waiting...

  • Perform each task for certain amount of time

    Hey,
    The program I'm trying to create is a very linear one and involves performing three different processes after one another, each for a set amount of time.
    I originally tried using case statements and timers to do this, but everything got very messy very quickly. I then looked at sequence structures however have read some bad things about them.
    Could anyone point me in the best direction to achive this?
    Thanks,
    Sam.

    Sam_R, Yamaeda is right. Sometimes things are so simple that you do not need a state machine. But sometimes you end up adding features to your simple little program and it ends up growing out of control. Trust me
    There are a million examples of state machines out there. This makes a million and one. It does not address the part of your question about performing an action for a certain amount of time. I wanted to keep it as simple as possible while also demonstrating programmatically determining the next state and error handling.
    I don't mean to sound condescending with this basic example. But I know there was a time when it seemed like advanced stuff to me. Hopefully someone will find this useful.
    =====================
    LabVIEW 2012
    Attachments:
    State Machine.vi ‏12 KB

  • Playing jpeg pictures for different amount of time

    hello everyone.
    i'm new to jmf and i have been reading a bit about it. so i tried the sample code at: http://java.sun.com/javase/technologies/desktop/media/jmf/2.1.1/solutions/JpegImagesToMovie.java (THIS WAS VERY HELPFUL)
    this program displays each image for 1 second each.
    now my problem is, i want to display these pictures for different amount of time... for example: display pic1 for 1 second, then display pic2 for 3 sec and so on.
    can anyone help me please. THANKS

    >
    i'm new to jmf ..>The [JMF forum|http://forums.sun.com/forum.jspa?forumID=28] is the best place to ask JMF related questions, but I am guessing you are also new to Java, and to programming, so this forum might be best for the moment.
    >
    ..and i have been reading a bit about it. so i tried the sample code at: http://java.sun.com/javase/technologies/desktop/media/jmf/2.1.1/solutions/JpegImagesToMovie.java (THIS WAS VERY HELPFUL)
    this program displays each image for 1 second each.
    now my problem is, i want to display these pictures for different amount of time... for example: display pic1 for 1 second, then display pic2 for 3 sec and so on.>Well.. given a run of [JpegImagesToMovie.java|http://java.sun.com/javase/technologies/desktop/media/jmf/2.1.1/solutions/JpegImagesToMovie.java ] might go..
    java JpegImagesToMovie -w 400 -h  300 -f 1
      -o the.mov pic1.jpg pic2.jpg pic3.jpg..you might instead try..
    java JpegImagesToMovie -w 400 -h  300 -f 1
      -o the.mov pic1.jpg pic2.jpg pic2.jpg pic2.jpg pic3.jpg
    Lines broken for clarity.  Instead have command all in one line.
    This simply repeats the second frame, three times. The .MOV format might 'optimize away' the second and third frame from the final file, but I would not count on it.
    Of course, a far better way to animate images (with transition effects, full control over time, and all the bells and whistles) is to load the images into memory and create the slide show using the Java 2D abilities, mostly found in the Graphics class.

  • Can I "show" an image for a specific amount of time after a click?

    Hello,
    I would like an image to appear for only a specific amount of time after the user clicks a button.
    Is this possible?
    Thanks for any help!

    You cannot time with an advanced or shared action. Isn't a rollover image (which you can time) a valid alternative? You could use the timeline in combination with an action Continue, but I don't know the precise circumstances of your question.

  • Increase the amount of times you can "Step backward" alt+cntrl+z?

    I find myself saving numerous iterations of my files, because the defualt settings only let step backwards a few times. Is there a way to greatly increase the amount of times you can step backward? I though perhaps the history window would let me double back on many of my actions, but even that did not go far back enough.

    I'm not fully sure I follow you in this logic yet...  I apologize if I'm being dense.
    Saving your work occasionally is always a good idea, REGARDLESS of the number of history states.  Even if you keep only 10 history states you'll lose just as much work if you haven't saved anything since the last several hundred steps and elephant droppings unexpectedly shut down your PC.
    The important thing I'd like to note is that my choice of the number of history states, from what I have observed, does not seem to change the stability of Photoshop in any way.
    If you find choosing a larger number of states does destabilize Photoshop on your system, perhaps you have RAM problems?  Or maybe configuration issues?  What do you have your RAM usage limit set to?
    Your example about brush strokes is actually a good one to make my point that a deeper history can be a Good Thing.  For example, if I touch up 75 spots (hot pixels in an astroimage, for example) with separate applications of the Spot Healing Brush, then I want to compare before and after images but I forgot to make a layer copy way back when, I can copy the image, go back 75 steps in the History, and paste the result over the original from an old history state to blink a before/after comparison.
    The thing that's not been mentioned here is Efficiency.  I have purposely populated my workstation with a lot of high reliability RAM (16 GB ECC).  You might find Photoshop's efficiency going down if your system doesn't have enough RAM to be able to easily manage a deep history...  Efficiency matters too, and hammering the scratch disk with a lot of I/O can not only slow things down for you but can also sometimes expose latent instability in a system.
    -Noel

  • Is there a way to create a custom screensaver with pics/slides that display for different amounts of time?

    Is there a way to create a custom screensaver with pics/slides that display for different amounts of time? Or even add a "video slide" into the screensaver?
    My business has 3 TVs displayed in our lobby, each with its own apple TV and they are all linked to the same photo stream. Currently the Atv screensaver displays the photostream pictures of our products for the default 3 seconds. We just added pics/slides that now display messages and useful info for our customers but I want that info to be displayed for a longer period of time than the pictures to give people a chance to read it.
    I tried using flickr and powerpoint as a work around but no luck.  I created a slide in powerpoint with play length of 15 secs and saved it as a wmv file.  I added that file to a "screensaver" set in flickr and pointed the Atv screensaver to that flickr set.  It still displays everything, pics and the wmv file at the default 3 sec.
    I know I can increase the duration of all pics in the screensaver to display for longer but the goal is to have the pics display at 3 secs and the info slides to display at 15 secs.

    Welcome to the Apple Community.
    No that's not possible.
    If you have any suggestions that you think might enhance the Apple TV you can send Apple your feedback here 

  • Monitoring user logins and amount of time logged in.

    I presently have an open directory master with several Mac Pro's authenticating against it. The owner of the Mac Pro would like to do billing to the other departments based on who was logged in and the amount of time they were logged in.
    Normally the "last" command would be perfect for this but with the removal of the wtmp file in 10.5 the last command is flaky at best. I will restart its self multiple times per day or not at all for a month so it is hard to write a script to pull the data out of it.
    Does anyone know of a way in 10.5 and higher to find out the type of information that the last command displayed, IE who logged in, what time they logged in and what time they logged out.
    I need this information for the Mac Pro's but can pull the information from the open directory server if there is a way to get it from their.
    Does anyone know where the last command data is being logged since the removal of the wtmp file.

    neither of them seem to give me the login and log out times like the "last" command does. Is there not anyway on the open directory server to pull this data?

  • Amount of time for clients to join a Coherence cluster?

    What are some of the factors that affect the amount of time it takes for a client (without local storage) to join the cluster?
    We are seeing some tests where the time is a few seconds and other tests where the time is 10's of seconds (up to 30-40 seconds). Would like it to be as deterministic as possible. Any ideas?
    Thanks!
    Andy

    Hi Andy,
    We have found that JMX Mbean registration is one factor in startup time, if you have JMX turned on. On joining the cluster, nodes register their MBeans with the MBean servers. If every node has management=all, every node is an MBean server node, whether its needed or not. This can result in an MBean registration storm as every node registers their Mbeans with every other node at startup. Its especially noticable if you have a lot of caches in your cluster. We suggest that only MBean server nodes have the management=all setting to minimize this registration process on startup.
    Marie
    [email protected]

Maybe you are looking for

  • I've been having a problem with auto-fill in the email on my ipod touch.

    I have updated to new email addresses, or accidnetly typed in the wrong on, and now the email addresses won't go away, even though they're not in my contacts. So when i said for instance i want to send a message to [email protected] and it just chang

  • Can't get setSize or setTitle to work in constructor

    I can't get the setTitle or setSize to have an effect from the constructor. If I refer to them in main (see commented out lines) it works fine. I'm using the new beta JDK. Any ideas? Thanks ----------Code----------------------------------------------

  • Second hand ipad locked to unknown previous owner

    hi,i bought a second hand ipad 4,but it is locked with the previous unknown users id,is there anyway of resetting the ipad back to factory settings.many thanks

  • DBMS_XMLStore.setkeycolumn

    I am tracking ORA_ROWSCN at row level using ROWDEPENDENCIES. If ORA_ROWSCN is used a normal update query it is not causing any problems. Error occurs while used it inside a XML for updation as DBMS_XMLStore.setkeycolumn(v_context,'ORA_ROWSCN').* This

  • Unable to Receive IO, because Lot is not found

    Hi All: I have a issue with one Internal Order. I tried to receive an Internal Order, but when I enter the LOT number, I got a msg : Not Result is Found But the Lot number exist for this item, even I use Ctrl+L and not Lot number is there. Receipt (P