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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:47:24+00:00 2026-06-06T03:47:24+00:00

* So I have a Thread that holds a sleep method and I think

  • 0

*So I have a Thread that holds a sleep method and I think that is the problem because my program crashes when I try to stop it using the stop() method. I’m trying to stop the Thread entirely when the user hits the back button (the hardware back button). How should I go about this? Should I use something other than the sleep method? *

Thread timer2 = new Thread() { // Threads - do multiple things
        public void run() {
            try {
                sleep(5000); // 5000 mil secs = 5 secs . sleeps thread
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                cdt2.start();
            }
        }
    };
    timer2.start(); // starts thread


public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
          timer1.stop(); //error
          timer2.stop(); //error
          cdt.cancel();
          cdt2.cancel();
          Intent menu = new Intent("android.intent.action.MENU");
          startActivity(menu);
          return true;
        }
        return false;
      }

LOG CAT:

06-20 13:08:58.006: D/dalvikvm(22050): GC_CONCURRENT freed 951K, 7% free 15393K/16455K,    paused 2ms+3ms
06-20 13:08:58.944: D/dalvikvm(22050): GC_CONCURRENT freed 700K, 6% free 15533K/16455K, paused 2ms+1ms
06-20 13:09:01.733: D/dalvikvm(22050): GC_CONCURRENT freed 955K, 7% free 15630K/16647K, paused 2ms+2ms
06-20 13:09:02.069: D/dalvikvm(22050): GC_CONCURRENT freed 880K, 7% free 15707K/16711K, paused 2ms+2ms
06-20 13:09:02.678: D/dalvikvm(22050): GC_CONCURRENT freed 762K, 5% free 15940K/16775K, paused 2ms+2ms
06-20 13:09:03.303: D/AccountTypeManager(21214): Registering external account type=ro.weednet.contactssync, packageName=ro.weednet.contactssync
06-20 13:09:03.311: D/AccountTypeManager(21214): Registering external account type=com.skype.contacts.sync, packageName=com.skype.raider
06-20 13:09:03.311: W/ResourceType(21214): getEntry failing because entryIndex 1377 is beyond type entryCount 189
06-20 13:09:03.311: W/ResourceType(21214): Failure getting entry for 0x7f020561 (t=1 e=1377) in package 0 (error -2147483647)
06-20 13:09:03.311: W/ResourceType(21214): getEntry failing because entryIndex 1378 is beyond type entryCount 189
06-20 13:09:03.311: W/ResourceType(21214): Failure getting entry for 0x7f020562 (t=1 e=1378) in package 0 (error -2147483647)
06-20 13:09:03.311: W/ResourceType(21214): getEntry failing because entryIndex 1379 is beyond type entryCount 189
06-20 13:09:03.311: W/ResourceType(21214): Failure getting entry for 0x7f020563 (t=1 e=1379) in package 0 (error -2147483647)
06-20 13:09:03.311: W/ResourceType(21214): getEntry failing because entryIndex 1778 is beyond type entryCount 189
06-20 13:09:03.311: W/ResourceType(21214): Failure getting entry for 0x7f0206f2 (t=1 e=1778) in package 0 (error -2147483647)
06-20 13:09:03.311: D/AccountTypeManager(21214): Registering external account type=com.yahoo.mobile.client.share.sync, packageName=com.yahoo.mobile.client.android.mail
06-20 13:09:03.319: D/AccountTypeManager(21214): Registering external account type=com.facebook.auth.login, packageName=com.facebook.katana
06-20 13:09:03.319: W/ResourceType(21214): getEntry failing because entryIndex 344 is beyond type entryCount 189
  • 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-06T03:47:26+00:00Added an answer on June 6, 2026 at 3:47 am

    Your not suppose to call stop() on a runnable. Instead you need to have the Thread return.
    To do this all you need is a flag that you can set to true. In this case you have mStopThread set to false and when you can to kill your thread just set it to true and it’ll return.
    However, I usually see this in some sort of loop with a smaller sleep time.

    Thread timer2 = new Thread() { // Threads - do multiple things
            public void run() {
                try {
                    if (mStopThread) return;
                    sleep(5000); // 5000 mil secs = 5 secs . sleeps thread
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    cdt2.start();
                }
            }
        };
    timer2.start(); // starts thread
    

    If you don’t chose to modify the code example to make a tighter loop than you can always wake up your thread by calling timer2.interrupt() and then getting it to return.

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

Sidebar

Related Questions

I have multiple threads that share use of a semaphore. Thread A holds the
I have a thread that downloads some images from internet using different proxies. Sometimes
I currently have a thread that I created using CreateRemoteThread(). Everything works great. Upon
I have read that Thread.sleep() will pause the currently running thread for the time
I have a program that holds many threads, lets put as an example six
I have a thread that does the following: 1) Do some work 2) Wait
I have one thread that is receiving data over a socket like this: while
I have a thread that sends GPS coordinates to a database every six seconds
I have a background thread that handles communication with an external service. Each time
I have a main thread that invokes a child thread function at different times

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.