| class Demo3 extends Thread {
 private String name;
 Demo3(String name)
 {
 this.name = name;
 }
 public void run()//重写Thread类中的run方法
 {
 for(int x=1; x<10; x++)
 {
 for(int y=1; y<999999999; y++){}
 System.out.println(name+"......x="+x);
 }
 }
 }
 class ThreadDemo
 {
 public static void main(String[] args)
 {
 Demo3 d1 = new Demo3("呵呵");
 Demo3 d2 = new Demo3("hehe");
 d1.start();//开启线程
 d2.start();
 /*
 d1.run();
 d2.run();
 */
 }
 }
 /*调用run()和调用start的区别:
 1.调用run()只是单纯的调用了定义好的run()方法并没有
 实现多线程而调用start是开启多线程的开关实现了多线程
 提高了运算速度,节省了时间。
 2.结果的排序不同,调run()的排序为先D1的内容然后D2的内容 调用start()为D1D2随机分布
 */
 |