Java线程基础

来源:互联网 发布:js 仿京东楼层特效 编辑:程序博客网 时间:2024/05/29 12:31

线程 是程序中的执行线程。Java 虚拟机允许应用程序并发地运行多个执行线程。

   每个线程都有一个优先级,高优先级线程的执行优先于低优先级线程。每个线程都可以或不可以标记为一个守护程序。当某个线程中运行的代码创建一个新 Thread 对象时,该新线程的初始优先级被设定为创建线程的优先级,并且当且仅当创建线程是守护线程时,新线程才是守护程序。

线程的ID是唯一标识getId()

线程的名称:getName(),如果不设置线程名称默认为“Thread-xx”

线程的优先级:线程最小优先级为1,最大为10,默认为5。

public final static int MIN_PRIORITY = 1;    public final static int NORM_PRIORITY = 5;    public final static int MAX_PRIORITY = 10;
可以通过set方法来设置线程的优先级

public final void setPriority(int newPriority) {        ThreadGroup g;        checkAccess();        if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {            throw new IllegalArgumentException();        }        if((g = getThreadGroup()) != null) {            if (newPriority > g.getMaxPriority()) {                newPriority = g.getMaxPriority();            }            setPriority0(priority = newPriority);        }    }

   首先java中线程分守护线程和非守护线程(用户线程),这两种线程的区别可以看这篇博文,其次线程Thread类中有两个嵌套类,一个是枚举类State,还有一个是接口定义了线程的五种状态还有一个是接口UncaughtExceptionHandler。Thread.State当 Thread 因未捕获的异常而突然终止时,调用处理程序的接口。

线程的六种状态:

    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;    }
1、新建状态(New):创建一个线程时,例如new Thread(r); 线程还没有开始运行,此时线程处于新建状态。
2、就绪状态(Runnable):创建了线程,还需要调用线程的start()方法,线程才会启动,start方法创建线程运行的系统资源并调用run方法,当start方法返回之后线程就处于就绪状态了。处于就绪状态的线程并不一定立即运行run()方法,线程还必须同其他线程竞争CPU时间,只有获得CPU时间才可以运行线程。因为在单CPU的计算机系统中,不可能同时运行多个线程,一个时刻仅有一个线程处于运行状态。因此此时可能有多个线程处于就绪状态。对多个处于就绪状态的线程是由Java运行时系统的线程调度程序(thread scheduler)来调度的。
3、运行状态(Running):当线程获得CPU时间后,它才进入运行状态,真正开始执行run()方法。
运行状态的情况比较复杂
第一:线程如果执行run() main()方法结束后,完成逻辑,线程就进入Terminated


第二:当线程调用sleep()或者join()方法就会进入Blocked状态,但是要注意的是阻塞的线程是不释放当前所占有的系统资源,当sleep()结束或者join()等待其他线程来到,当前线程则进入Runnable状态等待JVM分配资源。


第三:当线程进入Runnable状态,但是还没有开始运行的时候,此时发现需要的资源处于同步状态synchronized,这个时候线程将会进入Time waiting,JVM会使用队列对这些线程进行控制,既先进行Time waiting的线程会先得到JVM资源进行执行进入Waiting


第四:如果处于Runnable的线程调用yield()让出JVM资源,那么就会进入New状态和其他New状态线程进行竞争重新进入Runnable


第五:如果当前线程调用wait()方法,则当前线程进入Time waiting但是这个时候当前线程会释放所占有的JVM资源,进入这个状态过后是不能自动唤醒的,必须调用notify()或者notifyAll()方法,线程进入Waiting。

 
原创粉丝点击