带线程的 Java Servlet 会阻塞吗?

标签 java multithreading servlets

我正在尝试使用自己的连接池形式来处理数据库连接,但即使我使用了线程,它似乎也会由于某种原因而阻塞,有人可以帮我指出我的错误吗。

这是带有线程类的 servlet 代码。

protected void processRequest(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    // PrintWriter out = response.getWriter();
    OutputStream ostream = response.getOutputStream();
    PrintStream out = new PrintStream(ostream, true, "UTF8");

    try {

        response.setContentType("text/html");
        String test = request.getParameter("test");
        System.out.println("Received text: " + test);

        if(test.equals("free"))
        {
            for (int i = 0; i < list.size(); i++) {
                dbcm.freeConnection(list.get(i));
            }
            list.clear();
        }else
        {
            GetConnThread gct = new GetConnThread(test, dbcm);
            gct.start();
        }

    } catch (Exception e) {
        e.printStackTrace();
        out.println("fail");
    } finally {
        out.close();
    }

}



private class GetConnThread extends Thread
{
    private String test;
    private DBConnectionManager dbcm;

    public GetConnThread(String test, DBConnectionManager dbcm)
    {
        this.test = test;
        this.dbcm = dbcm;
    }

    public void run()
    {
        try {
            Connection conn = dbcm.getConnection(test);
            list.add(conn);             
            System.out.println(conn);
            System.out.println("list size: " + list.size());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

这是DBConnectionManager中的getConnection方法

    public synchronized Connection getConnection(String test) throws CGFatalException {
        Connection con = null;
        boolean connectionIsValid = false;
        if (freeConnections.size() > 0) {

            con = (Connection) freeConnections.firstElement();
            freeConnections.removeElementAt(0);

            connectionIsValid = validateConnection(con);
            if (connectionIsValid == false) {
                con = getConnection(test);
            }
        } else if (maxConn == 0 || checkedOut < maxConn) {
            con = newConnection();
            connectionIsValid = validateConnection(con);
            if (connectionIsValid == false) {
                con = getConnection(test);
            }
        }else
        {
            System.out.println("No available connections for " + test + ", try again in 2 secs....");
            try {
                Thread.sleep(2000);
                con = getConnection(test);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (con != null && connectionIsValid == true) {
            checkedOut++;
        }
        System.out.println("checkedOut: " + checkedOut);
        System.out.println("maxConn: " + maxConn);
        return con;
    }

我将最大连接数设置为 2,因此在我第三次调用 servlet 后,它会转到以下代码行:

System.out.println("No available connections for " + test + ", try again in 2 secs....");

当我第四次打电话时,我在期待

System.out.println("No available connections for " + test + ", try again in 2 secs....");

作为一个单独的线程启动,但第三个调用似乎阻塞了它,预计会出现无限循环,因为我希望调用“free”来清除连接,然后一切都会恢复正常。

最佳答案

您的 getConnection 方法已同步。所有其他线程都将阻塞该锁获取,直到“第三个”请求成功获取连接并继续。

关于带线程的 Java Servlet 会阻塞吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9255107/

相关文章:

java - Java SE 应用程序生命周期是什么......或者它没有生命周期?

java - 如何创建包含枚举列表的枚举常量?

Java volatile 关键字 - 我需要它吗?

Java : ReentrantReadWriteLock with priority

java - 如何通过 Java EE Web 应用程序将文件存储在服务器(Web 容器)上?

java - 在 catch block 中返回的代码是否干净?

java - 如何在 weblogic 中更改企业应用程序 (.ear) 的上下文根

java.lang.RuntimeException : How do I solve this crash? 是什么原因造成的?

java - Android/Java多线程-线程直到主线程完成工作才继续

java - tomcat中线程的文件问题