您的位置:首页 > 其它

匿名内部类在多线程中的应用

2016-09-22 14:25 176 查看
package thread;

public class InTreadTest {

public static void main(String[] args) {

//第三第四种利用了匿名内部类,分别是第二、第一种的简写版本

//第一种
Runnable s=new haha();
Thread t=new Thread(s);
t.start();

//第二种
Thread w=new hehe();
w.start();

//第三种
new Thread(){
public void run(){
for(int x=0;x<20;x++)
System.out.println(x);
}
}.start();

//第四种
Runnable r=new Runnable(){

public void run(){
for(int x=0;x<20;x++)
System.out.println(x);
}
};
new Thread(r);
}
}

class haha implements Runnable{

public void run(){
for(int x=0;x<20;x++)
System.out.println(x);
}
}

class hehe extends Thread{

public void run(){
for(int x=0;x<20;x++)
System.out.println(x);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  设计模式 多线程