首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >即使变量更新了,whlie循环也不会退出。

即使变量更新了,whlie循环也不会退出。
EN

Stack Overflow用户
提问于 2019-11-15 07:30:42
回答 2查看 75关注 0票数 1

我已经创建了一个嵌套类,它扩展了AsyncTask以从数据库中提取数据。当提取完成后,它将全局变量("notificationsExtracted")设置为true,以表示流程已经完成。

在主调用代码中,我设置了一个while循环,在使用提取的数据之前,该循环等待全局变量变为真。

代码语言:javascript
运行
复制
while(!notificationsExtracted) {}
...now continue running other code...

除了一部手机外,所有手机都能很好地工作,但是one phone (Conexis x2 -Android7.0)拒绝将全局变量设置为true。

当我进行日志记录时,它显示了实例化类、提取数据工作、将全局变量设置为true的代码流,然后什么也没有。在其他手机上,它执行上述操作,但在主程序中继续运行进一步的代码。

简单地说,我在调用程序中有以下内容

代码语言:javascript
运行
复制
public class FragmentMain extends Fragment {
  static private Boolean notificationsExtracted;
...
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment_main_layout, parent, false);
    ...
    Log.i("notif", "1");
    notificationsExtracted = false;
    GetNotificationsNewMySQL getNotificationsNewMySQL = new GetNotificationsNewMySQL();
    getNotificationsNewMySQL.execute("");
    while (!notificationsExtracted) { };
    Log.i("notif", "2");
    ...

然后嵌套类

代码语言:javascript
运行
复制
private class GetNotificationsNewMySQL extends AsyncTask<String, Void, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... params) {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection con = DriverManager.getConnection(url, user, pass);

                Statement st = con.createStatement();
                String q = "SELECT COUNT(*) AS cnt FROM training.app_notifications";
                Log.i("notif", "a");
                ResultSet rs = st.executeQuery(q);
                Log.i("notif", "b");
                ResultSetMetaData rsmd = rs.getMetaData();
                Log.i("notif", "c");
                while (rs.next()) {
                    notificationCount = Integer.parseInt(rs.getString(1).toString());
                }
                Log.i("notif", "d");
                notificationsExtracted = true;
            } catch (Exception e) {
                Log.i("notif", "error extracting");
                e.printStackTrace();
            }
            if (notificationsExtracted)
                Log.i("notif", "true");
            else
                Log.i("notif", "false");
            return "";
        }

        @Override
        protected void onPostExecute(String result) {
        }
    }

现在,在所有的手机上,除了一部手机,它记录了以下的程序流序列

代码语言:javascript
运行
复制
2019-11-15 11:18:40.338 1951-1951/com.example.pdcapp I/Notif: 1
2019-11-15 11:18:40.339 1951-1951/com.example.pdcapp I/Notif: 2
2019-11-15 11:18:40.438 1951-1951/com.example.pdcapp I/notif: 1
2019-11-15 11:18:40.512 1951-2025/com.example.pdcapp I/notif: a
2019-11-15 11:18:40.521 1951-2025/com.example.pdcapp I/notif: b
2019-11-15 11:18:40.521 1951-2025/com.example.pdcapp I/notif: c
2019-11-15 11:18:40.522 1951-2025/com.example.pdcapp I/notif: d
2019-11-15 11:18:40.522 1951-2025/com.example.pdcapp I/notif: true
2019-11-15 11:18:40.522 1951-1951/com.example.pdcapp I/notif: 2

除了一部手机之外,我得到了以下信息,其结果是手机挂起,也就是说,它永远不会通过while循环:

代码语言:javascript
运行
复制
2019-11-15 11:20:57.153 20089-20089/com.example.pdcapp I/Notif: 1
2019-11-15 11:20:57.154 20089-20089/com.example.pdcapp I/Notif: 2
2019-11-15 11:20:57.196 20089-20089/com.example.pdcapp I/notif: 1
2019-11-15 11:20:57.267 20089-20348/com.example.pdcapp I/notif: a
2019-11-15 11:20:57.274 20089-20348/com.example.pdcapp I/notif: b
2019-11-15 11:20:57.274 20089-20348/com.example.pdcapp I/notif: c
2019-11-15 11:20:57.274 20089-20348/com.example.pdcapp I/notif: d
2019-11-15 11:20:57.274 20089-20348/com.example.pdcapp I/notif: true

请帮我一把。作为参考,我以前在下面的链接https://androidforums.com/threads/while-loop-not-exiting.1315976/上查找帮助

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-11-15 07:47:09

您所做的工作违背了异步执行的原则。如果您启动了一个asynchTask,但是等到它结束时,它并不是真正的异步。

不过,这是可以的,有时您只需要按一定的顺序完成一些事情,但不能在主线程上完成。

我不太清楚为什么你的程序在技术层面上不起作用,但我想我曾经遇到过类似的问题。我相信,在Java中,从一个线程中更新变量并不一定也会更新所有其他线程的变量。Android是否在时间循环上给你一个警告?

我希望有更多Java多线程经验的人能对此有所了解。

但是,解决问题的方法很简单:

您希望在AsyncTask完成execute之后执行want循环后的任何代码,所以只需在onPostExcecute方法中执行该代码即可。

票数 0
EN

Stack Overflow用户

发布于 2019-11-15 07:47:01

请在获得字符串q、ResultSet和ResultSetMetaData的行之后添加记录器好吗?也许是在这条线上乱塞。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58872213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档