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 8286819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:52:43+00:00 2026-06-08T11:52:43+00:00

int delay = 1000; // delay for 1 sec. int period = 10000; //

  • 0
int delay = 1000; // delay for 1 sec. 
int period = 10000; // repeat every 10 sec. 
Timer timer = new Timer(); 
timer.scheduleAtFixedRate(new TimerTask() 
    { 
        public void run() 
        { 
            displayData();  // display the data
        } 
    }, delay, period);  

And other:

while(needToDisplayData)
{
    displayData(); // display the data
    Thread.sleep(10000); // sleep for 10 seconds
}   

Both of them doesn’t work (application is force closed). What other options I can try?

  • 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-08T11:52:44+00:00Added an answer on June 8, 2026 at 11:52 am

    You code is failed because you perform sleep in background thread but display data must be performed in UI thread.

    You have to run displayData from runOnUiThread(Runnable) or define handler and send message to it.

    for example:

    (new Thread(new Runnable()
            {
    
                @Override
                public void run()
                {
                    while (!Thread.interrupted())
                        try
                        {
                            Thread.sleep(1000);
                            runOnUiThread(new Runnable() // start actions in UI thread
                            {
    
                                @Override
                                public void run()
                                {
                                    displayData(); // this action have to be in UI thread
                                }
                            });
                        }
                        catch (InterruptedException e)
                        {
                            // ooops
                        }
                }
            })).start(); // the while thread will start in BG thread
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this timer here: timer = new Timer(delay, new ActionListener() { @Override public
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { int count = jSlider1.getValue(); int delay = jSlider2.getValue(); int
I have a code right below...take a look. enter.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent
I have table in data base name train delay, with columns train number(int), DelayTime(int),
int main(void) { char testStr[50] = Hello, world!; char revS[50] = testStr; } I
int* p_bob = new int; *p_bob = 78; The above code makes sense to
Given the following RX code: static void Main(string[] args) { int x = 0;
Can anyone tell me why does this timer run only once? import java.awt.*; import
My code: var list = new LinkedList<int>(); var ramCounter = new PerformanceCounter(Memory, Available MBytes);
I want to repeat execution of some codes after a delay. Following is the

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.