6

我很难理解这一点。这是正在发生的事情。我正在生成一个新线程,该线程拥有与 Oracle 数据库的 JDBC 连接。当我要求它连接到数据库时,父线程在调用 start() 方法时继续运行,但是当我要求子线程执行查询(在单独的方法上)时,父线程卡住等待子线程的方法来完成它的工作。任何猜测如何解决这个问题?提前致谢!

public class Main extends Thread{

    public Main()
    {
    }

    public void myCounter() {
        int i = 0;
        DBConnection myConnection = null;
        for(;;)
        {
            i++;

            System.out.println("time: " + i);
            if( i  == 5)
            {
                myConnection = new DBConnection("localhost", 1521, "hr", "hr", "XE");
                myConnection.start();


            }
            if(i == 10)
                try {

                    myConnection.runQuery("Select * from hr.numbers order by dbms_random.value");
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void run()
    {
        myCounter();
    }

    public static void main(String[] args) {

        Main boot = new Main();
        boot.start();

    }

}

public class DBConnection extends Thread{

    Connection myConnection;
    int port;
    String user;
    String password;
    String serviceName;
    String host;


    public void run()
    {
        setUpConnection(host, port, user, password, serviceName);
    }
    /**
     * Sets up variables to create a connection to Oracle.
     *  
     * @param host      host
     * @param port      port
     * @param user      user
     * @param password  password
     */
    public DBConnection(String host, int port, String user, String password, String serviceName)
    {
        this.host = host;
        this.port = port;
        this.user = user;
        this.password = password;
        this.serviceName = serviceName;
    }


    private void setUpConnection(String host, int port, String user,
            String password, String dataBase) {
        System.out.println("-------- Oracle "
                + "JDBC Connection Testing ------------");

        try {

            Class.forName("oracle.jdbc.OracleDriver");

        } catch (ClassNotFoundException e) {

            System.out.println("Couldn't find Oracle JDBC Driver... :-(");
            e.printStackTrace();
            return;

        }

        System.out.println("Oracle JDBC Driver Registered!");
        myConnection = null;
        try {
            myConnection = DriverManager.getConnection(
                    "jdbc:oracle:thin:@//" 
                            + host 
                            + ":" 
                            + port 
                            + "/" 
                            + dataBase,
                            user, password
                    );

        } catch (SQLException e) {

            System.out.println("Connection Failed!");
            e.printStackTrace();
            return;

        }

        if (myConnection != null) {
            System.out.println("Connected to Oracle! :-)");
        } else {
            System.out.println("Failed to make connection!");
        }
    }


    /**
     * Queries the database and returns a ResultSet
     * @param  query            SQL
     * @throws SQLException 
     */
    public  ResultSet runQuery(String query) throws SQLException 
    {
        System.out.println("                                                    [DBConnection] Started Running @ " + (new SimpleDateFormat("HH:mm:ss:S")).format(new Date()));
        ResultSet rs = null;

        Statement stt = myConnection.createStatement();
        rs = stt.executeQuery(query);
        System.out.println("                                                    [DBConnection] Finished Running @: " + (new SimpleDateFormat("HH:mm:ss:S")).format(new Date()));
        return  rs;

    }

这是我得到的输出:

time: 1
time: 2
time: 3
time: 4
time: 5
-------- Oracle JDBC Connection Testing ------------
Oracle JDBC Driver Registered!
time: 6
Connected to Oracle! :-)
time: 7
time: 8
time: 9
time: 10
                                                    [DBConnection] Started Running @ 14:46:00:660
                                                    [DBConnection] Finished Running @: 14:46:12:750
time: 11
time: 12
time: 13
time: 14

………………

4

3 回答 3

6

我认为您误解了线程的工作原理。问题是您在父线程上调用它:

myConnection.runQuery("Select * from hr.numbers order by dbms_random.value");

这是对恰好是线程的对象的runQuery方法的顺序调用。这并不意味着它将指示孩子执行该方法。相反,父线程将自己执行它,并且子线程在其方法返回后立即完成。myConnectionrun

如果你想有一个单独的线程不断接收命令来执行查询,你将不得不实现一个生产者-消费者模式,在这种模式下,父进程一直在排队等待子进程执行的命令。为此,我建议您查看ExecutorService

于 2012-08-21T13:58:30.250 回答
2

当您调用 时myConnection.runQuery(),您在父线程的方法中,因此即使您调用其方法之一,run()您也不在线程中。connection

你应该调用runQuery里面的run()方法myConnection来实现你想要的。

于 2012-08-21T13:59:57.067 回答
1

你的runQuery方法是阻塞的,因为你在主线程上调用它(所以它会等到查询完成)。如果您希望在另一个线程上调用它,请在另一个线程run方法上调用它(只需启动您最初为查询方法调用的另一个线程)

于 2012-08-21T14:00:44.917 回答