Java并发编程的艺术-----Java并发编程基础(启动和终止线程)

一、构造线程

在运行线程之前首先要构建一个线程,线程对象在构造的时候需要提供线程所需要的属性,如线程所属的线程组、线程优先级、是否是Daemon线程等信息。Thread中对线程进行初始化的部分代码如下:

private void init(ThreadGroup g, Runnable target, String name,
                      long stackSize, AccessControlContext acc,
                      boolean inheritThreadLocals) {
        if (name == null) {
            throw new NullPointerException("name cannot be null");
        }

        this.name = name;
         //当前线程就是该线程的父线程
        Thread parent = currentThread();
        SecurityManager security = System.getSecurityManager();
        if (g == null) {
            /* Determine if it's an applet or not */

            /* If there is a security manager, ask the security manager
               what to do. */
            if (security != null) {
                g = security.getThreadGroup();
            }

            /* If the security doesn't have a strong opinion of the matter
               use the parent thread group. */
            if (g == null) {
                g = parent.getThreadGroup();
            }
        }

        /* checkAccess regardless of whether or not threadgroup is
           explicitly passed in. */
        g.checkAccess();

        /*
         * Do we have the required permissions?
         */
        if (security != null) {
            if (isCCLOverridden(getClass())) {
                security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
            }
        }

        g.addUnstarted();

        this.group = g;
        //将daemon、priority 属性设置为父线程的对应属性
        this.daemon = parent.isDaemon();
        this.priority = parent.getPriority();
        if (security == null || isCCLOverridden(parent.getClass()))
            this.contextClassLoader = parent.getContextClassLoader();
        else
            this.contextClassLoader = parent.contextClassLoader;
        this.inheritedAccessControlContext =
                acc != null ? acc : AccessController.getContext();
        this.target = target;
        setPriority(priority);
        //将父线程的inheritableThreadLocal复制过来
        if (inheritThreadLocals && parent.inheritableThreadLocals != null)
            this.inheritableThreadLocals =
                ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
        /* Stash the specified stack size in case the VM cares */
        this.stackSize = stackSize;

        /* Set thread ID */
        //分配一个线程ID
        tid = nextThreadID();
    }

在上述过程中,一个构造的线程对象是由其parent线程来进行空间分配的,而child线程继承了parent是否为Daemon、优先级和资源的contextClassLoader 以及可以继承的ThreadLocal,同时还分配一个唯一的ID来标识这个child线程。至此,一个能够运行的线程对象就初始化好了,在堆内存中等待着运行。

二、启动线程

线程对象在初始化完成之后,调用start()方法就可以启动这个线程。线程start()方法的含义是:当前线程(即parent线程)同步告知Java虚拟机,只要线程规划器空闲,应立即启动调用start()方法的线程。

三、理解中断

中断可以理解为线程的一个标识位属性,它表示一个运行中的线程是否被其他线程进行了中断操作。其他线程通过调用该线程的interrupt()方法对其进行中断操作。
线程通过检查自身是否被中断来进行响应,线程通过方法isInterrupted()来进行判断是否被中断,也可以调用静态方法Thread.interrupted()对当前线程的中断标识位进行复位。如果该线程已经处于终结状态,即使该线程被中断过,在调用该线程对象的isInterrupted()时依旧返回false。
从JavaAPI中可以看出,许多声明抛出InterruptedException的方法在抛出InterruptedException之前,Java虚拟机会先将该线程的中断标识位清除,然后抛出InterruptedException,此时调用isInterrupted()将会返回false。示例代码如下:

package com.wholesmart.thread4;

import java.util.concurrent.TimeUnit;

public class Interrupted {
	public static void main(String[] args) throws Exception {
		// 不停尝试休眠
		Thread sleepThread = new Thread(new SleepRunner(), "SleepThread");
		sleepThread.setDaemon(true);
		// 不停运行
		Thread busyThread = new Thread(new BusyRunner(), "BusyThread");
		busyThread.setDaemon(true);
		sleepThread.start();
		busyThread.start();
		// 休眠5秒,让sleepThread和busyThread充分运行
		TimeUnit.SECONDS.sleep(5);
		sleepThread.interrupt();
		busyThread.interrupt();
		System.out.println("SleepThread interrupted is " + sleepThread.isInterrupted());
		System.out.println("BusyThread interrupted is " + busyThread.isInterrupted());
		// 防止sleepThread和busyThread立刻退出
		SleepUtils.second(2);
	}

	/**
	 * 一直尝试休眠的线程
	 * 
	 * @author dyw
	 * @date 2020年7月14日
	 */
	static class SleepRunner implements Runnable {

