1 /* 2 * 匿名内部类的格式: 3 */ 4 public class ThreadDemo { 5 public static void main(String[] args) { 6 // 继承thread类实现多线程 7 new Thread() { 8 public void run() { 9 for (int x = 0; x < 100; x++) { 10 System.out.println(Thread.currentThread().getName() + "--" 11 + x); 12 } 13 } 14 }.start(); 15 ; 16 17 // 实现runnable借口,创建多线程并启动 18 new Thread(new Runnable() { 19 @Override 20 public void run() { 21 for (int x = 0; x < 100; x++) { 22 System.out.println(Thread.currentThread().getName() + "--" 23 + x); 24 } 25 } 26 }) { 27 }.start(); 28 29 // 更有难度的,在Thread匿名内部类的里面再一次重写run方法 30 //在实际运行时的结果是 hello+x。以thread的run方法为准。但是此处无意义 31 new Thread(new Runnable() { 32 @Override 33 public void run() { 34 // TODO Auto-generated method stub 35 for (int x = 0; x < 100; x++) { 36 System.out.println("java" + "--" + x); 37 } 38 39 } 40 }) { 41 public void run() { 42 for (int x = 0; x < 100; x++) { 43 System.out.println("hello" + "--" + x); 44 } 45 } 46 }.start(); 47 } 48 }
时间: 2024-12-28 07:39:04