Thread actressThread=new Thread(new Actress(),"MsRunnable")为什么创建Actress()实例用要用Thread的方法
Thread actressThread=new Thread(new Actress(),"MsRunnable")为什么创建Actress()实例用要用Thread的方法
Thread actressThread=new Thread(new Actress(),"MsRunnable")为什么创建Actress()实例用要用Thread的方法
2016-09-06
查看api可知道 Runnable 接口,只提供了run()方法。所以只实现了 Runnable 接口的类并不能启动线程。当你再查看Thread的api时,有多个重载的构造函数比如这个:
public Thread(Runnable target, String name);
启动线程只能用Thread里面的start() 方法。
Thread actressThread = new Thread(new Actress(),"MsRunnable"); // 可写成 Actress actress = new Actress(); Thread actressThread = new Thread(actress, "MsRunnable"); // 此时 actreeThread 调用的 start() 方法时,也就调用了 actress 中的 run() 方法. actressThread.start();
举报