java里的 thread 源码

本文详细介绍了Java中Thread类的不同构造方法,包括无参数构造方法、带有Runnable对象的构造方法及带有名称的构造方法等。这些构造方法允许开发者创建新的线程,并指定线程所属的线程组以及线程启动后将执行的任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
     * Constructs a new {@code Thread} with no {@code Runnable} object and a
     * newly generated name. The new {@code Thread} will belong to the same
     * {@code ThreadGroup} as the {@code Thread} calling this constructor.
     *
     * @see java.lang.ThreadGroup
     * @see java.lang.Runnable
     */
    public Thread() {
        create(null, null, null, 0);
    }

    /**
     * Constructs a new {@code Thread} with a {@code Runnable} object and a
     * newly generated name. The new {@code Thread} will belong to the same
     * {@code ThreadGroup} as the {@code Thread} calling this constructor.
     *
     * @param runnable
     *            a {@code Runnable} whose method <code>run</code> will be
     *            executed by the new {@code Thread}
     *
     * @see java.lang.ThreadGroup
     * @see java.lang.Runnable
     */
    public Thread(Runnable runnable) {
        create(null, runnable, null, 0);
    }

    /**
     * Constructs a new {@code Thread} with a {@code Runnable} object and name
     * provided. The new {@code Thread} will belong to the same {@code
     * ThreadGroup} as the {@code Thread} calling this constructor.
     *
     * @param runnable
     *            a {@code Runnable} whose method <code>run</code> will be
     *            executed by the new {@code Thread}
     * @param threadName
     *            the name for the {@code Thread} being created
     *
     * @see java.lang.ThreadGroup
     * @see java.lang.Runnable
     */
    public Thread(Runnable runnable, String threadName) {
        if (threadName == null) {
            throw new NullPointerException();
        }

        create(null, runnable, threadName, 0);
    }

    /**
     * Constructs a new {@code Thread} with no {@code Runnable} object and the
     * name provided. The new {@code Thread} will belong to the same {@code
     * ThreadGroup} as the {@code Thread} calling this constructor.
     *
     * @param threadName
     *            the name for the {@code Thread} being created
     *
     * @see java.lang.ThreadGroup
     * @see java.lang.Runnable
     *
     */
    public Thread(String threadName) {
        if (threadName == null) {
            throw new NullPointerException();
        }

        create(null, null, threadName, 0);

    }







Java中的线程是通过Thread类来实现的,Thread类封装了所有线程相关的方法和属性。下面是Thread类的部分源码: ```java public class Thread implements Runnable { //线程状态 private volatile int threadStatus = 0; private static final int RUNNING = 1; private static final int SHUTDOWN = -1; private static final int STOP = -2; private static final int TIDYING = 2; private static final int TERMINATED = 3; //线程优先级 public final static int MIN_PRIORITY = 1; public final static int NORM_PRIORITY = 5; public final static int MAX_PRIORITY = 10; //线程组 private ThreadGroup group; private Runnable target; private String name; private long stackSize; private long eetop; //线程ID private long tid; //线程本地存储 ThreadLocal.ThreadLocalMap threadLocals = null; ThreadLocal.ThreadLocalMap inheritableThreadLocals = null; //线程中断标志 private volatile boolean interrupted = false; private static final int HIBERNATE = 0; private static final int WAITING = 1; private static final int TIMED_WAITING = 2; private static final int BLOCKED = 3; private static final int NEW = 0; //线程锁 private Object parkBlocker; //线程中断处理 private void handleInterrupt() { if (this != Thread.currentThread()) throw new RuntimeException("Only the original thread can be interrupted"); if (this.interrupted) { park(); } } //线程休眠 public static void sleep(long millis) throws InterruptedException { Thread.sleep(millis, 0); } //中断线程 public void interrupt() { if (this != Thread.currentThread()) checkAccess(); synchronized (this) { interrupted = true; notifyAll(); } } //线程运行方法 @Override public void run() { if (target != null) { target.run(); } } } ``` 在Thread类中,我们可以看到一些重要的属性和方法,比如: - threadStatus: 线程状态,用整数表示,包含RUNNING、SHUTDOWN、STOP、TIDYING、TERMINATED五种状态。 - group: 线程所属的线程组。 - target: 线程要执行的任务。 - name: 线程名称。 - stackSize: 线程堆栈大小。 - tid: 线程ID。 - threadLocals: 线程本地存储。 - interrupted: 线程中断标志。 - parkBlocker: 线程锁。 - sleep(): 线程休眠方法。 - interrupt(): 中断线程方法。 - run(): 线程运行方法。 通过这些属性和方法,我们可以使用Java中的线程实现多线程编程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值