Java 多线程 --- 创建线程, 线程状态
如何创建线程
使用Thread类
- 创建一个类继承Thread类
- 重写该接口的run()方法,该run()方法的方法体是该线程的线程执行体
package SimpleThread;
public class SimpleThread extends Thread {
static int count = 0;
//重写run方法, 具体业务逻辑的实现
@Override
public void run() {
while(true) {
System.out.println("Hello: " + count);
try {
Thread.sleep(1000);
count++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
//创建Cat对象,可以当做线程使用
SimpleThread thread = new SimpleThread();
//启动线程
thread.start();
}
}
使用Runnable接口
- 创建一个类实现Runnable接口, 并重写该接口的run()方法,该run()方法的方法体同样是该线程的线程执行体。
- 创建Runnable实现类的实例, 然后使用Runnable实例创建一个Thread实例,
- 调用Thread实例的start方法开始线程
package SimpleThread;
public class SimpleRunnable {
//创建一个类实现Runnable接口
public static class TaskOne implements Runnable{
static int count = 0;
//重写该接口的run()方法
@Override
public void run() {
while(true) {
count++;
System.out.println("taskOne: " + count);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//创建一个类实现Runnable接口
public static class TaskTwo implements Runnable{
static int count = 0;
//重写该接口的run()方法
@Override
public void run() {
while(true) {
count++;
System.out.println("taskTwo: " + count);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
//创建Runnable实现类的实例
TaskOne taskOne = new TaskOne();
TaskTwo taskTwo = new TaskTwo();
//使用Runnable实例创建Thread实例
Thread t1 = new Thread(taskOne);
Thread t2 = new Thread(taskTwo);
//调用Thread实例的start方法开始线程
t1.start();
t2.start();
}
}
使用Runnable接口和继承Thread类的区别 (continue)
使用Runnable接口
- 优点
- 使用Runnable接口的类还可以继承其他类
- 多个线程可以共享一个target对象
- 缺点
- 编程稍微复杂, 比如要访问当前线程, 则必须使用Thread.currentThread()方法
继承Thread类
- 优点
编写简单, 比如需要访问当前线程,则直接使用this
- 缺点
因为已经继承了Thread类, 则不能继承其他父类
线程状态
New Threads
- 当用new符号创建线程时, 比如
new Thread(r)
. 这个线程还不在运行状态, 而属于new threads.- 在这个阶段, 会进行线程的资源分配
Runnable Threads
- 当一个thread调用statr方法时, 这个线程处于
runnable
状态- 一个runnable状态的线程可能在也可能不在运行, 取决于操作系统有没有让这个线程运行
- Java specification 没有区分 running和runnable, 一个running thread依然是runnable state
- Always keep in mind that a runnable thread may or may not be running at any given time
Blocked and Waiting Threads
- 阻塞状态(Blocked) 阻塞状态是线程因为某种原因放弃CPU使用权,进行上下文切换, 暂时停止运行。直到线程进入就绪状态,才有机会转到运行状态。阻塞的情况分三种:
- 等待阻塞:运行的线程执行wait()方法,JVM会把该线程放入等待池中。
- 同步阻塞:运行的线程在获取对象的同步锁时,若该同步锁被别的线程占用,则JVM会把该线程放入Entry Set中.
- 其他阻塞:运行的线程执行sleep()或join()方法,或者发出了I/O请求时,JVM会把该线程置为阻塞状态。当sleep()状态超时、join()等待线程终止或者超时、或者I/O处理完毕时,线程重新转入就绪状态。
Terminated Threads
- It dies a natural death because the run method exits normally. (包括使用interrupt)
- It dies abruptly because an uncaught exception terminates the run method
线程优先级 Thread Priorities
- 每一个线程都有优先级
- 一个线程默认继承父类线程的优先级, 但是也可以通过
setPriority
设置线程优先级. FromMIN_PRIORITY
(数值为1) toNORM_PRIORITY
(数值为5)- 当scheduler选择线程时, 优先选择优先级高的线程.
- 线程优先级根据系统不同也会有改变. Java虚拟机会根据host操作系统改变对应的系统优先级, 比如Windows有7个优先级, 有些Java的优先级会有一样的操作系统优先级. In the Oracle JVM for Linux, thread priorities are ignored altogether—all threads have the same priority
- 如果有几个线程的优先级很高, 则低优先级的线程可能会处于饥饿状态
- 所以不要将程序的正确性依赖于线程优先级.
守护线程 Daemon Thread
- 当一个线程的唯一作用是服务于其他线程时, 比如timer thread给其他线程发送"timer ticks" , 这个线程被叫做Daemon Thread.
- 可以通过
t.setDaemon(true)
将一个线程设置为Daemon Thread- 当只有daemon thread 存在时, JVM会自动退出.
- 不要让Daemon Thread使用persistent resource, 比如数据或者文件. 因为daemon thread可能随时被终止.