多线程常用的操作方法

1.线程的命名与取得:
(1) 线程的命名:

  • 通过构造方法直接命名:
    public Thread (String name)
    public Thread(Runnable target, String name)
  • 通过Thread类提供的方法命名:
    public final synchronized void setName(String name)

(2) 线程名的取得:

  • 取得当前正在执行的线程:
    public static native Thread currentThread();
  • 取得线程名:
    public final String getName()

代码如下:

class MThread implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+" running...");
    }
}

public class ThreadMethod {
    public static void main(String[] args) {
        MThread mThread = new MThread();
        Thread thread = new Thread(mThread,"mythread_1");
        thread.start();
        Thread thread1 = new Thread(mThread);
        thread1.setName("mythread_2");
        thread1.start();
    }
}

结果如下:
在这里插入图片描述
2.线程的五大状态:

  • 新建状态(New):每当我们new Thread时,相当于创建了一个线程,此时线程处于新建状态,还没有开始运行。
  • 就绪状态(Runnable):当调用start()后,线程进入就绪状态,此时线程不一定执行了run(),因为要等待CPU的调度。
  • 运行状态(Running):当线程获得CPU时间后,则进入运行状态,此时真正执行run()。
  • 阻塞状态(Blocked):所谓阻塞态其实就是正在运行的线程并没有结束,而是暂时让出CPU,这样其它在就绪状态的线程就可以争取到CPU并进入运行状态。
  • 死亡状态(Dead):此时run()执行结束或是异常退出。

3.线程状态之间的转换:

(1) 线程休眠:指让线程暂缓执行,等到了预定时间再继续执行。
public static native void sleep(long millis) throws InterruptedException;

  • sleep()会立即交出CPU,让CPU去执行其他任务,但不会释放锁。
  • 此时线程从运行态变为阻塞态。

(2) 线程让步:暂停当前正在执行的线程,并执行其他线程。
public static native void yield();

  • yield()不会立即交出CPU,也不会释放对象锁。
  • yield()只会将CPU让给同等优先级或高优先级的线程。
  • 此时线程从运行状态变为就绪状态。

(3) join():等待线程终止
public final void join() throws InterruptedException

  • 若一个线程调用join(),则其他线程必须等待该线程终止后才可以继续执行。
  • 此时其他线程会从运行状态变为阻塞状态,等到join()结束后回到就绪状态。

(4)线程停止:

  • 设置标记位,使线程正常退出。
  • 调用stop()使线程强制退出,但是该方法不安全,有可能会产生不完整的数据。
  • 使用interrupt()可以中断线程。
    示例代码如下:
class TestThread implements Runnable{
    boolean flag = true;
    @Override
    public void run() {
        while (flag) {
            try {
                Thread.sleep(1000);
                boolean bool = Thread.currentThread().isInterrupted();
                if (bool) {
                    System.out.println("非阻塞退出!");
                    break;
                }
            } catch (InterruptedException e) {
                System.out.println("捕获到异常,阻塞退出!");
                boolean bool = Thread.currentThread().isInterrupted();
                System.out.println(bool);
                return;
            }
        }
    }
}

public class InterruptTest {
    public static void main(String[] args) throws InterruptedException {
        TestThread testThread = new TestThread();
        Thread thread = new Thread(testThread,"myThread");
        thread.start();
        Thread.sleep(2000);
        thread.interrupt();
        System.out.println(Thread.currentThread().getName()+"线程结束!");
    }
}

结果如下:
在这里插入图片描述
可见,interrupt()起到的作用其实只是设置中断标志位,当调用该方法,则将线程的中断标志位置为true,根据线程状态来判断是否抛出异常,最后确定如何退出线程。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值