0

I'll make simple to understand then: What I want to have is that my two threads start at the same time and not that one waits for the other to finish what it has to do before starting. Here's the code:

public class MyClass extends Activity{
    MediaPlayer player;
    int timeToPause;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylayout);

        final Runnable updateLyrics = new Runnable() {
            @Override
            public void run() {
                while(player.isPlaying()){
                    if(inside(player.getCurrentPosition(), lyricsBegin[index], lyricsEnd[index])){
                        updateLyrics(lyrics[index]);
                    }
                }
            }
        };
        Runnable pausePlayer = new Runnable() {
            @Override
            public void run() {
                while(player.isPlaying()){
                    if(player.getCurrentPosition()/1000 == timeToPause){
                        player.pause();
                    }
                }
            }
        };
        Thread threadUpdate = new Thread(updateLyrics);
        Thread threadPause = new Thread(pausePlayer);
        threadUpdate.start();
        threadPause.start();
    }
}

the function inside(int current, int begin, int end)check if the current position of the player is between the begin and the end , timeToPause is the specific time where the player should pause

  • What's the question? What do you mean by *threads won't to start at the same time*? Please elaborate clearly what the expected output is and what output you are getting! Also show us all relevant code. We cant guess what `lecteur.pause();` does for example. Show us the `Lecturer` class please? – Chetan Kinger Feb 12 '17 at 16:17
  • what I want to have is that "threadChanson" and "threadPause" start at the same time, but what I get is that "threadChanson" starts and then "threadPause" is waiting for it to finish before starting – Kevin Randriajaoson Feb 12 '17 at 16:21
  • 2
    I upvoted to honor your attempts to clearly describe the problem; and by having the relevant code pieces in your question. But please understand: you are a beginner. You have *basic* questions. It is **very** safe to assume that *anything* you can ever ask right now ... has been asked here before. And answered. So, next time: please spend some serious time doing **prior research**. It took me less time to find that "duplicate question" using google than it took me to read your question ... – GhostCat Feb 12 '17 at 17:44
  • Thanks for your reply too GhostCat, yes I'm a real beginner here, but I'm starting to get used to it now, next time, i'll do more research – Kevin Randriajaoson Feb 12 '17 at 17:56
  • Re, "I want [...] my two threads [to] start at the same time." The phrase "at the same time" leaves a lot unsaid. Better to say something like, "I need _X_ to happen within so-many milliseconds or so-many microseconds of _Y_." Then we can talk about whether or not your program and your JVM and your operating system can realistically meet that requirement. – Solomon Slow Feb 13 '17 at 15:55

1 Answers1

2

You can't. The actual threads depend on the OS scheduler.

What you can do is to wait for the states of both to be RUNNABLE. After that you could use a synchronization flag to let them run wild. Atomic variables or volatile should do the trick

bichito
  • 1,406
  • 2
  • 19
  • 23