一旦所有线程完成执行,如何返回值

时间:2017-02-01 07:42:19

标签: java android multithreading

我想要取大号 来自SQLite数据库的数据采用xml格式,之前我正在这样做 UI线程我得到了ANR并且花了很多时间 提取数据,所以我尝试了这段代码,这段代码工作正常 但仍然给我一个ANR对话框。

请建议更好的方法。

    private Map<String, String> getSaleParams()  {


                            final String[] invSaleMasterXML = {""};
                            final String[] invSaleTransactionXML = {""};
                            final String[] invSaleVoucherXML = {""};
                            final Map<String, String> params = new HashMap<>();
                            final DatabaseHandler databaseHandler = ApplicationController.getDatabaseHandler(mContext);

                            Runnable saleMasterRunnable = new Runnable() {
                                @Override
                                public void run() {
                                    invSaleMasterXML[0] =databaseHandler.
                                    getAllInventoryMasterRecords();
                                    params.put("InvSaleMasterXML",
invSaleMasterXML[0]);
                                }
                            };
                            Thread saleMasterThread = new Thread(saleMasterRunnable);

                            Runnable saleTransactionRunnable = new Runnable() {
                                @Override
                                public void run() {
                                invSaleTransactionXML[0] = databaseHandler.
                                getAllInventoryTransactionRecords();
                                params.put("InvSaleTransactionXML",
                                invSaleTransactionXML[0]);
                                }
                            };
                            Thread saleTransactionThread = new 
                            Thread(saleTransactionRunnable);
                            Runnable saleVoucherRunnable = new Runnable() {
                                @Override
                                public void run() {
                                    invSaleVoucherXML[0] =
                                    databaseHandler.getVoucherTableRecords();
                                    params.put("InvSaleVoucherXML", invSaleVoucherXML[0]);
                                }
                            };
                            Thread saleVoucherThread = new Thread(saleVoucherRunnable);
                            saleMasterThread.start();
                            saleTransactionThread.start();
                            saleVoucherThread.start();
                            params.put("UserID",
ApplicationController.getSessionManager(mContext).getUserId());


                            try {
                                saleMasterThread.join();
                                saleTransactionThread.join();
                                saleVoucherThread.join();
                                return params;
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            return params;
                    }

1 个答案:

答案 0 :(得分:0)

<IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond $1 !^(index\.php|css|js|images|robots\.txt) RewriteRule ^(.*)$ index.php?$1 [L] </IfModule> 仍会导致UI线程等到结果加载完毕。删除这些,并实现一个侦听器方案。这样,UI线程就不会被阻止。

完成数据获取操作后,UI线程会收到通知并可以更新视图。在此之前,您可以显示加载栏或类似的内容。

.join()