Coderji
Coderji

Reputation: 7745

wait() and notify() in a thread android

Im writing an educational mathematics game were you can choose many operations at the same time, now im trying to generate 5 questions, however it runs all the five questions soo fast that i cant answer them except last questions because it is stuck there. so i thought about creating a thread that will wait() after creating the first question and wait till it is solved and answered correctly, then proceed to the next question and so on... now i have never used wait and notify before so where should i assign them here what i got so far, however it gives me an exception:

 while (counter < 5) {
    Thread pause = new Thread() {

        @Override
        public void run() {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally{

                // ops array is for knowing what operation he chose, 
                // 1 for plus and 2 for minus
                //generate random number within the range of the operations length.
                int gen = Integer.valueOf((int) ((Math.random() * (ops.length))));
                Log.d("Testgen", String.valueOf(gen));

                //random operation is generated
                int TheChosenOperation = ops[gen]; 
                Log.d("Test The chosen", String.valueOf(TheChosenOperation));

                switch (TheChosenOperation) {
                case 1: // if it is plus, assign the generated numbers to i and j from plus.java
                    Toast t = Toast.makeText(SecondActivity.this, "Plus", 5000);
                    t.show();
                    tv.setText("+");
                    int i = plus.getBorder();
                    int j = plus.getBorder2();
                    tv2.setText(String.valueOf(i));
                    tv3.setText(String.valueOf(j));
                    answer = plus.getAnswer();
                    break;
                case 2: //if it is minus, assign the generated numbers to i and j from minus.java
                    Toast t2 = Toast.makeText(SecondActivity.this, "minus", 5000);
                    t2.show();
                    tv.setText("-");
                    int i2 = minus.getBorder();
                    int j2 = minus.getBorder2();
                    tv2.setText(String.valueOf(i2));
                    tv3.setText(String.valueOf(j2));
                    answer = minus.getAnswer();
                    break;
                }
                b.setOnClickListener(new OnClickListener() {

                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        if (answer > 0 && answer == Integer.parseInt(ed.getText().toString())) {
                            Toast t = Toast.makeText(SecondActivity.this, "true",
                                    5000);
                            t.show();
                            this.notify(); // if the answer is true then notify()
                        } else {
                            Toast t = Toast.makeText(SecondActivity.this, "false",
                                    5000);
                            t.show();
                        }

                    }
                }); // end of clicklistner
                counter++; // counting for 5 questions


            }//finally
        }//run

    }; // end of thread
    pause.start();
 } //end of while loop

im still new with android and threads so please be patient with me. Thanks in advance and sorry for my horrible english.

Upvotes: 1

Views: 1650

Answers (1)

Rajesh
Rajesh

Reputation: 15774

The usage of Thread in this context is not really correct.

  • There's a main thread or the UI thread responsible for presenting and managing the UI state. If you want to make any changes to the UI elements like TextView, Button, etc., you'll have to do it in this thread. If you keep long duration tasks in this thread or make this thread wait, your application will become unresponsive and the Android OS will show an ANR dialog. (Read more about it in Keeping Your App Responsive)
  • Threads are usually used when there are concurrent tasks to be executed. In this case, the tasks are not really concurrent, but rather sequential.

You should think more in terms of an event-driven approach where the application takes decision based on events (like question answered, which can be used to check how many questions have been answered to decide whether you need to generate more questions).

Upvotes: 1

Related Questions