}

2. Thread类

1、Runnable接口源码:

1 public interface Runnable {

2 public void run();

3 }

2、Thread类与Runnable接口的继承关系

1 public class Thread implements Runnable{

2

3 }

Runnable接口仅有一个run()方法,Thread类实现了Runnable接口。

3、构造函数

1 public Thread() {

2 init(null, null, “Thread-” + nextThreadNum(), 0);

3 }

1 public Thread(Runnable target) {

2 init(null, target, “Thread-” + nextThreadNum(), 0);

3 }

1 public Thread(ThreadGroup group, Runnable target) {

2 init(group, target, “Thread-” + nextThreadNum(), 0);

3 }

1 public Thread(String name) {

2 init(null, null, name, 0);

3 }

还有其它的构造方法,此处省略。。。

这里的第三个参数是设置线程的名称,从下面的代码中可以看出,生成名称的规则是:”Thread-”加上创建的线程的个数(第几个)。

继续查看init方法:

复制代码

1 /**

2 * Initializes a Thread.

3 *

4 * @param g the Thread group 5 * @param target the object whose run() method gets called 6 * @param name the name of the new Thread 7 * @param stackSize the desired stack size for the new thread, or 8 * zero to indicate that this parameter is to be ignored.

9 */

//ThreadGroup:线程组表示一个线程的集合。此外,线程组也可以包含其他线程组。线程组构成一棵树,在树中,除了初始线程组外,每个线程组都有一个父线程组。

10 private void init(ThreadGroup g, Runnable target, String name,

11 long stackSize) {

12 Thread parent = currentThread();

13 SecurityManager security = System.getSecurityManager();

14 if (g == null) {

15 /* Determine if it’s an applet or not */

16

17 /* If there is a security manager, ask the security manager

18 what to do. */

19 if (security != null) {

20 g = security.getThreadGroup();

21 }

22

23 /* If the security doesn’t have a strong opinion of the matter

24 use the parent thread group. */

25 if (g == null) {

26 g = parent.getThreadGroup();

27 }

28 }

29

30 /* checkAccess regardless of whether or not threadgroup is

31 explicitly passed in. */

32 g.checkAccess();

33

34 /*

35 * Do we have the required permissions?

36 */

37 if (security != null) {

38 if (isCCLOverridden(getClass())) {

39 security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);

40 }

41 }

42

43

44 g.addUnstarted();

45

46 this.group = g;

//每个线程都有一个优先级,高优先级线程的执行优先于低优先级线程。每个线程都可以或不可以标记为一个守护程序。当某个线程中运行的代码创建一个新 Thread 对象时,该新线程的初始优先级被设定为创建线程的优先级,并且当且仅当创建线程是守护线程时,新线程才是守护程序。

47 this.daemon = parent.isDaemon();

48 this.priority = parent.getPriority();

49 this.name = name.toCharArray();

50 if (security == null || isCCLOverridden(parent.getClass()))

51 this.contextClassLoader = parent.getContextClassLoader();

52 else

53 this.contextClassLoader = parent.contextClassLoader;

54 this.inheritedAccessControlContext = AccessController.getContext();

55 this.target = target;

56 setPriority(priority);

57 if (parent.inheritableThreadLocals != null)

58 this.inheritableThreadLocals =

59 ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);

60 /* Stash the specified stack size in case the VM cares */

61 this.stackSize = stackSize;

62

63 /* Set thread ID */

64 tid = nextThreadID();

65 }

复制代码

初始化时设置了是否为守护线程,优先级,初始化名称。

4、Thread的start方法的实现:

复制代码

1 public synchronized void start() { 2 /**

3 * This method is not invoked for the main method thread or “system”

4 * group threads created/set up by the VM. Any new functionality added

5 * to this method in the future may have to also be added to the VM.

6 *

7 * A zero status value corresponds to state “NEW”.

8 */

9 if (threadStatus != 0)

10 throw new IllegalThreadStateException();

11 group.add(this);

12 start0();

13 if (stopBeforeStart) {

14 stop0(throwableFromStop);

15 }

16 }

复制代码

这里主要的是start0方法;查看其实现:

1 private native void start0();

这里使用了本地调用,通过C代码初始化线程需要的系统资源。可见,线程底层的实现是通过C代码去完成的。

4、Thread的run方法的实现

1 public void run() {

2 if (target != null) {

3 target.run();

4 }

5 }

这里的target实际上要保存的是一个Runnable接口的实现的引用:

1 private Runnable target;

所以使用继承Thread创建线程类时,需要重写run方法,因为默认的run方法什么也不干。

而当我们使用Runnable接口实现线程类时,为了启动线程,需要先把该线程类实例初始化一个Thread,实际上就执行了如下构造函数:

1 public Thread(Runnable target) {

2 init(null, target, “Thread-” + nextThreadNum(), 0);

3 }

即是把线程类的引用保存到target中。这样,当调用Thread的run方法时,target就不为空了,而是继续调用了target的run方法,所以我们需要实现Runnable的run方法。这样通过Thread的run方法就调用到了Runnable实现类中的run方法。

这也是Runnable接口实现的线程类需要这样启动的原因。

最后

希望本文对你有所启发,有任何面试上的建议也欢迎留言分享给大家。

好了,今天的分享就到这里,如果你对在面试中遇到的问题,或者刚毕业及工作几年迷茫不知道该如何准备面试并突破现状提升自己,对于自己的未来还不够了解不知道给如何规划,可以加一下下面的技术群。来看看同行们都是如何突破现状,怎么学习的,来吸收他们的面试以及工作经验完善自己的之后的面试计划及职业规划。

这里放一下资料获取方式:GitHub

好了~如果你看到了这里,觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足。谢谢。

后的面试计划及职业规划。**

这里放一下资料获取方式:GitHub

[外链图片转存中…(img-ZNsARX9f-1644043864306)]

好了~如果你看到了这里,觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足。谢谢。

[外链图片转存中…(img-rlvApcen-1644043864307)]

为什么某些人会一直比你优秀,是因为他本身就很优秀还一直在持续努力变得更优秀,而你是不是还在满足于现状内心在窃喜!希望读到这的您能点个小赞和关注下我,以后还会更新技术干货,谢谢您的支持!

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