java 匿名內部類說明 及其多執行緒實現繼承Thread,實現Runnable
阿新 • • 發佈:2018-12-26
原文:http://tjukk.iteye.com/blog/2047663
匿名內部類也就是沒有名字的內部類
正因為沒有名字,所以匿名內部類只能使用一次,它通常用來簡化程式碼編寫
但使用匿名內部類還有個前提條件:必須繼承一個父類或實現一個介面
例項1:不使用匿名內部類來實現抽象方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
abstract class Person
{
public abstract void eat();
}
class Child extends Person
{
public void eat()
{
System.out.println( "eat
something" );
}
}
public class Demo
{
public static void main(String[]
args) {
Person
p = new Child();
p.eat();
}
}
|
執行結果:eat something
可以看到,我們用Child繼承了Person類,然後實現了Child的一個例項,將其向上轉型為Person類的引用
但是,如果此處的Child類只使用一次,那麼將其編寫為獨立的一個類豈不是很麻煩?
這個時候就引入了匿名內部類
例項2:匿名內部類的基本實現
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
abstract class Person
{
public abstract void eat();
}
public class Demo
{
public static void main(String[]
args) {
Person
p = new Person()
{
public void eat()
{
System.out.println( "eat
something" );
}
};
p.eat(); }
}
|
執行結果:eat something
可以看到,我們直接將抽象類Person中的方法在大括號中實現了
這樣便可以省略一個類的書寫
並且,匿名內部類還能用於介面上
例項3:在介面上使用匿名內部類
interface Person
{
public void eat();
}
public class Demo
{
public static void main(String[]
args) {
Person
p = new Person()
{
public void eat()
{
System.out.println( "eat
something" );
}
};
p.eat();
}
}
|
執行結果:eat something
由上面的例子可以看出,只要一個類是抽象的或是一個介面,那麼其子類中的方法都可以使用匿名內部類來實現
最常用的情況就是在多執行緒的實現上,因為要實現多執行緒必須繼承Thread類或是繼承Runnable介面
例項4:Thread類的匿名內部類實現
public class Demo {
public static void threadMethod(String name){
Thread t = new Thread(name) {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.print(Thread.currentThread().getName()+":"+i + " ; ");
}
}
};
t.start();
}
public static void main(String[] args) {
threadMethod("執行緒1");
threadMethod("執行緒2");
}
}
執行結果(不確定):執行緒1:1 ; 執行緒1:2 ; 執行緒1:3 ; 執行緒2:1 ; 執行緒1:4 ; 執行緒2:2 ; 執行緒1:5 ; 執行緒2:3 ; 執行緒2:4 ; 執行緒2:5 ;
例項5:Runnable介面的匿名內部類實現
public class Demo {
public static void threadMethod(){
Runnable r = new Runnable() {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.print(Thread.currentThread().getName()+":"+i + " ; ");
}
}
};
Thread t1 = new Thread(r, "執行緒1");
Thread t2 = new Thread(r, "執行緒2");
t1.start();
t2.start();
}
public static void main(String[] args) {
threadMethod();
}
}
執行結果(不確定):執行緒1:1 ; 執行緒2:1 ; 執行緒1:2 ; 執行緒2:2 ; 執行緒1:3 ; 執行緒2:3 ; 執行緒1:4 ; 執行緒2:4 ; 執行緒2:5 ; 執行緒1:5 ; --------------------------------------------------------------- ps:上面的繼承Thread和實現Runnable是常用的 之前沒寫過多執行緒,也記一下下面的吧
package com;
public class TestRunnable implements Runnable{
private int i = 10;
@Override
public void run() {
System.out.println("TestRunnable class "+Thread.currentThread().getName()+":"+(--i));
}
public static void main(String[] args) {
TestRunnable t1 = new TestRunnable();
TestRunnable t2 = new TestRunnable();
//執行出的結果的順序不確定 變數i copy
new Thread(t1).start();
new Thread(t2).start();
//執行出的結果的順序不確定 變數i 共享
/*new Thread(t1,"thread1").start();
new Thread(t1,"thread2").start();*/
}
}
執行結果:
TestRunnable class Thread-1:9TestRunnable class Thread-0:9 /* TestRunnable class thread2:9
TestRunnable class thread1:8 */
package com;
public class TestRunnable1 implements Runnable{
private int i = 10;
@Override
public void run() {
System.out.println("TestRunnable1 class "+Thread.currentThread().getName()+":"+(--i));
}
public static void main(String[] args) {
TestRunnable t1 = new TestRunnable();
TestRunnable1 t2 = new TestRunnable1();
//執行出的結果的順序不確定
new Thread(t1).start();
new Thread(t2).start();
}
}
執行結果:
TestRunnable1 class Thread-1:9
TestRunnable class Thread-0:9