【问题标题】:How create a button which pause the thread which is inside the loop and another button which resumes如何创建一个暂停循环内线程的按钮和另一个恢复的按钮
【发布时间】:2023-03-20 23:48:01
【问题描述】:
what is wrong with following code.

做一些事情说 words.lenght=1000 次
然后假设用户想在两者之间休息
单击 id = bt 的暂停按钮此按钮暂停线程,直到用户单击 id= bt1 的恢复

Runnable myRun = new Runnable(){
public void run(){

   for(int j =0 ;j<=words.length;j++){

   synchronized(this){
       try {

            wait(sleepTime);

            bt.setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {

                            try {
                                wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                }});
            bt2.setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {
                    notify();

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

        } }
   runOnUiThread(new Runnable(){
       public void run(){
           try {
                et.setText(words[i]);
                i++;
            } catch (Exception e) {
                e.printStackTrace();
            }
       }});
  }}};

【问题讨论】:

  • 代码中是否有任何错误或正在询问您的方法。
  • @fahad 我在问方法

标签: android multithreading runnable synchronized


【解决方案1】:

代码包含不匹配的右大括号。除此之外,它的格式很糟糕。首先,您应该在向人们寻求建议之前进行修复。其次,我认为混乱的格式也让您感到困惑,因为其中一些与您的描述完全不符。

【讨论】:

  • 感谢您的建议..我是 android 和 java 的新手..如果您了解我的要求,请帮助我提供一些“代码”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多