![](/img/trans.png)
[英]how can i avoid running of more than one instance on same java project at the same time?
[英]How to avoid a same instance of class java is running many times
我的類名為:ReceiptGenerateAgentDAO receiveGenerateAgentDAO = new 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++);
}
您可以使用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類的方法,請使用: Singleton.getInstance.someMethod()
。
可以用很多方法解決。
方法1:
將該類的范圍更改為會話范圍。 這樣,您的課程將不會在每次請求后被收集。 Class對象將一直保留到會話期滿為止。
方法2:
將類的范圍更改為Singleton。 這樣,您將只為整個用戶會話創建一個對象。 您可以在以下步驟中執行此操作。
步驟1:創建該類的靜態對象,並使用空值對其進行初始化
Private Static ClassName staticClassName=null;
步驟2:阻止構造函數,以使ClassName obj = new ClassName()不會為您帶來新的對象。
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
使用此方法初始化Object類,它將始終返回相同的對象。
ClassName obj= ClassName.findClassObject();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.