简体   繁体   English

多线程错误

[英]Multiple thread error

I have an app where I need to use AsyncTask but I am having the issue that the main thread is going too far and ends up crashing my app because my Connect1 thread has not retrieved the information yet needed to continue. 我有一个需要使用AsyncTask的应用程序,但是我遇到了一个问题,即主线程走得太远,最终导致我的应用程序崩溃,因为我的Connect1线程尚未检索到需要继续的信息。 I was wondering how could I have the thread wait till the AsyncTask thread dies out and then the main thread can continue. 我想知道如何让线程等待AsyncTask线程消失,然后主线程可以继续。

Code: 码:

private void gNameOriginTag() {

    TextView tV;

    Connect1 connect1 = new Connect1();
    connect1.execute();

    // Set the long name for the chosen.
    tV = (TextView) view.findViewById(R.id.gLName);
    tV.setText(columns.get(4));      //<<<< Error is here.

    ....
}

Connect1 AsyncTask: Connect1 AsyncTask:

private class Connect1 extends AsyncTask<String, Void, String>{

    @Override
    protected String doInBackground(String... params) {

        Connection conn = null;
        Statement stmt = null;

        try {
            // STEP 2: Register JDBC driver
            Class.forName(JDBC_DRIVER);

            // STEP 3: Open a connection
            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            // STEP 4: Execute a query
            stmt = conn.createStatement();

            // STEP 5a: Extract data from result set
            ResultSet rs = stmt.executeQuery("SELECT * FROM gs WHERE name ='"
                    + gSelected + "'");
            ResultSetMetaData rsmd = (ResultSetMetaData) rs.getMetaData();
            int x = 1;
            while (rs.next()) {
                while (x < rsmd.getColumnCount()) {

                    // Retrieve Strings & Add it to the ArrayList.
                    columns.add(rs.getString(x));
                    x++;
                }

            }
            rs.close();
            stmt.close();
            conn.close();

        } catch (SQLException se) {
            // Handle errors for JDBC
            se.printStackTrace();
        } catch (Exception e) {
            // Handle errors for Class.forName
            e.printStackTrace();
        } finally {
            // finally block used to close resources
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException se2) {
            }// nothing we can do
            try {
                if (conn != null)
                    conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }// end finally try
        }// end try

        return "";
    }
}

The reason why I am doing this is because android 2.3.3 doesn't run mysql on the main thread from what I learned so I am trying to learn AsyncTask, so what I am trying to accomplish in "Code:" is somehow make the main thread wait/join (I also read up that pausing the main thread is bad so I am even more lost on what to do) up for the Connect1 thread to finish so that: 我这样做的原因是因为android 2.3.3不在我学到的主线程上运行mysql,所以我试图学习AsyncTask,所以我试图在“代码:”中完成的工作以某种方式使主线程等待/连接(我也读过,暂停主线程很不好,所以我什至不知道该怎么做)准备完成Connect1线程,以便:

tV.setText(columns.get(4)); 

can be retrieved so my app does not crash. 可以被检索,因此我的应用程序不会崩溃。

Define a method that is called by the AsyncTask when it is finished and that is then run on the UIThread again. 定义一个方法,该方法由AsyncTask完成时调用,然后再次在UIThread上运行。

MainThread: calls AsyncTask MainThread:调用AsyncTask

AsyncTask: does the work and calls doWhenDoneWithBackgroundWork AsyncTask:完成工作并调用doWhenDoneWithBackgroundWork

doWhenDoneWithBackgroundWork: does some work on MainThread again doWhenDoneWithBackgroundWork:再次对MainThread做一些工作

您可以在异步任务的onPostExecute方法上编写代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM