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

  • SEARCH
  • Home
  • 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 8783475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:50:17+00:00 2026-06-13T20:50:17+00:00

My situation is, I have two concurrent threads, one that cant start a timer

  • 0

My situation is, I have two concurrent threads, one that cant start a timer and the other can stop the timer. The timer works in a way such that, once it has started it will count to 5 seconds and execute a function after, it will keep doing this until the timer is stopped by the other thread. How can this be implemented in Java. This is what I have, I feel it is the wrong way of doing it:

Note that sleep is a global volatile variable that the other two threads turn on and off.

            void creatTime(final EventHandler handler)
            {
                Thread timer = new Thread()
                {
                    public void run()
                    {
                        try
                        {
                            while(true)
                            {
                                while(sleep) Thread.sleep(1000);

                                //invoke function

                            }
                        }
                        catch(IOException e)
                        {
                            System.out.println(e);
                        }
                    }
                };
                timer.start();
            }
            }
  • 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-06-13T20:50:19+00:00Added an answer on June 13, 2026 at 8:50 pm

    You can create a TimerTask and schedule it to run every 5 seconds

    TimerTask timerTask = new TimerTask() {
    
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    //Invoke your function here
                }
            };
    

    //Create a Timer and schedule it

    Timer timer = new Timer();
    
    timer.scheduleAtFixedRate(timerTask, 0, 5*1000);
    

    //To terminate the Timer you can call its cancel method.

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

Sidebar

Related Questions

This is the situation: I have two tables where the one references the other
My situation is that I have two threads. The 1st thread produces a number
Consider the following situation: We have two Localizable.string files, one in en.lproj and one
Here's the situation: I have a two images that are over 1024 in width
I have a situation where I want a Java client to have a two-way
This is situation I have: I have two entities with one-to-many relationship mapped like
I have a situation where I have two arrays and I use one array(A)
The situation is I have two xslt files: one is called from my ASP.NET
I have a situation in which we have two production databases that synchronize with
I have the following situation: I have two threads thread1 , which is a

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.