Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By jim829

Thread: Please, can someone just give me a simple code snippet foor a java.util.Timer?

  1. #1
    JavaJake516 is offline Member
    Join Date
    Aug 2014
    Posts
    6
    Rep Power
    0

    Default Please, can someone just give me a simple code snippet foor a java.util.Timer?

    I don't want a tutorial that shows me how to do a util timer with a button, or that prints a message, or that does anything. I want the simplest possible snippet that makes the timer, lets me have it do something, and can be stopped. No bells and whistles, no strings, no sparkles and explosions. Just a timer. It seems no one knows how to do this, and always wants to do something else. I can do all the other things, I just need code that is known to work right, and is plain. I don't even need explanation, or words. Just code. Thank you to anyone who has brains enough to do this for me, and please don't be mean.

  2. #2
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Please, can someone just give me a simple code snippet foor a java.util.Timer?

    "java.util.timer example"
    Stick that into google, and press enter. And that's it. It's that simple. Literally the first link that comes up has an example.

  3. #3
    JavaJake516 is offline Member
    Join Date
    Aug 2014
    Posts
    6
    Rep Power
    0

    Default Re: Please, can someone just give me a simple code snippet foor a java.util.Timer?

    No. No, no, no.... No. I don't think you get it. This example is just that; an example. That means that it has things other than the required code. Me looking at this, I can't devise what is necessary (to create a timer that waits X amount of time, performs something, waits, etc) from what isn't. This thing has some thing called remindtask, some extension of other classes that I' pretty sure just have to be "import TimerTask" or "import BlahBleghBlogh", and this toolkit thing that, from what I can find, has something to do with gui's which is not necessary for util.timer. I can get this down to this;

    Timer timer;
    public ReminderBeep(int seconds){
    timer = new Timer();
    timer.schedule(new RemindTask(), seconds * 1000);}
    class RemindTask extends TimerTask {public void run() { //tasks } }
    public static void main(String args[]) {new ReminderBeep(5); }

    Which I can't sort out. I'm not used to this, and I know for sure someone here can copy paste code for me, and help me learn, because they are one of few people who can figure out how to help others. I don't mean to insulting, but I came from the stackexchange site system, which I got auto banned from for asking perfectly good questions. So I'm a bit annoyed. Just please help a helpless idiot who can't figure this out.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Please, can someone just give me a simple code snippet foor a java.util.Timer?

    You create a Timer, passing in a TimerTask which involves implementing the run() method which contains the code you want to execute (or a call to a method you want to execute), as well as some other data for when you want it executed (Timer has half a dozen schedule methods).

    At its simplest:
    Java Code:
    Timer t = new Timer().schedule(new TimerTask() {
        @Override
        public void run() {
            // Stick your code in here, all (preferably) call a method in your class to do whatever.
        }
    }, <some Date for when this is to execute>);
    At the top you would import both Timer and TimerTask.

    ETA: Oops...missed the schedule call.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    JavaJake516 is offline Member
    Join Date
    Aug 2014
    Posts
    6
    Rep Power
    0

    Default Re: Please, can someone just give me a simple code snippet foor a java.util.Timer?

    Thank you so much, but one question, can this <date to execute> be milliseconds for the timer? or is this in some date-requiring form of timer?
    EDIT; Oh and compiling results in this error
    test.java:6: error: constructor Timer in class Timer cannot be applied to given
    types;
    Timer t = new Timer().schedule(new TimerTask() {
    ^
    required: int,ActionListener
    found: no arguments
    reason: actual and formal argument lists differ in length
    1 error
    and I don't really know what this means, other than int or actionlistener isn't there, and needs to. But I'm not the one who has used this code.
    Last edited by JavaJake516; 08-22-2014 at 04:26 PM.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,598
    Rep Power
    5

    Default Re: Please, can someone just give me a simple code snippet foor a java.util.Timer?

    The problem is that the call to schedule returns void so you can't assign it to t. Try this - it should print "Waking up..." every second.

    However, you must read the API for both java.util.Timer and java.util.TimerTask to fully understand what is going on.

    Regards,
    Jim

    Java Code:
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class TimerEx {
    
       public static void main(String[] args) {
          Timer t = new Timer();
          t.schedule(new TimerTask() {
             public void run() {
                System.out.println("Waking up...");
             }
          }, 0, 1000);
       }
    }
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    JavaJake516 is offline Member
    Join Date
    Aug 2014
    Posts
    6
    Rep Power
    0

    Default Re: Please, can someone just give me a simple code snippet foor a java.util.Timer?

    thank you, I actually got this to make no complie errors, but the class won't run. It can't seem to find (or load) the main class. It's set to the right path and all, it just won't run.
    current code-
    Java Code:
    	import java.util.Timer;
    	import java.util.TimerTask;
    	public class test{
    	public static void main(String[] args) {
    		Timer t = new Timer();
    		t.schedule(new TimerTask() {
    	public void run() {
    		System.out.println("Waking up...");}
    		}, 0, 5000);
    	}
    	}
    Last edited by JavaJake516; 08-22-2014 at 06:22 PM.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,598
    Rep Power
    5

    Default Re: Please, can someone just give me a simple code snippet foor a java.util.Timer?

    It is in a file called test.java?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    JavaJake516 is offline Member
    Join Date
    Aug 2014
    Posts
    6
    Rep Power
    0

    Default Re: Please, can someone just give me a simple code snippet foor a java.util.Timer?

    command prompt-

    C:\Users\Owner>cd C:\Users\Owner\Desktop\codes\codes

    C:\Users\Owner\Desktop\codes\codes>javac test.java

    C:\Users\Owner\Desktop\codes\codes>java test.class
    Error: Could not find or load main class test.class

    C:\Users\Owner\Desktop\codes\codes>java test
    Error: Could not find or load main class test

    I try both test and test.class, and it just doesn't work.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,598
    Rep Power
    5

    Default Re: Please, can someone just give me a simple code snippet foor a java.util.Timer?

    Perhaps you have your classpath environment variable set. Try the following:

    java -cp . test

    Make certain that you are running this in the same directory where test.class is located. This also
    assumes that there is no explicit package definition.

    Regards,
    Jim
    AlexGraal likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    JavaJake516 is offline Member
    Join Date
    Aug 2014
    Posts
    6
    Rep Power
    0

    Default Re: Please, can someone just give me a simple code snippet foor a java.util.Timer?

    OMG I love you. You have saved this for me! It worked! finally, finally, I can just do what I know how! THANK YOU

Similar Threads

  1. Need code Snippet to switch users in unix
    By mohtashims in forum Advanced Java
    Replies: 4
    Last Post: 09-04-2013, 11:53 AM
  2. Modify JNDI java.util.Properties in code
    By RRB in forum Java Servlet
    Replies: 3
    Last Post: 09-09-2012, 08:57 AM
  3. Code snippet pls?
    By Srinivas0615 in forum New To Java
    Replies: 1
    Last Post: 07-29-2011, 10:32 PM
  4. Plz give me the code of following simple problem..
    By juhiswt in forum New To Java
    Replies: 3
    Last Post: 03-07-2011, 03:54 AM
  5. Timer java code does not work
    By mariapatrawala in forum New To Java
    Replies: 10
    Last Post: 12-12-2010, 10:34 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •