Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7545473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:56:20+00:00 2026-05-30T08:56:20+00:00

I’m a Java beginner and have been futzing around with various solutions to this

  • 0

I’m a Java beginner and have been futzing around with various solutions to this problem and have gotten myself kind of knotted up. I’ve tried with Threads and then discovered this Timer class and have messed around with it without success so far. If you could post executable code with a main method so I could see it working and start playing around from there, that would be great.

  1. Launch program
  2. call doSomething()
  3. Generate random number and set Timer for that long.
  4. When Timer goes off, call doSomething() again.

Probably using this: http://docs.oracle.com/javase/6/docs/api/java/util/Timer.html

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:56:22+00:00Added an answer on May 30, 2026 at 8:56 am

    If you want to simply use Timer, I would do something like this:

    public class TestClass {
        public long myLong = 1234;
    
        public static void main(String[] args) {
            final TestClass test = new TestClass();
    
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
    
                @Override
                public void run() {
                    test.doStuff();
                }
            }, 0, test.myLong);
        }
    
        public void doStuff(){
            //do stuff here
        }
    }
    

    Sorry for the lousy identation.

    Also, if you need to schedule execution of code, take a look at Guava Services since it can really make your code much clearer and abstract quite a bit of the boilerplate of creating threads, scheduling, etc.

    By the way, I didn’t take the trouble of generating random number, etc, but I think you can figure out how to include that part. I hope this is enough to get you on the right track.

    For the record, if you were to use Guava, it would look something like this:

    class CrawlingService extends AbstractScheduledService {
    
        @Override
        protected void runOneIteration() throws Exception {
            //run this alot
        }
    
        @Override
        protected void startUp() throws Exception {
            //anything you need to step up
        }
    
        @Override
        protected void shutDown() throws Exception {
            //anything you need to tear down
        }
    
    
        @Override
        protected Scheduler scheduler() {
            return new CustomScheduler() {
                @Override
                protected Schedule getNextSchedule() throws Exception {
                    long a = 1000; //number you can randomize to your heart's content
                    return new Schedule(a, TimeUnit.MILLISECONDS);
                }
            };
        }
    }
    

    And you would simply create a main that called new CrawlingService.start(); that’s it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
I have some data like this: 1 2 3 4 5 9 2 6
I have a French site that I want to parse, but am running into
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.