在多线程中,要调用start()来实现并发;
public synchronized void start() {
if (threadStatus != 0)//判断是否已经开始
throw new IllegalThreadStateException();//如若开始,重复调用则抛出异常
group.add(this);
boolean started = false;
try {
start0();//调用start0();
started = true;
} finally {
try {
if (!started) {
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
}
}
}
每个操作系统对资源调度的算法是不一样的,如果JVM要在windows绑定,windows就要有能进行JVM操作的算法
要想进行多线程的启动,一个程序是不行的,所以我们JVM准备了一个start0()的方法,要启动多线程,我们程序
通过start()找到start0();JVM根据不同的操作系统来实现start0();
如图:
THread(Runnable targe)
Callable类与Thread的关系;
main为主线程,一个程序为一个JVM进程,由主线程创建若干个子线程;
volatile 与synchronized区别:
- volatile主要用在属性上,而synchronized是在代码块与方法上使用的;
- volatile无法描述同步的处理,它是一种直接的内存操作,避免了副本的操作,sychronized是实现同步的;