Java只是一门语言,多线程最终的执行还是操作系统对其本身资源的调度,不同的操作系统: Window,Mac,Linux对其资源的调度有其不同的算法,Java语言的出现,能够使得我们程序开发者能够轻松的与这些操作系统,轻松的打交道,Thread的出现是我们与操作系统的方式,Thread类是对线程的抽象描述。
我们通过Thread表达我们的想法,JVM将我们的这些想法传递给操作系统,从而实现我们与操作系统的一次对话。
- 我们通过Thread代表线程的抽线
- Java语言分装了Thread,在Thread中有一个native方法start0
- 应用在不同的操作系统的JVM实现了start0,该start0调用了该操作系统下提供的api.
- 操作系统通过api接收到我们表达的想法,然后执行。
Thread与Runnable的关系
Thread是一个代理类,核心业务的实现是Runnable的run方法,在整个过程中应用了代理设计模式。
// Thread是继承了Runnable接口的
public class Thread implements Runnable
/* 代理的目标target*/
private Runnable target;
// 线程构造器中,接收的是Runnable,Thread由于继承了Runnable所以也可以被接受
public Thread(Runnable target)
// 通过接收后,target最终被赋值
this.target = target;
// 调用run方法时,实际上执行的代理类target的方法
@Override
public void run() {
if (target != null) {
target.run();
}
}
Thread能描述线程,Runnable能更好的描述访问资源的.再代码架构上可以实现接口,Thread描述线程,Runnable专心描述我们对资源的操作。run方法是由虚拟机调用的。
多线程访问同一资源
通过Runnable实现线程
public static void main(String[] args) {
new Thread(()->{
System.out.println("Hello");
}).start();
}
其底层原理在Thread中,通过,代理模式调用
@Override
public void run() {
if (target != null) {
target.run();
}
}
而通过Thread继承的方式,则通过继承,覆盖的方式实现,完全覆盖了父类Thread的run方法
public class ThreadStyle extends Thread {
public ThreadStyle(String name){
super(name);
}
@Override
public void run() {
System.out.println("Thread");
}
public static void main(String[] args) {
new ThreadStyle("name").start();
}
}
由于覆盖了run方法,runnable不会生效了
public class ThreadStyle extends Thread {
public ThreadStyle(Runnable runnable){
super(runnable);
}
@Override
public void run() {
System.out.println("Thread");
}
public static void main(String[] args) {
new ThreadStyle(()->{
System.out.println("Runnable");
}).start();
}
}
/**output
* Thread
*/
创建线程只有一种方式那就是构造Thread类,而实现线程的执行单元,资源的操作由两种方式
线程对象的抽象描述Thread通过继承,我们能灵活封装一些操作,如将start放到构造函数中执行。
public class ThreadStyle extends Thread {
public ThreadStyle(Runnable runnable){
super(runnable);
// 将start放入构造函数
start();
}
public static void main(String[] args) {
new ThreadStyle(()->{
System.out.println("Runnable");
});
}
}
线程中需要注意线程的状态:
/* Java thread status for tools,
* initialized to indicate thread 'not yet started'
*/
private volatile int threadStatus = 0;
public synchronized void start() {
/**
* This method is not invoked for the main method thread or "system"
* group threads created/set up by the VM. Any new functionality added
* to this method in the future may have to also be added to the VM.
*
* A zero status value corresponds to state "NEW".
*/
if (threadStatus != 0)
throw new IllegalThreadStateException();
// ... ...
}