线程的创建
Java中由java.lang.Thread类表示线程。创建线程有两种方法:继承Thread类,或者实现Runnable接口来创建。
继承Thread类:
class MyThread extends Thread{
@OVerride
public void run(){
System.out.println("thread name:"+Thread.currentThread().getName());
System.out.println("thread id:"+Thread.currentThread().getId());
}
}
实现Runnable接口:
class MyRunnable implements Runnable{
@OVerride
public void run(){
System.out.println("runnalbe thread name:"+Thread.currentThread().getName());
System.out.println("runnable thread id:"+Thread.currentThread().getId());
}
}
public static void main(String[] args){
Thread t1 = new MyThread();
Thread t2 = new Thread(new MyRunnable());
t1.start();
t2.start();
}
线程的状态
在Java中线程有6个状态:
状态 | 说明 |
---|---|
NEW | 初始状态,线程被创建,但还没有调用start方法 |
RUNNABLE | 运行状态,包含操作系统中的就绪与运行两种状态 |
BLOCKED | 阻塞状态,表示线程阻塞于锁 |
WAITING | 等待状态,进入此状态需要其它线程做一些特定的动作 |
TIME_WAITING | 超时等待,可以在指定的时间自动返回 |
TERMINATED | 终止状态,表示当前线程已经执行完毕 |
线程的优先级
在操作系统中,线程可以划分优先级,优先级较高的线程得到的CPU资源较多,执行的机会较多。
我们可以通过调用下面的方法来设置线程的优先级:
public final void setPriority(int newPriority)
在Java中,线程优先级分为1-10级,如果小于1或大于10,则抛出 IllegalArgumentException异常.
默认的线程优先级为5.
Java中的线程优先级具有继承性,也就是说,如果A线程启动B线程,则默认情况下,B线程的优先级与A线程是一样的。
虽然Java中有优先级,但是线程的调度是由操作系统来完成的,操作系统完全可以不支持线程优先级。因此不可以使用线程的优先级来进行业务的调度。
守护线程
在线程启动之前通过调用thread.setDaemon(true)方法可以将线程设置为守护线程(Daemon线程)。
Deamon线程是一种支持性的线程,因为它主要被用作程序中后台调度以及支持性工作。这意味着,当一个Java虚拟机中不存在非Daemon线程的时候,Java虚拟机将会退出。
需要注意的是,Deamon线程中的finally语句块不一定执行,因此不能依靠finally语句块来执行清理操作和释放资源。
线程的中断
中断可以理解为线程的一个标志位属性,表示一个运行中的线程是否被其它线程进行了中断操作。
Thread类中关于中断的方法有以下3个:
1. public boolean isInterrupted()
2. public void interrupt()
3. public static boolean interrupted()
interrtuped()是一个静态方法,用来判断当前线程是否被中断,并对中断标志复位。也就是说,如果线程线程已经中断,第一次调用会返回true,表示线程已经被中断,第二次调用则返回false.
isInterrupted()与 interrupt()是实例方法,isInterrupted()用于判断线程是否已被中断, interrupt() 方法一般在对应线程实例的外部调用,用于中断对应的线程。
class InterruptThread extends Thread{
public void run(){
while(!this.isInterrupted()){
System.out.println("未中断");
}
System.out.println("是否中断?"+Thread.interrupted());
System.out.println("是否中断?"+Thread.interrupted());
}
}
public static void main(String[] args) throws InterruptedException {
Thread t1 = new InterruptThread();
t1.start();
Thread.sleep(100);
t1.interrupt();
}
当线程中断时,如果调用以下方法,使得线程进入WAITING或TIMED_WAITING状态,
则会抛出InterruptException:
public final void wait() throws InterruptedException
public final native void wait(long timeout) throws InterruptedException;
public static native void sleep(long millis) throws InterruptedException;
public final synchronized void join(long millis) throws InterruptedException;
public final void join() throws InterruptedException
抛出异常后,中断标志位会被清除。
我们在上面的测试代码中使线程调用sleep方法,查看输出,两次都是false:
class InterruptThread extends Thread{
public void run(){
while(!this.isInterrupted()){
System.out.println("未中断");
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("是否中断?"+Thread.interrupted());
System.out.println("是否中断?"+Thread.interrupted());
}
}
public static void main(String[] args) throws InterruptedException {
Thread t1 = new InterruptThread();
t1.start();
Thread.sleep(100);
t1.interrupt();
}
线程的停止
Thread为我们提供了suspend()、resume()和stop()分别表示线程的暂停、恢复和停止,然而它们是废弃的方法,已经不建议使用。
suspend()方法在暂停线程时,不会释放已经占有的资源,而是占着资源进入睡眠状态,这样容易造成死锁。而stop()方法在结束线程时不保证资源的正常释放。
可以利用中断机制来停止线程:
class StopThread extends Thread{
@Override
public void run(){
while(!this.isInterrupted()){
System.out.println("线程执行");
}
System.out.println("线程停止,释放资源");
}
}
public static void main(String[] args) throws InterruptedException {
Thread t1 = new StopThread();
t1.start();
Thread.sleep(100);
t1.interrupt();
}
也可以自定义标志位来进行停止:
class StopThread extends Thread{
private volatile boolean stop = false;
public void cancel(){
this.stop = true;
}
@Override
public void run(){
while(!stop){
System.out.println("线程执行");
}
System.out.println("线程停止,释放资源");
}
}
public static void main(String[] args) throws InterruptedException {
StopThread t1 = new StopThread();
t1.start();
Thread.sleep(100);
t1.cancel();
}