在一个线程android中的wait()和notify()

时间:2013-05-21 13:12:27

标签: android wait notify

我正在写一个教育数学游戏,你可以同时选择多个操作,现在我试图生成5个问题,但它会快速运行所有五个问题,除了最后一个问题我无法回答它们因为它被困在那里。所以我想创建一个线程,在创建第一个问题后等待(等待它解决并正确回答),然后继续下一个问题等等......现在我从未使用过等待并通知之前所以我应该分配它们 在这里我到目前为止,但它给了我一个例外:

 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

我仍然是新的Android和线程所以请耐心等待我。 提前谢谢,对不起我可怕的英语。

1 个答案:

答案 0 :(得分:1)

在此上下文中使用Thread并不正确。

  • 有一个主线程或UI线程负责呈现和管理UI状态。如果您想对TextViewButton等UI元素进行任何更改,则必须在此主题中执行此操作。如果您在此线程中保留较长持续时间的任务或使此线程等待,则您的应用程序将无响应,并且Android OS将显示ANR对话框。 (在Keeping Your App Responsive
  • 中阅读更多相关信息 当要执行并发任务时,通常使用
  • Thread s。在这种情况下,任务不是真正的并发,而是顺序。

您应该根据事件驱动的方法考虑更多,其中应用程序根据事件做出决策(例如回答的问题,可以用来检查已经回答了多少问题以决定是否需要生成更多问题)