java 线程
进程(Process)和线程(Thread)是程序执行中的两个基本单元,在JAVA并发编程中更关心JAVA线程。
线程(Process)
一个进程是一个自包含的执行环境(可以被看作是程序或应用程序)。一个程序中可以包含多个线程。JAVA运行(JAVA runtime)时作为一个进程运行,可以包含不同的类和程序作为进程。
进程(Thread)
线程可称为轻量级进程,线程的创建只需要较少的资源并存在于进程中,线程共享进程资源。
Java多线程例子
每个JAVA应用程序至少有一个线程:主线程(Main thread)。 尽管还有诸如:内存管理,系统管理,信号处理等其他线程在后台运行,但从应用的角度来看:主线程(Main thread)是JAVA应用的第一个线程,我们可以从主线程(Main thread)中创建多个线程。
多线程是指在单个程序中并发执行的两个或多个线程。单核CPU一次只能执行一个线程,而时间片是OS功能,可以共享不同进程和线程之间的处理器时间。
多线程优点
- 与进程相比线程是轻量级的,创建线程花费的时间和资源更少。
- 线程共享其父进程数据和代码
- 线程之间的上下文切换通常比进程之间更节约资源。
- 线程相互通信比进程通信相对容易
java 中创建线程的两种方式:
- 实现
java.lang.Runnable
接口 - 继承
java.lang.Thread
类
Demo 如下:
// 实现 runnable 接口
public class HeavyWorkRunnable implements Runnable {
@Override
public void run() {
System.out.println("Doing heavy processing - START "+Thread.currentThread().getName());
try {
Thread.sleep(1000);
//Get database connection, delete unused data from DB
doDBProcessing();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Doing heavy processing - END "+Thread.currentThread().getName());
}
private void doDBProcessing() throws InterruptedException {
Thread.sleep(5000);
}
}
// 继承 Thread 类
public class MyThread extends Thread {
public MyThread(String name) {
super(name);
}
@Override
public void run() {
System.out.println("MyThread - START "+Thread.currentThread().getName());
try {
Thread.sleep(1000);
//Get database connection, delete unused data from DB
doDBProcessing();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("MyThread - END "+Thread.currentThread().getName());
}
private void doDBProcessing() throws InterruptedException {
Thread.sleep(5000);
}
}
// 测试类
public class ThreadRunExample {
public static void main(String[] args){
Thread t1 = new Thread(new HeavyWorkRunnable(), "t1");
Thread t2 = new Thread(new HeavyWorkRunnable(), "t2");
System.out.println("Starting Runnable threads");
t1.start();
t2.start();
System.out.println("Runnable Threads has been started");
Thread t3 = new MyThread("t3");
Thread t4 = new MyThread("t4");
System.out.println("Starting MyThreads");
t3.start();
t4.start();
System.out.println("MyThreads has been started");
}
}
// 输出:
Starting Runnable threads
Runnable Threads has been started
Doing heavy processing - START t1
Doing heavy processing - START t2
Starting MyThreads
MyThread - START Thread-0
MyThreads has been started
MyThread - START Thread-1
Doing heavy processing - END t2
MyThread - END Thread-1
MyThread - END Thread-0
Doing heavy processing - END t1
Runnable vs Thread
如果你的类有更多的功能,不仅仅是作为线程来运行,你应该实现Runnable接口。如果你的类只作为线程运行,你可以继承Thread类。
建议采用实现Runnable接口的方式实现多线程,因为JAVA支持实现多个接口,但只能继承一个父类。
Tip: 注意线程没有任何返回值,如果你希望线程处理完之后有返回值请查看Callable和Future接口。
Update: 从JAVA8起,Runnable接口成为了一个Functional interface
,因此可以使用lambda表达式代替匿名内部类来实现Runnable接口,更多相关的知识请阅读Java 8 Functional Interfaces
线程生命周期
使用多线程编程时,了解线程生命周期非常重要。JAVA线程的生命周期如下:
下图展示了JAVA中线程的生命周期,我们可以在代码中中创建一个新的线程并启动它,但是该线程的状态迁移比如:从Runnable到Running再到Blocked并不是由JAVA程序控制的而是由线程调度程序的OS控制的。
New:当我们用new
关键字创建一个新的线程对象时,线程的状态是New Thread
,此时,线程是不存在,new 出来的这个对象它只是Java程序内部的一个变量。
Runnable:当我们在线程对象上执行start()
方法后,这个线程对象的状态迁移为Runnable
,对这个线程的控制将被转交给线程调度程序(Thread scheduler),是立即执行这个线程还是将这个线程保持为Runnable
状态并放在runnable线程池中,完全依赖OS的thread scheduler实现。