实例4:Thread类的匿名内部类实现[backcolor=rgb(255, 255, 255) !important][size=1em][backcolor=rgb(244, 244, 244) !important]public class Demo {
public static void main(String[] args) {
[backcolor=rgb(244, 244, 244) !important] Thread t = new Thread() {
public void run() {
[backcolor=rgb(244, 244, 244) !important] for (int i = 1; i <= 5; i++) {
System.out.print(i + " ");
[backcolor=rgb(244, 244, 244) !important] }
}
[backcolor=rgb(244, 244, 244) !important] };
t.start();
[backcolor=rgb(244, 244, 244) !important] }
}
|
运行结果:1 2 3 4 5
实例5:Runnable接口的匿名内部类实现[backcolor=rgb(255, 255, 255) !important][size=1em][backcolor=rgb(244, 244, 244) !important]1
2
[backcolor=rgb(244, 244, 244) !important]3
4
[backcolor=rgb(244, 244, 244) !important]5
6
[backcolor=rgb(244, 244, 244) !important]7
8
[backcolor=rgb(244, 244, 244) !important]9
10
[backcolor=rgb(244, 244, 244) !important]11
12
[backcolor=rgb(244, 244, 244) !important]13
| [backcolor=rgb(244, 244, 244) !important]public class Demo {
public static void main(String[] args) {
[backcolor=rgb(244, 244, 244) !important] Runnable r = new Runnable() {
public void run() {
[backcolor=rgb(244, 244, 244) !important] for (int i = 1; i <= 5; i++) {
System.out.print(i + " ");
[backcolor=rgb(244, 244, 244) !important] }
}
[backcolor=rgb(244, 244, 244) !important] };
Thread t = new Thread(r);
[backcolor=rgb(244, 244, 244) !important] t.start();
}
[backcolor=rgb(244, 244, 244) !important]}
|
|
|