Richard
Richard

Reputation: 1117

Using Syncroniation wait/notify complete stops program from working Android

I am trying to stop the for loop and wait until the method has finished and continue once it has called onFinish. I was suggested to use either CyclicBarrier or syncronized wait/notify, but neither of them work.

When I run the method without the "stoppers", it always reaches to the onFinish, calling all 3 System.out.prints, but when I add either CyclicBarrier or syncronized it just does not start ticking. Meaning it only prints the first line countDownTimer first call and then stops and does nothing.
Just to make it shorter I have added both stoppers here to show how I did either of them, but I did use them seperately.

What can I do to make it "tick" ?

    cyclicBarrier = new CyclicBarrier(2);
    object = new Object();


    for (int i = 0; i < sentenceList.size() - 1; i++) {
        String currentLyricLine = sentenceList.get(i).content;

        long diff = sentenceList.get(i+1).fromTime - sentenceList.get(i).fromTime;
        int interval = (int) (diff / sentenceList.get(i).wordCount);


        if(isFirstLine) {
            startLyricCountDownTimer(diff, interval, currentLyricLine, coloredLyricsTextViewFirstLine);
            isFirstLine = false;
        } else {
            startLyricCountDownTimer(diff, interval, currentLyricLine, coloredLyricsTextViewSecondLine);
            isFirstLine = true;
        }


        //First tried with this
        synchronized (object) {
            try {
                object.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        //Then tried with this
        try {
            cyclicBarrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
    }

}


private void startLyricCountDownTimer(final long millisInFuture, final int countDownInterval, String lyrics, final ColoredLyricsTextView coloredLyricsText){
    System.out.println("countDownTimer first call" );
    coloredLyricsText.setText(lyrics);

    new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
            new CountDownTimer(millisInFuture,10) {
                @Override
                public void onTick(long millisUntilFinished) {
                    System.out.println("countDownTimer second call + " + millisUntilFinished);
                    //Do some stuff (irrelevant since it never gets here)
                }

                @Override
                public void onFinish() {
                    System.out.println("countDownTimer last call" );
                     //First tried with this
                    synchronized (object) {
                        object.notify();
                    }

                    //Then tried with this
                    try {
                        cyclicBarrier.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        }
    });


}

If i understand correctly then it was also mentioned that I run my loop on UI thread which is why everything stops. And well I do not wish to stop the UI thread, just to wait for one countDownTimer to finish, then start a new loop.

Upvotes: 1

Views: 47

Answers (0)

Related Questions