Java Reference
In-Depth Information
}
v
=
new
new
Vector
<
Sprite
>();
// multithreaded, use Vector
}
public
public
void
void
actionPerformed
(
ActionEvent e
) {
System
.
out
.
println
(
"Creat-ing another one!"
);
Sprite s
=
new
new
Sprite
(
this
this
,
img
);
s
.
start
();
p
.
add
(
s
);
v
.
add
(
s
);
}
public
public
void
void
stop
() {
for
for
(
int
int
i
=
0
;
i
<
v
.
size
();
i
++) {
v
.
get
(
i
).
stop
();
}
v
.
clear
();
}
}
Stopping a Thread
Problem
You need to stop a thread.
Solution
Don't use the
Thread.stop()
method; instead, use a
boolean
tested at the top of the main
loop in the
run()
method.
Discussion
Though you can use the thread's
stop()
method, Sun recommends against it. That's because
the method is so drastic that it can never be made to behave reliably in a program with mul-
tiple active threads. That is why, when you try to use it, the compiler will generate depreca-
tion warnings. The recommended method is to use a
boolean
variable in the main loop of
shutDown()
method is called; it then sets the controlling variable
done
to false, which ter-