我写了以下代码。我希望当控制变为 1 时,无限循环应该停止。否则,它会在一段时间后继续更新 SQL 数据库。某种异常导致线程终止。我无法弄清楚。任何人都可以帮助它。
public void run()
{
while(true)
{
System.out.println("I am working t asfbjakhbfjabf");
synchronized(lock)
{
if(control==1)
{
return;
}
}
try
{
Thread.sleep(wTIME);
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
String st="SELECT count FROM ThreadResults WHERE ThreadNo = "
+t;
Statement stmt = conn.createStatement();
stmt.execute(st);
ResultSet resultSet = stmt.getResultSet();
boolean b=resultSet.next();
synchronized(WebCrawler.seed.lock6)
{
float t1=(System.currentTimeMillis()-time)/60000;
if(b)
{
String s2="UPDATE ThreadResults SET count = "+WebCrawler.seed.count+", time = "+t1+" WHERE ThreadNo = "+t;
System.out.println("updated count");
stmt.executeUpdate(s2);
}
else
{
String s1="INSERT ThreadResults VALUES("+t+" ,"+WebCrawler.seed.count+" ,"+t1+")";
System.out.println("inserting count");
stmt.executeUpdate(s1);
}
}
resultSet.close();
conn.close();
}
catch(InterruptedException | ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException c)
{
System.out.println("caught in thread resilt updation "+ c.getMessage());
}
}
}