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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:19:18+00:00 2026-06-02T01:19:18+00:00

I have a process which should react on some events. So when playFromList() is

  • 0

I have a process which should react on some events. So when playFromList() is called it plays some sound from soundpool. Then in a thread I set a flag and for 3,5 seconds it should not play any sounds.

What I got is: It plays sound and if than wait 3,5 seconds. If playFromList() is called 5 times in 3,5 seconds it still gets to SoundManager.playSound(listNr), and still is done in 17,5 seconds. And its not exactly what I wanted. I wanted method SoundManager.playSound(listNr) called only once.

public class Settings{
    public static boolean flag = false;
}

 public class Main{
 public void playFromList(int listNr,int g){

        if(!Settings.flag){
            SoundManager.playSound(listNr);
            if(g ==0){
                mpVolume((float) 0.3);
                t5sec.run();
            }else{pauseMus();}      
        }           
    }

        private Handler vijfSeconden = new Handler(){
            public void handleMessage(Message msg){
                mpVolume((float)0.8);
            }
        };
        Thread t5sec = new Thread(){
            public void run(){
                if(Settings.flag == false){
                    Settings.flag = true;
                    try {
                        Thread.sleep(3500); 
                    } catch (InterruptedException e) {
                        Settings.flag = false;
                        e.printStackTrace();
                    }
                    vijfSeconden.sendEmptyMessage(0);

                    Settings.flag = false;
                }
            }
        };
 }
  • 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-02T01:19:19+00:00Added an answer on June 2, 2026 at 1:19 am

    There are few problems with the code. Probably most important thing that is strange is
    t5sec.run(), in Java you you should use start method on the Thread object to start new Thread. As written it will execute in the calling thread.
    Second problem is absolute lack of synchronization, one way to fix that, I guess, would be to use AtomicBoolean instead of boolean in Settings.flag

    Another issue is that it is quite expensive to start new thread every time. It is a bit hard to tell from the description precisely what you want to do, but if my understanding is correct you should just do something like this:

    if ( (System.currentTimeInMillis() - lastTimePlayed) < 3500) {
     playSound();
     lastTimePlayed = System.currentTimeInMillis();
    }
    

    that’s it and no threads required. You might want to use AtomicInteger to hold lastTimePlayed value if you want your class to be thread safe.

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

Sidebar

Related Questions

I have a main thread which should process data coming from several worker threads.
I am currently trying to communicate a parent process which should have multiple children
I have a process which iterates through a bunch of ActiveRecord models, does some
I have a C++ process which has a thread that needs to send floats
I have to do a background process which will perform some operations.. while this
I have piece of javascript code, which should process when the browser window is
Good day, I have a gen_server process which does some long-running state-updating tasks periodically
I have process which continuously downloads XML from the feed and saves it to
I have ASP.NET MVC application with action which should process posted XML data. At
I have some vb.net code which should print out labels using Teklynx LabelView software

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.