Java多线程二:Thread类介绍

类介绍

首先看一下JavaDoc中对该类的介绍:

A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.

文档中说明:
1. Thread类表示程序中的一个可执行线程,java虚拟机允许程序中存在多个线程同时运行
2. 每个线程都存在优先级,优先级高的线程比优先级低的线程更有几率得到线程。其实然并卵
3. 线程可以声明为后台线程,通过后台线程声明和创建的线程也是后台线程
4. 通过某个线程创建的线程拥有当前线程相同的优先级

构造函数

//常用构造函数
public Thread()
public Thread(Runnable target)
public Thread(String name)
public Thread(Runnable target,String name)
//不常用构造函数
public Thread(ThreadGroup group,Runnable target)
public Thread(ThreadGroup group,String name)
public Thread(ThreadGroup group,Runnable target,String name)
public Thread(ThreadGroup group,Runnable target, String name,long stackSize)

Thinking In Java中曾说明,可以把线程组看成是一次不成功的尝试,忽略它即可。我们站在巨人的肩膀上,那就无视线程组,所以后面四个构造函数直接忽视吧。
示例:

public class Demo01 {
    public static void main(String[] args) {
        /* 这里使用了空构造函数public Thread() */
        Thread t1 = new Thread();
        Thread t2 = new MyThread("线程名字");
        /* 使用构造函数public Thread(Runnable target) target是线程执行的任务 */
        Thread t3 = new Thread(new MyRunnable());
        /* public Thread(Runnable target,String name) name是线程名称 target是线程执行的任务 */
        Thread t4 = new Thread(new MyRunnable(), "123");
    }
}

class MyThread extends Thread {

    /**
     * 这里使用了构造函数public Thread(String name),name参数表示线程名称
     * 
     * @param name
     */
    public MyThread(String name) {
        super(name);
    }

    @Override
    public void run() {
        System.out.println("线程已启动");
    }
}

class MyRunnable implements Runnable {

    public void run() {
        System.out.println("线程已启动");
    }
}

常用函数

方法名返回值解释
getId()long返回当前线程的ID
getName()String返回当前线程的名称
getState()Thread.state返回当前线程的状态
interrupted()static boolean当线程是否被中断
isAlive()boolean检测当前线程是否存活
isDaemon()boolean检测当前线程是否为后台线程
isInterrupted()boolean当线程是否被中断
setName(String name)void设置线程名称
setPriority(int newPriority)void设置线程优先级
start()void启动线程
yield()static void参考
sleep(long millis)static void参考
sleep(long millis, int nanos)static void参考
join()void参考
join(long millis)void参考
interrupt()void参考
run()void线程要执行的主体方法

(如有遗漏,请自行补充)
下面通过几个例子演示Thread的几个常用方法,其他请自行研究

package com.yin.myproject.demo.concurrent.base.threadDemo;

public class Demo02 {
    public static void main(String[] args) {
        MyRunnable1 r = new MyRunnable1();
        Thread t = new Thread(r, "我是一个线程");
        /**
         * getState()方法
         * 返回类型: Thread.state 一个枚举 表示线程的状态
         * 具体可参考[线程的生命周期]()
         */
        System.out.println("线程刚刚声明:状态getState()-->"+t.getState());
        /**
         * void start()方法 
         * 启动线程
         */
        t.start();
        /**
         * isAlive()方法
         * 返回类型:boolean
         * 当线程已经启动并且线程死亡前,线程都处于存活状态
         */
        System.out.println("线程是否存活:isAlive()-->"+t.isAlive());
        /**
         * getId()方法
         * 返回类型 long
         * 返回当前线程的Id,Id时一个long类型的数字,当线程被创建时自动生成。
         * 并且该Id唯一不可变,直到线程死亡后才可被复用
         */
        System.out.println("线程的ID:getId()-->"+t.getId());
        /**
         * getName()方法
         * 返回当前线程的名称。java虚拟机默认生成的线程名称格式为:"Thread-x",x是一个可变整数
         */
        System.out.println("线程的名称:getName()-->"+t.getName());

    }
}

class MyRunnable1 implements Runnable {

    public void run() {
        System.out.println("线程已启动");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值