线程的创建
1.继承Thread,重写run方法, 创建 Thread 对象 调用 Thread 对象的start方法,让线程进入就绪状态
这种方式没什么好说的,继承Thread类,重写run方法就行
2.实现 Runnable 接口,实现接口中run方法,并且创建 Thread对象,将Runnable任务对象传递进Thread的构造方法,然后在调用 Thread 对象的start方法,源码调用流程:当线程被调度,执行Thread类的run方法,线程对象的run方法判断传进来的Runnable任务对象不为null,则执行任务对象的Runnable 的run方法
贴上Thread类中 run方法的源码
@Override
public void run() {
//这里的target就是Runnable接口的子类
if (target != null) {
target.run();
}
}
3.创建FutureTask 有返回值的任务 对象,(此类实现了Runnable 也是一个任务对象,所以也可以传入到Thread对象的构造方法中),构造函数传入Callable 接口的子类,(实现接口中的call方法,此方法最终为线程要执行的代码)并且创建 Thread对象,将FutureTask任务对象传递进Thread的构造方法,然后在调用 Thread 对象的start方法
源码调用流程:当线程被调度,执行Thread对象run方法,线程对象的run方法判断传进来的任务对象不为null,则执行任务对象的Runnable接口子类 的run方法,构造函数传进来的Runnable子类为FutureTask,则调用FutureTask的run方法,在FutureTask的run方法中,调用了传进来的Callable 接口的子类 call方法,(线程真正执行的代码)
贴上Runnable接口的字类 FutureTask类中 run方法的源码
public void run() {
if (state != NEW ||
!UNSAFE.compareAndSwapObject(this, runnerOffset,
null, Thread.currentThread()))
return;
try {
Callable<V> c = callable;
if (c != null && state == NEW) {
V result;
boolean ran;
try {
//在FutureTask的run方法中,调用了传进来的Callable 接口的子类 call方法
result = c.call();
ran = true;
} catch (Throwable ex) {
result = null;
ran = false;
setException(ex);
}
if (ran)
set(result);
}
} finally {
// runner must be non-null until state is settled to
// prevent concurrent calls to run()
runner = null;
// state must be re-read after nulling runner to prevent
// leaked interrupts
int s = state;
if (s >= INTERRUPTING)
handlePossibleCancellationInterrupt(s);
}
}
Thread 线程对象 (可以继承Thread类,重写run方法,但是这样任务和线程搅合在一块,耦合度太高)
Runnable 接口抽象出来的任务对象(抽离的好处低耦合)FutureTask也实现了Runnable,所以他也是一个任务对象‘
线程重要的api
start : 调用此方法,线程进入就绪模式,等待调度
run 方法:线程真正执行的代码
sleep: 调用此方法线程进入阻塞等待状态,阻塞状态的线程,不会被调度,不会占用cpu
join: 调用此方法,(等待某一个线程执行完成,自己才会继续往下运行)阻塞状态的线程,不会被调度,不会占用cpu
interrupt : 打断线程。调用此方法,将线程进行打断,可以通过打断标记来停止结束线程,where 死循环场景。当线程在 sleep,join,wait 状态时被打断,会抛出异常,并且会将打断标记清空,重置为false,那么我们可以在异常catch中 再次调用interrupt方法来打断线程,使得标记变为true(两阶段终止模式场景使用),非阻塞正常运行的线程被打断,打断标记会为true,注意!!! 被打断的线程并不会停止 run方法线程代码的执行,需要手动去结束线程运行,比如break 跳出循环
线程的状态
操作系统层面线程的五种状态:
new (线程被创建吗,调用start方法之前)
就绪状态 ,(线程调用start方法,进入就绪状态)
运行状态 , (被调度器调度,得到cpu时间片。正在运行的线程,时间片用完线程会重新进入就绪状态,等下下一次调度器的调度,运行状态,到就绪状态的切换,会发生线程上下文切换)
阻塞状态, (线程调用sleep,jion,wait 等方法,进入阻塞状态)
线程死亡结束状态 (线程结束,java程序,所有线程全部执行完毕,java进程才会结束)
java 层面 api线程的六种状态
在线程Thread类中 内部类 state 明确表明的六种状态如下
NEW 新建状态(线程被创建吗,调用start方法之前)
RUNNABLE (包含了操作系统层面的 就绪,运行,阻塞状态,这三种状态在java debug 模式下,查看,线程显示的都为running==RUNNABLE状态)
BLOCKED 等待锁的过程,进入BLOCKED状态
WAITING 阻塞状态(例如调用的join方法)
TIMED_WAITING 有时效的阻塞状态(例如调用的sleep方法)
TERMINATED (线程结束,java程序,所有线程全部执行完毕,java进程才会结束)
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,
/**
* 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,
/**
* 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,
/**
* 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,
/**
* Thread state for a terminated thread.
* The thread has completed execution.
*/
TERMINATED;
}