多线程匿名内部类实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/S031302306/article/details/82118888
package cn.itcast.innerclass.startThread;
/**
 * 匿名内部类的格式:
 * 		new 类名或者接口名(){
 * 			重写方法;
 * 		};
 * 	本质:是该类或者接口的子类对象
 * 
 */
public class InnerClassStartThread {
	public static void main(String[] args) {
		
		//继承Thread类来实现多线程
		new Thread(){
			public void run(){
				for(int x=0; x<100; x++){
					System.out.println(Thread.currentThread().getName()+" : "+x);
				}
			}
		}.start();
		
		//实现Runnable接口来实现多线程
		new Thread(new Runnable(){
			@Override
			public void run() {
				for(int x=0;x<100;x++){
					System.out.println(Thread.currentThread().getName()+" : "+x);
				}				
			}			
		}){}.start();
		
		new Thread(new Runnable(){
			@Override
			public void run() {
				for(int x=0;x<100;x++){
					System.out.println("hello" + " : " + x);
				}				
			}			
		}){
			public void run() {
				for(int x=0;x<100;x++){
					System.out.println("world" + " : " + x);    // 执行此方法
				}				
			}	
			
			
		}.start();
		
	}
}

猜你喜欢

转载自blog.csdn.net/S031302306/article/details/82118888