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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:43:09+00:00 2026-05-23T09:43:09+00:00

Right now I’m trying to make a function that increases the volume while boolean

  • 0

Right now I’m trying to make a function that increases the volume while boolean flag1 == true. So what I want to do is increase volume (I can do this), wait and do nothing (“nop”), then check the flag again.

I’ve tried doing a while-loop with Thread.sleep() but it gave kinda unreliable behaviour. Keep in mind I’m calling this function from within a service which may have been started more than once, so it’s important that I check the flags.

Here’s a sample of what I tried:

private void cycleVolume() {

    while (flag1 == true) {

        mAudioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND); 

        try {
            Thread.sleep(750);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

}

flag1 becomes false from a BroadcastReceiver event, which should stop executing this function hopefully. However in testing I noticed that this doesn’t happen for quite some time after it’s supposed to happen, and since I’ve never done much with Threads before I’m guessing there’s an issue there.

So what should I be doing instead?

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-05-23T09:43:09+00:00Added an answer on May 23, 2026 at 9:43 am

    I’m not sure you’re going about this the right way but here is how to fix your current code.

    1) Make your flag variable volatile.

    2) Provide a synchronized getter and setter method for the flag.

    The problem is caused by Java’s memory model. Since you have 2 threads accessing the same field you need to provide a way to synchronize their access. Right now, the 2 threads may not see each other’s changes to the shared field. For a simple boolean using a volatile is probably more efficient.

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

Sidebar

Related Questions

Right now I am trying to use selenium ide to write code that makes
Right now, I keep all of my projects on my laptop. I'm thinking that
Right now I have an SSIS package that runs every morning and gives me
Right now, I have code that looks something like this: Private Sub ShowReport(ByVal reportName
Right now, I'm using this fashion: window.Foo = { alpha: function() {}, bravo: function(arg)
Right now I am trying to save a variable when i close the app
Right now I'm having an issue with a Singleton that I just wrote for
Right now you can sort out the list, by clicking on links that is
Right now i am storing files inside public directory in my application. I want
Right now I'm asking the user for two numbers. I'm trying to print 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.