興味深いガベージコレクターの仕事

問題
私は最近Javaの作業を開始し、実行時のメソッドの動的な再定義に関連する興味深い問題に直面しました。 オーバーライドされたメソッドを終了した後、オブジェクトへの参照が失われました。 問題の解決は、1台のコンピューター上にあり、別のコンピューター上にはないという事実のために遅れました。


コード構造
常駐クラスがあります。

パブリック クラス Resident {
/ *変数* /
int age;
文字列トレース;
boolean live;
intエネルギー;
文字列名;
int id;
int x;
int y;
世界の世界

/ *本体* /
public void Turn(){
年齢++;
}
}


ボットはResidentクラスから継承されます。 Thurnメソッドが再定義されました。

パブリック クラス BotはResident {
/ *変数* /
キューm;
ベクターの移動= 新しいベクター();

/ *建設業者* /
パブリックボット( int energy、文字列名、世界世界){
この .energy = energy;
this .name = name;
this .trace = "" ;
ifthis .energy <1){
this .live = false ;
} else {
live = true ;
}
this .world = world;
this .age = 0;
}

/ *本体* /
public void Turn(){
年齢++;
エネルギー-;
if (!moves.isEmpty()){
world.ResidentAction( this 、(ACTION)moves.lastElement());
moves.remove(moves.size()-1);
}
}
}


ワールドクラスには、多数の居住者がいます。

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;
bX(1);
bY(2);
}

public void AddBot(Bot b) {
residents[1][2] = b;
bX(1);
bY(2);
}

public void AddBot(Bot b, int x, int y) {
residents[x][y] = b;
bX(x);
bY(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 .
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;
bX(1);
bY(2);
}

public void AddBot(Bot b) {
residents[1][2] = b;
bX(1);
bY(2);
}

public void AddBot(Bot b, int x, int y) {
residents[x][y] = b;
bX(x);
bY(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 .
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;
bX(1);
bY(2);
}

public void AddBot(Bot b) {
residents[1][2] = b;
bX(1);
bY(2);
}

public void AddBot(Bot b, int x, int y) {
residents[x][y] = b;
bX(x);
bY(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 .

住民[i] [j]を出た後、ターン()、住民[i] [j]、nullでした。 そして、すべてが例外的に飛んだ。 一部のコンピューターで言ったように、問題はそうでしたが、そうでないものもありました。

なぜ
ボットはProgramクラスで宣言されているためです。 また、Thornボットメソッドを入力すると、そのリンクは失われ、出力で既にガベージコレクターがオブジェクトを削除します。 問題は次のように解決されます。

パブリック クラス Programm {
静的ボットb;

public static void main(String [] args){
World w = new World( "Test1" 、100、100);
b = 新しいボット(20、 "" 、w);
w.AddBot(b);
w。ターン();

}
}


*このソースコードは、 ソースコードハイライターで強調表示されました。
または、w.AddBot()メソッドを使用してWordでボットを作成します。

PS。 この原因を特定するのは間違っているかもしれません。 私は意見を聞いてうれしいです

Source: https://habr.com/ru/post/J68577/


All Articles