一、Thread类的构造方法
public Thread();
public Thread (Runnable target);
public Thread (Runnable target,String name);
public Thread (String name);
public Thread (ThreadGroup group,Runnable target);
public Thread (ThreadGroup group,Runnable target,String name);
public Thread (ThreadGroup group,String name);
二、Java中提供了2种创建线程的方法:
(1) 继承Thread类(java.lang.Thread)
public class Thread extends Object implements Runnable
这种方法是将类声明为Thread
的子类。该子类应重写Thread
类的run
方法。接下来可以分配并启动该子类的实例。
实例一:
运行结果:
(2)利用Runnable接口(java.lang.Runnable)
这种方法是声明实现Runnable
接口的类。该类然后实现run
方法。然后可以分配该类的实例,在创建Thread
时作为一个参数来传递并启动
实例二:
运行结果: