【发布时间】:2019-11-15 07:30:42
【问题描述】:
我创建了一个嵌套类,它扩展了 AsyncTask 以从数据库中提取数据。 提取完成后,它会将一个全局变量(“notificationsExtracted”)设置为 true,表示该过程已完成。
在主调用代码中,我设置了一个 while 循环,等待该全局变量变为真,然后再使用提取的数据,即
while(!notificationsExtracted) {}
...now continue running other code...
在除一部之外的所有手机上,这都能完美运行,但一部手机 (Conexis x2 - Android 7.0) 拒绝反映全局变量设置为 true。
当我进行日志记录时,它显示了实例化类、提取数据、将全局变量设置为 true 的代码流,然后什么也没有。 在其他手机上,它会执行上述操作,但随后会继续在主程序中运行更多代码。
简单来说,我在调用程序中有以下内容
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");
...
然后是嵌套类
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) {
}
}
现在在除一部手机之外的所有手机上,它都会记录以下程序流程序列
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 循环:
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/寻求帮助
【问题讨论】:
-
异步任务的重点是在不同的线程中执行阻塞的长任务,以避免阻塞 UI 线程并冻结您的应用程序。有一个 while 循环除了吃掉所有的 CPU 并冻结 UI 线程直到“后台”任务完成之外什么都不做,这比不使用异步任务更糟糕(而且你也在以非线程安全的方式执行它)。阅读如何正确使用异步任务,因为你这样做的方式是死路一条。
-
asyncTask 需要
-
因为它不是易失性的,也不是原子的,也不是从 snchrnized 块访问的。所以没有可见性保证一个线程完成的写入对另一个线程可见。
-
但是如果是静态全局变量,为什么线程不能更新呢?
-
一个线程可以。但是 Java 内存模型不保证另一个线程会看到更改,除非使用了适当的同步(易失性、原子性、同步)。多线程是一件复杂的事情。
标签: java variables android-asynctask global