jeudi 12 avril 2018

I couldn't stop thread with if statement

That's my code below

class SecondTextViewTask extends Thread {
        @Override
        public void run() {
            while (true) {
                if (semaphore.availablePermits() == 0 && firstTextDigit <= 9 && secondTextDigit <= 9) {
                    try {
                        Thread.sleep(1000);

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                secondTextDigit++;
                                secondDigit.setText(String.valueOf(secondTextDigit));
                                Log.e("Test== ", "" + secondTextDigit + " " + semaphore.availablePermits());
                                if (secondTextDigit == 9) {
                                    semaphore.release();
                                }
                            }
                        });

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }  

First I start semaphore with 0 and after the secondTextDigit(integer) arrives 9 semaphore release and This task must stop. But when I use Log.e, I can see on my Logcat secondTextDigit=10 and my semaphore=1. At this situation how if statement work and go inside? Where is my logic mistake?
(secondDigit is TextView.)

Aucun commentaire:

Enregistrer un commentaire