1.继承Thread
步骤:
- 定义Thread类的子类,并重写该类的run()方法,该run()方法的方法体就代表了线程需要完成的任务,因此把run()方法称为线程执行体。
- 创建Thread子类的实例,即创建了线程对象。
- 调用线程对象的start()方法来启动该线程。
public class TextThead1 extends Thread {
@Override
public void run() {
//run方法方法体
for (int i = 0; i < 60; i++) {
System.out.println("run方法"+i);
}
}
public static void main(String[] args) {
TextThead1 th1 = new TextThead1();
th1.start();
for (int i = 0; i < 500; i++) {
System.out.println("主线程"+i);
}
}
}
2.实现Runnable接口
步骤:
- 定义Runnable接口的实现类,并重写该接口的run()方法,该run()方法的方法体同样是该线程的线程执行体。
- 创建Runnable实现类的实例,并以此实例作为Thread的target来创建Thread对象,Thread对象才是真正的线程对象。
- 调用线程对象的start()方法来启动线程。
public class implRunnable implements Runnable{
@Override
public void run() {
//run方法方法体
for (int i = 0; i < 60; i++) {
System.out.println("run方法"+i);
}
}
public static void main(String[] args) {
implRunnable implrunnable =new implRunnable();
/* Thread thread =new Thread(implrunnable);
thread.start();*/
new Thread(implrunnable).start();
for (int i = 0; i < 500; i++) {
System.out.println("主线程"+i);
}
}
}
***继承Thread与实现Runnable两种方式的比较
Thread类本质上是实现了Runnable接口的一个实例,代表一个线程的实例。
* Unless otherwise noted, passing a {@code null} argument to a constructor
* or method in this class will cause a {@link NullPointerException} to be
* thrown.
*
* @author unascribed
* @see Runnable
* @see Runtime#exit(int)
* @see #run()
* @see #stop()
* @since JDK1.0
*/
public
class Thread implements Runnable {
/* Make sure registerNatives is the first thing <clinit> does. */
private static native void registerNatives();
static {
registerNatives();
}
实现Runnable接口比继承Thread类所具有的优势
- 适合多个相同的程序代码的线程去共享同一个资源。
- 可以避免java中的单继承的局限性。
- 增加程序的健壮性,实现解耦操作,代码可以被多个线程共享,代码和数据独立。
- 线程池只能放入实现Runable或callable类线程,不能直接放入继承Thread的类。
在线程的生命周期一共有6种状态
public enum State {
/**
* Thread state for a thread which has not yet started.
*/
NEW,(新建) 线程刚被创建,但是并未启动。
/**
* Thread state for a runnable thread. A thread in the runnable
* state is executing in the Java virtual machine but it may
* be waiting for other resources from the operating system
* such as processor.
*/
RUNNABLE,(可运行)
线程可以在java虚拟机中运行的状态,可能正在运行自己代码,也可能没有,这取决于操作系统处理器。
/**
* Thread state for a thread blocked waiting for a monitor lock.
* A thread in the blocked state is waiting for a monitor lock
* to enter a synchronized block/method or
* reenter a synchronized block/method after calling
* {@link Object#wait() Object.wait}.
*/
BLOCKED,阻塞当一个线程试图获取一个对象锁,而该对象锁被其他的线程持有,
则该线程进入Blocked状态;当该线程持有锁时,该线程将变成Runnable状态。
*线程的线程状态被阻塞,等待监视器锁定。
*处于阻塞状态的线程正在等待监视器锁定
*输入同步块/方法或
*调用后重新输入同步块/方法
/**
* Thread state for a waiting thread.
* A thread is in the waiting state due to calling one of the
* following methods:
* <ul>
* <li>{@link Object#wait() Object.wait} with no timeout</li>
* <li>{@link #join() Thread.join} with no timeout</li>
* <li>{@link LockSupport#park() LockSupport.park}</li>
* </ul>
*
* <p>A thread in the waiting state is waiting for another thread to
* perform a particular action.
*
* For example, a thread that has called <tt>Object.wait()</tt>
* on an object is waiting for another thread to call
* <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
* that object. A thread that has called <tt>Thread.join()</tt>
* is waiting for a specified thread to terminate.
*/
WAITING,(无限等待)
一个线程在等待另一个线程执行一个(唤醒)动作时,该线程进入Waiting状态。
进入这个状态后是不能自动唤醒的,必须等待另一个线程调用notify或者notifyAll方法才能够唤醒。
/**
* Thread state for a waiting thread with a specified waiting time.
* A thread is in the timed waiting state due to calling one of
* the following methods with a specified positive waiting time:
* <ul>
* <li>{@link #sleep Thread.sleep}</li>
* <li>{@link Object#wait(long) Object.wait} with timeout</li>
* <li>{@link #join(long) Thread.join} with timeout</li>
* <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
* <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
* </ul>
*/
TIMED_WAITING,(计时等待)
同waiting状态,有几个方法有超时参数,调用他们将进入Timed Waiting状态。
这一状态将一直保持到超时期满或者接收到唤醒通知。带有超时参数的
常用方法有Thread.sleep 、Object.wait。
/**
* Thread state for a terminated thread.
* The thread has completed execution.
*/
TERMINATED;(被终止)
因为run方法正常退出而死亡,或者因为没有捕获的异常终止了run方法而死亡
}