JUC指的是:Java里的三个包
程序执行的一次过程,一个进程包含一个或多个线程。进程是资源分配的单位
可以指程序执行过程中,负责实现某个功能的单位。线程是CPU调度和执行的单位
同一时刻,多个线程交替执行。(一个CPU交替执行线程)
同一时刻,多个线程同时执行。(多个CPU同时执行多个线程)
查询cpu核数
- public class Test1 {
- public static void main(String[] args) {
- //查询cpu核数
- //CPU 密集型,IO密集型
- System.out.println(Runtime.getRuntime().availableProcessors());
- }
- }
并发编程的本质是充分利用cpu资源。
问题:Java真的可以开启线程吗
不可以。Java创建Thread类调用start方法,底层是把线程放到一个组里面,然后调用一个本地方法start0;方法底层是C++;Java无法操作硬件。
Thread部分源码
- 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();
-
- /* Notify the group that this thread is about to be started
- * so that it can be added to the group's list of threads
- * and the group's unstarted count can be decremented. */
- group.add(this);
-
- boolean started = false;
- try {
- start0();
- started = true;
- } finally {
- try {
- if (!started) {
- group.threadStartFailed(this);
- }
- } catch (Throwable ignore) {
- /* do nothing. If start0 threw a Throwable then
- it will be passed up the call stack */
- }
- }
- }
-
- //本地方法,底层的C++ java无法直接操作硬件
- private native void start0();
新生状态、运行状态、阻塞状态、等待状态(死等)、超时等待状态(过期不候)、停止状态
Thread.State的源码
- public enum State {
- /**
- * Thread state for a thread which has not yet started.
- */
- // 新生
- NEW,
- /**
- * Thread state for a runnable thread. A thread in the runnable
- * state is executing in the Java virtual machine but it may
- * be waiting for other resources from the operating system
- * such as processor.
- */
- // 运行
- RUNNABLE,
- /**
- * Thread state for a thread blocked waiting for a monitor lock.
- * A thread in the blocked state is waiting for a monitor lock
- * to enter a synchronized block/method or
- * reenter a synchronized block/method after calling
- * {@link Object#wait() Object.wait}.
- */
- // 阻塞
- BLOCKED,
- /**
- * Thread state for a waiting thread.
- * A thread is in the waiting state due to calling one of the
- * following methods:
- *
- *
- {@link Object#wait() Object.wait} with no timeout
- *
- {@link #join() Thread.join} with no timeout
- *
- {@link LockSupport#park() LockSupport.park}
- *
- *
- *
A thread in the waiting state is waiting for another thread to
- * perform a particular action.
- *
- * For example, a thread that has called Object.wait()
- * on an object is waiting for another thread to call
- * Object.notify() or Object.notifyAll() on
- * that object. A thread that has called Thread.join()
- * is waiting for a specified thread to terminate.
- */
- // 等待,死死的等待
- WAITING,
- /**
- * Thread state for a waiting thread with a specified waiting time.
- * A thread is in the timed waiting state due to calling one of
- * the following methods with a specified positive waiting time:
- *
- *
- {@link #sleep Thread.sleep}
- *
- {@link Object#wait(long) Object.wait} with timeout
- *
- {@link #join(long) Thread.join} with timeout
- *
- {@link LockSupport#parkNanos LockSupport.parkNanos}
- *
- {@link LockSupport#parkUntil LockSupport.parkUntil}
- *
- */
- // 超时等待
- TIMED_WAITING,
- /**
- * Thread state for a terminated thread.
- * The thread has completed execution.
- */
- // 终止
- TERMINATED;
- }