JUC底层的详细学习

JUC底层的详细学习
1、什么是JUC
 
源码+官方文档
 
JUC是 java util concurrent
 
面试高频问JUC——!
 
java.util 是Java的一个工具包——
 
业务:普通的线程代码 Thread
 
Runnable: 没有返回值、效率相比于Callable 相对较低!
 
2、线程和进程
 
进程:一个程序,QQ.EXE Music.EXE;数据+代码+pcb
 
一个进程可以包含多个线程,至少包含一个线程!
 
Java默认有几个线程?2个线程! main线程、GC线程
 
线程:开了一个进程Typora,写字,等待几分钟会进行自动保存(线程负责的)
 
对于Java而言:Thread、Runable、Callable进行开启线程的,我们之前。
 
提问?JAVA真的可以开启线程吗? 开不了的!
 
    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();
 
Java是没有权限去开启线程、操作硬件的,这是一个native的一个本地方法,它调用的底层的C++代码。
 
并发、并行
 
并发: 多线程操作同一个资源。
 
CPU 只有一核,模拟出来多条线程,天下武功,唯快不破。那么我们就可以使用CPU快速交替,来模拟多线程。
 
并行: 多个人一起行走
 
CPU多核,多个线程可以同时执行。 我们可以使用线程池!
 
public class Test1 {
 
    public static void main(String[] args) {
 
        //获取cpu的核数
 
        System.out.println(Runtime.getRuntime()。availableProcessors());
 
    }
 
}
 
并发编程的本质:充分利用CPU的资源!
 
线程有几个状态?
 
线程的状态:6个状态
 
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:
 
         * <ul>
 
         *   <li>{@link Object#wait() Object.wait} with no timeout</li>
 
         *   <li>{@link #join() Thread.join} with no timeout</li>
 
         *   <li>{@link LockSupport#park() LockSupport.park}</li>
 
         * </ul>
 
         *
 
         * <p>A thread in the waiting state is waiting for another thread to
 
         * perform a particular action.
 
         *
 
         * For example, a thread that has called <tt>Object.wait()</tt>
 
         * on an object is waiting for another thread to call
 
         * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
 
         * that object. A thread that has called <tt>Thread.join()</tt>
 
         * 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:
 
         * <ul>
 
         *   <li>{@link #sleep Thread.sleep}</li>
 
         *   <li>{@link Object#wait(long) Object.wait} with timeout</li>
 
         *   <li>{@link #join(long) Thread.join} with timeout</li>
 
         *   <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
 
         *   <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
 
         * </ul>
 
         */
 
    //超时等待
 
        TIMED_WAITING,
 
        /**
 
         * Thread state for a terminated thread.
 
         * The thread has completed execution.
 
         */
 
    //终止
 
        TERMINATED;
 
    }

推荐阅读