		@Override
		public void run() {
			while (true) {
				SleepUtils.second(10);
			}
		}
	}

	/**
	 * 忙碌的线程
	 * 
	 * @author dyw
	 * @date 2020年7月14日
	 */
	static class BusyRunner implements Runnable {

		@Override
		public void run() {
			while (true) {

			}
		}
	}
}

运行输出结果如下

SleepThread interrupted is false
BusyThread interrupted is true

从结果可以看出,抛出InterruptedException的线程SleepThread,其中断标识位被清除了,而一直运行的线程BusyThread,中断标识位没有被清除。

四、过期的suspend()、resume()和stop()

类比于CD机,如果把播放音乐作为一个线程的运行,那么对音乐的播放做出的暂停、恢复和停止操作对应线程Thread的API就是suspend()、resume()和stop()。示例代码如下:

package com.wholesmart.thread4;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class Deprecated {
	public static void main(String[] args) throws Exception {
		DateFormat format = new SimpleDateFormat("HH:mm:ss");
		Thread printThread = new Thread(new Runner(), "PrintThread");
		printThread.setDaemon(true);
		printThread.start();
		TimeUnit.SECONDS.sleep(3);
		//暂停输出
		printThread.suspend();
		System.out.println("main suspend PrintThread at " + format.format(new Date()));
		TimeUnit.SECONDS.sleep(3);
		//恢复输出
		printThread.resume();
		System.out.println("main resume PrintThread at " + format.format(new Date()));
		TimeUnit.SECONDS.sleep(3);
		//结束输出
		printThread.stop();
		System.out.println("main stop PrintThread at " + format.format(new Date()));
		TimeUnit.SECONDS.sleep(3);
	}

	static class Runner implements Runnable {

		@Override
		public void run() {
			DateFormat format = new SimpleDateFormat("HH:mm:ss");
			while (true) {
				System.out.println(Thread.currentThread().getName() + "Run at " + format.format(new Date()));
				SleepUtils.second(1);
			}
		}
	}
}

输出示例如下(可能会不同):

PrintThreadRun at 23:20:50
PrintThreadRun at 23:20:51
PrintThreadRun at 23:20:52
PrintThreadRun at 23:20:53
main suspend PrintThread at 23:20:53
PrintThreadRun at 23:20:56
main resume PrintThread at 23:20:56
PrintThreadRun at 23:20:57
PrintThreadRun at 23:20:58
main stop PrintThread at 23:20:59

在执行过程中PrintThread运行3秒,随后被暂停3秒,最后经过3秒被停止。但是这些API是过期的,不建议使用的原因如下:
过期的suspend()、resume()和stop()

五、安全地终止线程

中断状态是线程的一个标识位,而中断操作是一种简便的线程交方式,而这种交互方式最适合用来取消或是停止任务。除了中断以外,还可以利用一个boolean变量来控制是否需要停止任务并终止该线程。
下面的示例,创建一个线程CountThread,它不断地进行变量的累加,而主线程尝试对其进行中断操作和停止操作:

package com.wholesmart.thread4;

import java.util.concurrent.TimeUnit;

public class Shutdown {
	public static void main(String[] args) throws Exception {
		Runner one = new Runner();
		Thread countThread = new Thread(one, "CountThread");
		countThread.start();
		// 睡眠1秒,main线程对CountThread进行中断,使CountThread能够感知中断而结束
		TimeUnit.SECONDS.sleep(1);
		countThread.interrupt();
		Runner two = new Runner();
		countThread = new Thread(two, "CountThread");
		countThread.start();
		// 睡眠1秒,main线程对Runner two进行取消,使CountThread能够感知on为false而结束
		TimeUnit.SECONDS.sleep(1);
		two.cancel();
	}

	private static class Runner implements Runnable {
		private long i;
		private volatile boolean on = true;

		@Override
		public void run() {
			while (on && !Thread.currentThread().isInterrupted()) {
				i++;
			}
			System.out.println("Count i = " + i);
		}

		public void cancel() {
			on = false;
		}
	}
}

输出结果如下(会有不同):

Count i = 600573650
Count i = 527952155

这种通过标识位或者中断操作的方式能够使得线程在终止时有机会去清除资源,而不是武断的将线程停止,因此这种终止线程的做法显得更见安全和优雅。

附SleepUtils 源码:

package com.wholesmart.thread4;

import java.util.concurrent.TimeUnit;

public class SleepUtils {
	public static final void second(long senconds) {
		try {
			TimeUnit.SECONDS.sleep(senconds);
		} catch (InterruptedException e) {

		}
	}
}

最明目张胆的恭维,就是把“悍妇撒泼”说成是“贵妃醉酒”。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豢龙先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值