1.Thread类中常用方法
Thread类常用方法 | 描述 |
---|---|
start() | 启动当前线程,调用当前线程的run()方法。 |
run() | 通常需要重写该方法,将线程要执行的操作写在此方法中。 |
currentThread() | 静态方法,返回执行当前代码的线程。 |
getName() | 获取当前线程的名字。 |
setName() | 设置当前线程的名字。 |
yield() | 释放当前CPU的执行权。 |
join() | 在线程a中调用线程b的join(),此时线程a就进入了阻塞状态,直到线程b完全执行完以后,线程a才结束阻塞状态。 |
stop() | 此方法已过时,用于强制结束当前线程。 |
sleep(long millitime) | 使当前线程阻塞millitime毫秒。1秒 = 1000毫秒 |
isAlive() | 判断当前线程是否存活。 |
Thread类常用方法
```java
public class Test {
private int priority; //线程优先级,提供getPriority()/set方法
public final static int MIN_PRIORITY = 1; //最低
public final static int NORM_PRIORITY = 5; //默认优先级
public final static int MAX_PRIORITY = 10; //最高
public synchronized void start() {
if (threadStatus != 0)
throw new IllegalThreadStateException();
group.add(this);
boolean started = false;
try {
start0();
started = true;
} finally {
try {
if (!started) {
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
}
}
}
@Override
public void run() {
if (target != null) {
target.run();
}
}
public static native Thread currentThread();
public final String getName() {
return name;
}
public final synchronized void setName(String name) {
checkAccess();
if (name == null) {
throw new NullPointerException("name cannot be null");
}
this.name = name;
if (threadStatus != 0) {
setNativeName(name);
}
}
public static native void yield();
public final void join() throws InterruptedException {
join(0);
}
@Deprecated
public final void stop() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
checkAccess();
if (this != Thread.currentThread()) {
security.checkPermission(SecurityConstants.STOP_THREAD_PERMISSION);
}
}
if (threadStatus != 0) {
resume(); // Wake up thread if it was suspended; no-op otherwise
}
stop0(new ThreadDeath());
}
public static native void sleep(long millis) throws InterruptedException;
public final native boolean isAlive();
}
</details>
## 2.用户线程和守护线程
用户线程:运行在前台,main线程。
守护线程:运行在后台,为前台线程提供服务,如垃圾回收。
参考:[Java用户线程和守护线程](https://www.cnblogs.com/myseries/p/12078413.html "Java用户线程和守护线程")
原文地址:http://www.cnblogs.com/zhishu/p/16784500.html
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请务用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性