
Interesting work of the garbage collector
Problem
Java was dealt with recently, and faced an interesting problem associated with the dynamic redefinition of methods at runtime. After exiting the overridden method, the reference to the object was lost. The solution to the problem was delayed due to the fact that it was on one computer and not on another.
Code Structure
I have a Resident class.
Bot is inherited from the Resident class. And the Thurn method is redefined.
In the World class, we have an array of residents.
After leaving residents [i] [j]. Turn (), residents [i] [j], it was null. And then everything flew by exception. As I said on some computers, the problem was, and on some not.
Why
Because the Bot is declared in the Program class. And when you enter the Thorn bot method, the link to it is lost and already at the output the garbage collector deletes the object. The problem is solved either like this:
* This source code was highlighted with Source Code Highlighter .
Or by creating a bot in Word through the w.AddBot () method.
PS. I may be wrong in determining the cause of this. I will be glad to hear opinions
Java was dealt with recently, and faced an interesting problem associated with the dynamic redefinition of methods at runtime. After exiting the overridden method, the reference to the object was lost. The solution to the problem was delayed due to the fact that it was on one computer and not on another.
Code Structure
I have a Resident class.
public class Resident {
/ * Variables * /
int age;
String trace;
boolean live;
int energy;
String name;
int id;
int x;
int y;
World world
/ * Body * /
public void Turn () {
age ++;
}
}
Bot is inherited from the Resident class. And the Thurn method is redefined.
public class Bot extends Resident {
/ * Variables * /
Queue m;
Vector moves = new Vector ();
/ * Constrctors * /
public Bot (int energy, String name, World world) {
this.energy = energy;
this.name = name;
this.trace = "";
if (this.energy <1) {
this.live = false;
} else {
live = true;
}
this.world = world;
this.age = 0;
}
/ * Body * /
public void Turn () {
age ++;
energy--;
if (! moves.isEmpty ()) {
world.ResidentAction (this, (ACTION) moves.lastElement ());
moves.remove (moves.size () - 1);
}
}
}
In the World class, we have an array of residents.
public class World {
/*Variables*/
String name;
int width;
int height;
Resident[][] residents;
/*Constrctors*/
public World(String name, int height, int width) {
this.height = height;
this.name = name;
this.width = width;
this.residents = new Resident[height][width];
}
/*Body*/
public void AddBot() {
Bot b = new Bot(100, "B", this);
residents[1][2] = b;
b.X(1);
b.Y(2);
}
public void AddBot(Bot b) {
residents[1][2] = b;
b.X(1);
b.Y(2);
}
public void AddBot(Bot b, int x, int y) {
residents[x][y] = b;
b.X(x);
b.Y(y);
}
public void Turn() {
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (residents[i][j] != null) {
residents[i][j].Turn();
if (!residents[i][j].isLive())
residents[i][j] = null;
}
}
}
}
}
В Мейне. Создаем объект типа Ворлд и Бот, и запихиваем Бота в Ворлд по координатам 1,2. Потом пробуем выполнить метод Турн для ворлда.
public class Programm {
public static void main(String[] args) {
World w = new World("Test1", 100, 100);
Bot b = new Bote(20, "B", w)
w.AddBot(b);
w.Turn();
}
}
* This source code was highlighted with Source Code Highlighter.
After leaving residents [i] [j]. Turn (), residents [i] [j], it was null. And then everything flew by exception. As I said on some computers, the problem was, and on some not.
Why
Because the Bot is declared in the Program class. And when you enter the Thorn bot method, the link to it is lost and already at the output the garbage collector deletes the object. The problem is solved either like this:
public class Programm {
static Bot b;
public static void main (String [] args) {
World w = new World ("Test1", 100, 100);
b = new Bot (20, "", w);
w.AddBot (b);
w.Turn ();
}
}
* This source code was highlighted with Source Code Highlighter .
Or by creating a bot in Word through the w.AddBot () method.
PS. I may be wrong in determining the cause of this. I will be glad to hear opinions