java resume过时方法_Java并发编程笔记-Thread类常用方法详解

话不多说,先上图,先看看Thread类中的public方法。

93656d1e257086fb9a04bbb993225fd7.png

Thread类所有public方法

打扰了。。。

构造器

常用的构造器有Thread()、Thread(String name)、Thread(Runnable target),看构造器不难发现,Thread类实例化都是调用了init方法,看看init方法的实现,叭叭叭一大段讲了从SecurityManager拿到线程组以及权限校验, Thread类基本属性主要是根据当前正在执行的线程来赋值的。

private void init(ThreadGroup g, Runnable target, String name, long stackSize, AccessControlContext acc) { if (name == null) { throw new NullPointerException("name cannot be null"); } this.name = name.toCharArray(); 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; 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); if (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 */ tid = nextThreadID();}

start方法和run方法:

Thread类是Java语言对操作系统线程概念的抽象,new Thread()只是创建了一个实例对象,其他线程调用了实例的start方法(start方法中调用了一个native方法start0),新建的线程进入可运行线程池中,等待被线程调度选中,获取cpu 的使用权,执行业务逻辑,start方法不能重复调用;run方法是我们业务逻辑实现的地方,本质上就是类里面的一个成员方法,只是线程执行的时候会调用它,完全可以重复调用。

suspend|resume|stop方法

stop 方法: 该方法终止所有未结束的方法,包括 run方法。当线程被终止,立即释放被它锁住的所有对象的锁,这会导致对象处于不一致的状态。例如银行转账, 假设一个账户向另一个账户转账的过程中被终止,钱款已经转出,却没有转入目标账户,这就破坏了数据一致性。当线程要终止另一个线程时, 大多数情况我们无法知道什么时候调用 stop 方法是安全的。因此,该方法被弃用了。

suspend|resume 方法:suspend挂起线程,resume恢复挂起的线程。问题在于如果suspend挂起一个持有锁的线程, 那么,该锁在恢复之前是不可用的。如果调用 suspend方法的线程试图获得同一个锁, 那么程序死锁: 被suspend挂起的线程等着被恢复,而将其挂起的线程等待获得锁。因此,该方法被弃用了。

interrupt|interrupted|isInterrupted

interrupt:interrupt方法用来请求终止线程,但不会强制终止。每一个线程都具有一个boolean的中断状态。当对一个线程调用 interrupt 方法时,实际上就是去修改了线程的中断状态。当在一个被阻塞的线程(调用sleep或wait或存在不能被中断的阻塞 I/O 调用) 上调用 interrupt 方法时,阻塞调用将会被 Interrupted Exception 异常中断。

interrupted:返回线程中断状态,同时线程的中断状态重置为false。

isInterrupted():返回线程中断状态。常用于线程中逻辑判断依据,例如:

while (!Thread.currentThread().islnterrupted()) {  //业务逻辑}

join

join:把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行。比如在线程B中调用了线程A的Join()方法,直到线程A执行完毕后,才会继续执行线程B。

yield

yield:让出CPU时间,但不会释放锁。如果有其他的可运行线程具有至少与此线程同样高的优先级,那么这些线程接下来会被调度。

setDaemon

setDaemon:将线程转换为守护线程。守护线程的唯一用途 是为其他线程提供服务。当只剩下守护线程时, 虚拟机就退出了,所以守护线程不应该去访问固有资源, 如文件、 数据库,因为它会在任何时候甚至在一个操作的中间发生中断。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值