java 线程 Thread 类介绍

一、概念

java 中用Thread 类用来描述线程,包含一些线程的基本信息,构造方法和 run 方法。

Thread implements Runnable {
    private volatile String name;
    private int            priority; // 线程优先级,数字越大越先被执行,可以设置,但是操作系统并不一定遵照设置的优先级
    private Thread         threadQ; // 线程对象的队列
    private long           eetop;
    /* Whether or not to single_step this thread. 单步线程*/
    private boolean     single_step;

    /* Whether or not the thread is a daemon thread. 默认非守护线程 */
    private boolean     daemon = false;

    /* JVM state */
    private boolean     stillborn = false;

    /* What will be run. */
    private Runnable target;

    /* The group of this thread */
    private ThreadGroup group;

    /* The context ClassLoader for this thread */
    private ClassLoader contextClassLoader;
    /* Thread ID */
    private long tid;

    /* For generating thread ID */
    private static long threadSeqNumber;

    /* Java thread status for tools,
     * initialized to indicate thread 'not yet started'
     */

    private volatile int threadStatus = 0;

二、线程的状态

A thread state. A thread can be in one of the following states:
/* 线程未启动时的状态 */
NEW A thread that has not yet started is in this state.
/* 线程可执行的状态-- 正在执行或者启动后准备执行 */
RUNNABLE A thread executing in the Java virtual machine is in this state.
/* 被阻塞的状态 */
BLOCKED A thread that is blocked waiting for a monitor lock is in this state.
/* 无期限等待其他线程的状态 */
WAITING A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
/* 定时等待的状态 */
TIMED_WAITING A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
/* 线程退出的状态 */
TERMINATED A thread that has exited is in this state.
/* 注意:这些状态是虚拟机状态,不反映任何操作系统线程的实际状态 */
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 Also:
getState

三、线程的创建方式

  1. 继承 Thread 重写 run 方法;
  2. 实现 Runnable 接口,重写run 方法;
  3. 实现 Callable 接口 + Future 创建有返回值的线程。
  4. 线程池创建线程

本质上都是创建 Thread 对象,重写 run 方法的形式。

// 方式一 和 方式二在 Thread 类源码中有具体的例子
// 方式三:实现 Callable 接口 +  Future 对象
// FutureTask 类实现了 Runnable 接口,可以构造 Thread 对象 
FutureTask<Integer> futureTask = new FutureTask<Integer>(
                (Callable<Integer>)()-> {
                    log.info(Thread.currentThread().getName() + " go ,,,");
                    log.info(Thread.currentThread().getName() + " stop ,,,");
                    return 3;
                }
        );
        Thread thread3 = new Thread(futureTask,"测试线程3");
        // 设置线程的优先级,数字越大优先级越高,越先执行,建议系统,非强制性的,一般线程的优先级都是 5
        thread3.setPriority(10);
        // 当前线程休眠,注意:这里是在main线程的任务中,所以是main线程休眠(thread3 的任务在 run方法中)
        Thread.sleep(2000);
        // TimeUnit.SECONDS.sleep(2); // 常用的当前线程休眠方式,可以指定时间单位
        log.info("state " + thread3.getState());
        
        thread3.start();
        log.info("state " + thread3.getState());

        // get() 方法时阻塞的方法,线程任务执行完之后才会有结果返回
        log.info("return " + futureTask.get());

// 方式四:线程池创建线程
ExecutorService executorService = Executors.newFixedThreadPool(5);
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                log.info(Thread.currentThread().getName() + " 启动 ,,,");
                log.info(Thread.currentThread().getName() + " 结束 ,,,");
            }
        });
        executorService.shutdown();

四、常用的几个方法

  • // 设置线程的优先级,数字越大优先级越高,越先执行,建议系统,非强制性的,一般线程的优先级都是 5
  • setPriority(10);
  • // 当前线程休眠,注意:这里是在main线程的任务中,所以是main线程休眠(thread3 的任务在 run方法中)
  • Thread.sleep(2000);
  • // TimeUnit.SECONDS.sleep(2); // 常用的当前线程休眠方式,可以指定时间单位
  • 获取线程状态,状态的描述上面已经讲过了
  • getState()
  • // Future 对象获取有返回值的线程时, get() 方法时阻塞的方法,线程任务执行完之后才会有结果返回
  • get();

你可能感兴趣的:(#,线程,java,线程的创建,线程的状态)