Java线程的一点理解

Posted 哈特谢普苏特

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java线程的一点理解相关的知识,希望对你有一定的参考价值。

1.线程和进程的区别

进程是资源进行分配和调度的基本单位 进程包含线程 进程有自己

线程cpu调度的最小单位

2.线程的生命周期

 一个线程被创建变成了new,在获取到除了cpu之外的资源后变成了runnable,在等待检测锁的时候是blocked(实际上sychronized字段才会导致线程这样),在被执行wait() sleep()以及join()等方法后变为waiting以及等待特定的时间时是timed_waiting,终结后变为terminated.

理解一下blocked:当一个线程被调用了wait()/join()之类的方法后,cpu显然没有,wait()连锁也没有,当被notify()之后,如果线程没有抢到monitor lock,就会变成blocked,如果抢到了就变成runnable状态,在runnable状态下如果拥有了cpu,就可以正常执行

 /**
     * A thread state.  A thread can be in one of the following states:
     * <ul>
     * <li>@link #NEW<br>
     *     A thread that has not yet started is in this state.
     *     </li>
     * <li>@link #RUNNABLE<br>
     *     A thread executing in the Java virtual machine is in this state.
     *     </li>
     * <li>@link #BLOCKED<br>
     *     A thread that is blocked waiting for a monitor lock
     *     is in this state.
     *     </li>
     * <li>@link #WAITING<br>
     *     A thread that is waiting indefinitely for another thread to
     *     perform a particular action is in this state.
     *     </li>
     * <li>@link #TIMED_WAITING<br>
     *     A thread that is waiting for another thread to perform an action
     *     for up to a specified waiting time is in this state.
     *     </li>
     * <li>@link #TERMINATED<br>
     *     A thread that has exited is in this state.
     *     </li>
     * </ul>
     *
     * <p>
     * A thread can be in only one state at a given point in time.
     * These states are virtual machine states which do not reflect
     * any operating system thread states.
     *
     * @since   1.5
     * @see #getState
     */
    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;
    

 3.线程的创建

在java里,线程创建实际上就是Thread thread = new Thread(),其它的事情都交给了java虚拟机,Thread的构造器实际上执行了init()函数,冰川乳当前的stackSize大小为0,但是在实际中,stackSize的大小是不为0的,可以跟踪一下init()函数

发现int()函数进行了重载 

 

 

4.线程的启动

3中说的 Thread thread = new Thread()仅仅是创建了一个新的线程,要想启动线程,需要执行线程的start()方法。

在start()方法中可以看到,一个线程只能被start一次,不能被多次start,否则会抛出异常。

可以看到threadStatus是被volatile修饰的 

在start()方法中,调用了start0()方法

发现start0()是个native方法,可以去查找一下这个native()方法到底做了什么,去查找Thread.cpp

路径在 安卓源码\\libcore\\ojluni\\src\\main\\native\\Thread.c

发现Thread.cpp中只有几句话,继续查找start0对应的JVM_StartThread,位于jvm.cpp文件中,路径

hotspot\\src\\share\\vm\\prims\\jvm.cpp

发现里面创建了一个JavaThread对象 去查看thread.cpp

 发现调用了os的zz()方法,并把堆栈大小传入(此时为0) 

 在create_thread()方法中,发现如果当前栈的大小为0,则将其设置为默认值default_stack_size(大小为8k),最终并调用pthread_create(&tid, &attr, (void* (*)(void*)) java_start, thread)真正创建线程。

5.待解决问题 

a.default_stack_size

b.threadStatus

以上是关于Java线程的一点理解的主要内容,如果未能解决你的问题,请参考以下文章

程序,进程,线程的最形象理解(理解不了可以直接怼我)

对synchronized的一点理解

day15:对threadlocal的一点理解

对事件循环的一点理解

java多线程,多线程加锁以及Condition类的使用

Java反射的一点简单理解