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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:43:52+00:00 2026-06-06T15:43:52+00:00

In the application I’m developing, I have a thread that is running in a

  • 0

In the application I’m developing, I have a thread that is running in a loop. Inside the loop, several conditions are evaluated, and depends on these conditions, a value or another is stored in the SharedPreferences.

public void run()
{
  try
  {
    SharedPreferences preferences = 
       context.getSharedPreferences("MyPrefs", Activity.MODE_PRIVATE);

    SharedPreferences.Editor editor = preferences.edit();

    while (true)
    {                       
      if (condition1)
      {
        editor.putBoolean("mykey", "1");
      }
      else if (condition 2)
      {
        editor.putBoolean("mykey", "2");            
      }
      editor.commit();

      if (isInterrupted())
         throw new InterruptedException();              

      Thread.sleep(60000); // 60 seconds
    }
  }
  catch (InterruptedException e)
  {
    Thread.currentThread().interrupt();
  }
}

This thread is started by an Activity in the onResume method, and is interrupted in the onPause method.

If the thread is interrupted by the activity (main thread) when it is sleeping, an InterruptedException is thrown. That is ok.

But my problem is that if the activity (main thread) interrupts the thread when it is running (and not sleeping). The “interrupted flag” is set to true, but after calling commit on the editor, the flag is set to false, so I can’t interrupt the thread throwing an InterruptedException.

What can I do?

Thanks

  • 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-06T15:43:53+00:00Added an answer on June 6, 2026 at 3:43 pm

    Calling editor.commit() is going to do I/O. If there is a pending interrupt on the thread then this will likely abort the I/O and clear the pending interrupt.

    In order to do what you want you will probably need to prevent that the thread is interrupted while committing. You’ll need to synchronize the access so that the application can only interrupt it while the thread is sleeping. Something like this:

    // This method will interrupt the thread that is looping ONLY while it is sleeping
    public synchronized void interruptNow() {
        threadThatIsLooping.interrupt();
    }
    
    public void run() {
    try {
        SharedPreferences preferences = 
           context.getSharedPreferences("MyPrefs", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        while (true) {
            synchronized {
                // Cannot be interrupted within this block                  
                if (condition1) {
                    editor.putBoolean("mykey", "1");
                } else if (condition 2) {
                    editor.putBoolean("mykey", "2");            
                }
                editor.commit();
            }             
            Thread.sleep(60000); // 60 seconds
        }
    } catch (InterruptedException e) {
        // Do whatever you want here when the thread is interrupted while sleeping
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Application is running in my local system app.config file <add key=dbconn value=Data Source=(local);database=VTech;User ID=sa;Password=sa;
Application use NHibernate. I Have object A that contains set of objects B. I
application design/architecture question for a project that I am building. I have a main
Application, have a TextBlock and two Buttons, the text is displayed TextBlock by clicking
Application : HTA (therefore IE) This is an application that uses SendKeys to populate
Application[temp] = 8; should set the value 8 to the key temp . However,
Application I work on generates several hundreds of files (csv) in a 15 minutes
Application I am developing does some kind of server-side authorization. Communication is done via
The application I'm currently writing is using MVVM with the ViewModel-first pattern. I have
my application use 10 threads that to read a lot of html file.similar 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.