黑马程序员技术交流社区

标题: 多线程、匿名类问题 [打印本页]

作者: 奔跑的二叉树    时间: 2013-9-14 17:04
标题: 多线程、匿名类问题
本帖最后由 奔跑的二叉树 于 2013-9-14 19:47 编辑
  1. package cn.baidu.com;

  2. public class ThreadTest {

  3.         public static void main(String[] args) {
  4.                 new Thread()
  5.                 {
  6.                         public void run()
  7.                         {
  8.                                 for(int x=0;x<100;x++)
  9.                                 {
  10.                                         System.out.println(Thread.currentThread().getName()+"......"+x);
  11.                                 }
  12.                         }
  13.                 }.start();
  14.                
  15.                 for(int x=0;x<100;x++)
  16.                 {
  17.                         System.out.println(Thread.currentThread().getName()+"......"+x);
  18.                 }
  19.                
  20.                 Runnable r=new Runnable()
  21.                 {
  22.                         public void run()
  23.                         {
  24.                                 for(int x=0;x<100;x++)
  25.                                 {
  26.                                         System.out.println(Thread.currentThread().getName()+"......"+x);
  27.                                 }
  28.                         }
  29.                 };
  30.                 new Thread(r).start();
  31.                
  32.                
  33.                

  34.         }

  35. }
复制代码
看多线程最后一个视频,老师写了三种线程写法,有些不理解,哪位老大给分析一下,第一,第二个线程的写法

作者: 李锡碧    时间: 2013-9-14 17:12
你去看看内部类的视频,第一个是new了一个Thread的子类(重写run方法)。第二个是主线程。
作者: 471686505    时间: 2013-9-14 17:16
顶一下  黑马的同学  加油 !!!!
作者: 肖勇    时间: 2013-9-14 17:22
第一种方法是直接继承Thread类,覆盖run方法,这就是使这个类具有了多线程的特性,在run方法中可以定义自定义代码。而第二种方法是实现Runnable 接口,Runnable接口本身和线程没有联系,只有new Thread 然后将Runnable 对象传递作为参数传递进去,使之与联系才能够具有多线程的特性。

作者: 奔跑的二叉树    时间: 2013-9-14 17:37
肖勇 发表于 2013-9-14 17:22
第一种方法是直接继承Thread类,覆盖run方法,这就是使这个类具有了多线程的特性,在run方法中可以定义自定 ...

看了下内部类的视频,明白了,我想,现在有了Lock,那synchronized,wait,notify什么的是不是可以去死了呢

作者: Inspur    时间: 2013-9-14 17:52
本帖最后由 Inspur 于 2013-9-14 17:58 编辑

创建线程的第一种方式:定义类继承Thread ,然后复写run方法。通过创建Thread类的子类对象,调用线程的start方法,开启线程,并执行run方法。
创建线程的第二种方式:实现一个接口Runnable。覆盖接口中的run方法(用于封装线程要运行的代码)。通过Thread类创建线程对象;将实现了Runnable接口的子类对象作为实际参数传递给Thread类中的构造函数。调用Thread对象的start方法。开启线程,并运行Runnable接口子类中的run方法。
改代码代码中

用到了匿名内部类,直接创建了Thread类的子类对象,覆盖run方法,并调用start方法启动线程。

同样

创建了Runnable接口的子类对象,覆盖run方法,然后将Runnable接口的子类对象r作为参数传递,启动线程。


作者: Bad_Boy    时间: 2013-9-14 18:21
  1. public static void main(String[] args) {
  2.            //这是通过一个继承了Thread类的匿名子类创建的线程
  3.             new Thread()
  4.             {
  5.                     public void run()
  6.                     {
  7.                             for(int x=0;x<100;x++)
  8.                             {
  9.                                     System.out.println(Thread.currentThread().getName()+"......"+x);
  10.                             }
  11.                     }
  12.             }.start();
  13.            //这个线程是调用当前线程也就是main方法的那个线程
  14.             for(int x=0;x<100;x++)
  15.             {
  16.                     System.out.println(Thread.currentThread().getName()+"......"+x);
  17.             }
  18.            //这种方法是通过实现了Runnable接口的匿名子类创建的线程
  19.             Runnable r=new Runnable()
  20.             {
  21.                     public void run()
  22.                     {
  23.                             for(int x=0;x<100;x++)
  24.                             {
  25.                                     System.out.println(Thread.currentThread().getName()+"......"+x);
  26.                             }
  27.                     }
  28.             };
  29.             new Thread(r).start();
  30.            
  31.            
  32.            

  33.     }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2