Repeated initialization variables cause OutofMemoryError

xiaoxiao2021-03-06  66

Public synchronized void refresh () {

While (this.keys ()! = null && this.keys (). HasMoreElements ()) {

String ticketid = this.keys (). NextElement (). TOSTRING ();

Ticket T = this.getticket (ticketid);

IF (t.isexpired ()) {

This.deleteticket (TicketID);

System.out.println ("Ticket" Ticketid "(" T.Username

") HAS been deleded (Expired).");

}

}

}

Starting this program When you run, you will fall into the dead cycle, and finally report OutofMemoryError, and then use this.keys () when you discover the while loop. HasMoreElements () Every cycle is a new Enumeration object, the cursor is always in the first Element, so HasMoreElements is True, which is falling into a dead cycle. The code is modified to solve the problem as follows.

Public synchronized void refresh () {

ENUMERATION E = this.keys ();

While (e! = null && e.hasmorelements ()) {

String ticketid = E.NEXTELEMENT (). TOSTRING ();

Ticket T = this.getticket (ticketid);

IF (t.isexpired ()) {

This.deleteticket (TicketID);

}

T = NULL;

Ticketid = NULL;

}

}

转载请注明原文地址:https://www.9cbs.com/read-90053.html

New Post(0)