java url连接如何关闭,Java - URLConnections停止连接?

我'm working on a homework assignment that has the purpose of showing how increasing the number of threads can help or hurt a program'的表现 . 基本思想是对来自网站的各个数据请求进行处理,然后确定在同时运行n个查询时执行所有查询所需的时间 .

我认为我已经正确完成了线程和时钟,但是有些奇怪的事情正在发生 . 我正在使用 java.net.URLConnection 来连接数据库 . 我的前三千个左右的连接将成功并加载 . 然后,几百个左右的调用失败,没有任何Java在指定的超时期限内尝试过的证据 .

我在一个线程中运行的代码如下:

/* This code to get the contents from an URL was adapted from a

* StackOverflow question found at http://goo.gl/QPqR4 .

*/

private static String loadContent(String address) throws Exception {

String toReturn = "";

try {

URL url = new URL(address);

URLConnection con = url.openConnection();

con.setConnectTimeout(5000);

con.setReadTimeout(5000);

InputStream stream = con.getInputStream();

Reader r = new InputStreamReader(stream, "ISO-8859-1");

while (true) {

int ch = r.read();

if (ch < 0) {

break;

}

toReturn += (char) ch;

}

r.close();

stream.close();

} catch (Exception e) {

System.out.println(address + ": " + e.getMessage());

throw e;

}

return toReturn;

}

运行线程的代码如下 . NormalPerformance 类是我为简化计算一系列观察的均值和方差而编写的类 .

/* This code is patterned after code provided by my professor.

*/

private static NormalPerformance performExperiment(int threads, int runs)

throws Exception

{

NormalPerformance toReturn = new NormalPerformance();

for (int i = 0; i < runs; i++) {

final List> tasks = new ArrayList>();

for (int j = 0; j < URLS.length; j++) {

final String url = URLS[i];

tasks.add(new Callable() {

public Void call() throws Exception {

loadContent(url);

return null;

}

});

}

long start = System.nanoTime();

final ExecutorService exectuorPool = Executors.newFixedThreadPool(threads);

executorPool.invokeAll(tasks);

executorPool.shutdown();

double time = (System.nano() - start) / 1000000000.;

toReturn.addObservation(time);

System.out.println("" + threads + " " + (i + 1) + ": " + time);

}

return toReturn;

}

为什么我会看到这种奇怪的成功和失败模式?甚至更奇怪的是,有时杀死程序并重新启动无助于阻止故障的运行 . 我已经尝试过强迫线程休眠,调用 System.gc() ,增加连接和读取超时值的事情,但是没有一个单独或组合修复了这个问题 .

我怎样才能保证我的连接最有可能连接?

环境:Windows 7 64位,Eclipse Juno 64位,JRE 7

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Java连接MySQL数据库,需要下载并安装MySQL Connector/J驱动程序。以下是安装步骤: 1. 访问MySQL官方网站,下载MySQL Connector/J驱动程序。当前最新版本为8.0.20,下载地址为:https://dev.mysql.com/downloads/connector/j/ 2. 下载后,将mysql-connector-java-8.0.20.jar文件复制到项目的classpath路径下。 3. 在Java代码中添加连接MySQL数据库的代码,示例代码如下: ``` import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class MySQLConnect { public static void main(String[] args) { Connection conn = null; try { // 加载MySQL驱动程序 Class.forName("com.mysql.cj.jdbc.Driver"); // 获取数据库连接 String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "root"; conn = DriverManager.getConnection(url, user, password); System.out.println("数据库连接成功!"); } catch (SQLException e) { System.out.println("数据库连接失败:" + e.getMessage()); } catch (ClassNotFoundException e) { System.out.println("找不到MySQL驱动程序:" + e.getMessage()); } finally { try { if (conn != null) { conn.close(); } } catch (SQLException e) { System.out.println("关闭数据库连接失败:" + e.getMessage()); } } } } ``` 以上代码演示了如何连接本地MySQL数据库,如果需要连接远程MySQL数据库,只需要将url中的localhost改为远程MySQL服务器的IP地址即可。同时,需要确保远程MySQL服务器已经允许远程连接

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值