我的课名为:ReceiptGenerateAgentDAO receiptGenerateAgentDAO = 新的ReceiptGenerateAgentDAO();这个类将从jsp中调用 页面,有一个方法,我有一个for循环 执行超过5分钟,问题是我请求jsp 调用类,类开始执行,现在我重新加载jsp 并再次调用类现在这个类都运行它不是一个 线程,如何可能以及如何解决它?
for (String Key : list) {
if (!"userDetailsCache".equals(Key)) {
preparedStatement2.setString(1, Key);
@Cleanup
ResultSet rsCreditReceipt = preparedStatement2.executeQuery();
if (rsCreditReceipt.next()
&& rsCreditReceipt.getString("SUMMARY_DATE") != null) {
String exBalance = rsCreditReceipt.getString("balance");
/** Agent Current Balance */
preparedStatement3.setString(1,
rsCreditReceipt.getString("SUMMARY_DATE"));
preparedStatement3.setString(2, Key);
@Cleanup
ResultSet rsCreditBal = preparedStatement3.executeQuery();
double creditDebit = Double.parseDouble(exBalance);
if (rsCreditBal.next()) {
if (rsCreditBal.getString("creditdebit") != null) {
creditDebit += Double.parseDouble(rsCreditBal
.getString("creditdebit"));
}
}
if (creditDebit < 0) {
Element element = EhcacheManager.getUserDetailsCache()
.get(Key);
UserDetails details = (UserDetails) element
.getObjectValue();
/** Agent Opening Balance */
getZoneDetails(details, connection);
receiptGenerateTempDTOs.add(new ReceiptGenerateTempDTO(
details.getTerritoryId(), details
.getSatelliteId(), details.getZoneId(),
rsCreditReceipt.getString(3),
NumericConstants.ZERO, String.valueOf(formatter
.format((creditDebit * (-1)))), time,
receiptGenerateAgentDTO.getSelectedUserId(),
NumericConstants.ZERO));
}
} else {
/** If no opening balance,only current Balance */
preparedStatement4.setString(1, Key);
@Cleanup
ResultSet rsCreditBal = preparedStatement4.executeQuery();
if (rsCreditBal.next()) {
Element element = EhcacheManager.getUserDetailsCache()
.get(Key);
UserDetails details = (UserDetails) element
.getObjectValue();
getZoneDetails(details, connection);
receiptGenerateTempDTOs
.add(new ReceiptGenerateTempDTO(
details.getTerritoryId(),
details.getSatelliteId(),
details.getZoneId(),
details.getUserId(),
NumericConstants.ZERO,
String.valueOf(formatter.format((Double.parseDouble(rsCreditBal
.getString("creditdebit")) * (-1)))),
time, receiptGenerateAgentDTO
.getSelectedUserId(),
NumericConstants.ZERO));
}
}
}
System.out.println(i++);
}
答案 0 :(得分:0)
您可以使用Singleton Pattern创建一个只有一个实例的类:
public class Singleton {
private static Singleton instance = null;
private Singleton() {
// Is private to be not accessible
}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
如果要调用单例类的方法,请使用:Singleton.getInstance.someMethod()
。
答案 1 :(得分:0)
可以通过多种方式解决..
方法1:
将类的范围更改为会话范围。 这样,您的课程将不会在每次请求后收集垃圾。 Class对象将保留到会话到期为止。
方法2:
将类的范围更改为Singleton。通过这种方式,您将为整个用户会话创建一个对象。您可以通过以下步骤执行此操作。
第1步: 创建类的静态Object,并使用空值
初始化它Private Static ClassName staticClassName=null;
第2步: 阻止构造函数,以便ClassName obj = new ClassName()不会得到一个新的Object。
private ClassName() {....}
第3步 创建另一个方法findClassObject(),它返回类的对象
public static ClassName findClassObject(){
if(staticClassName==null){
staticClassName= new ClassName() ; //since the constructor method is called from within the class, it returns a new Object of the Class ClassName
}
return staticClassName;
}
Setp 4
使用this初始化类Object,它将始终返回相同的对象。
ClassName obj= ClassName.findClassObject();