博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java--Thread类常用方法介绍
阅读量:2443 次
发布时间:2019-05-10

本文共 3036 字,大约阅读时间需要 10 分钟。

Thread类常用方法

Thread对象调用:

序号 方法 方法描述
1 pubic void start() 使该线程开始执行;Java虚拟机调用该线程的run方法
2 public void run() 如果该线程是使用独立的 Runnable 运行对象构造的,则调用该 Runnable 对象的 run 方法;否则,该方法不执行任何操作并返回。
3 public final void setName(String name) 改变线程名称,使之与参数 name 相同。
4 public final void setPriority(int priority) 更改线程的优先级
5 public final void setDaemon(boolean on) 将该线程标记为守护线程或用户线程。
6 public final void join(long millisec) 调用线程等待被调用线程完成后,才能继续用下运行。参数为最多等待多少毫秒
7 public void interrupt() 中断线程
8 public final boolean isAlive() 测试线程是否处于活动状态。

Thread类调用:

序号 方法 方法描述
1 public static void yield() 暂停当前正在执行的线程对象,并执行其他线程。
2 public static boolean holdsLock(Object x) 当且仅当当前线程在指定的对象上保持监视器锁时,才返回 true。
3 public static void sleep(long millisec) 在指定的毫秒数内让当前正在执行的线程休眠(暂停执行),此操作受到系统计时器和调度程序精度和准确性的影响。
4 public staticThread currentThread() 返回对当前正在执行的线程对象的引用。
5 public static void dumpStack() 将当前线程的堆栈跟踪打印至标准错误流。

示例:

package pers.zhang.thread;/** * @author zhang * @date 2020/1/15 - 0:36 */public class GuessANumber extends Thread {
private int number; public GuessANumber(int number) {
this.number = number; } public void run() {
int counter = 0; int guess = 0; do {
guess = (int) (Math.random() * 100 + 1); System.out.println(this.getName() + " guesses " + guess); counter++; } while(guess != number); System.out.println("** Correct!" + this.getName() + "in" + counter + "guesses.**"); }}
package pers.zhang.thread;/** * @author zhang * @date 2020/1/15 - 0:35 */public class DisplayMessage implements Runnable {
private String message; public DisplayMessage(String message) {
this.message = message; } @Override public void run() {
while(true) {
System.out.println(message); } }}
package pers.zhang.thread;/** * @author zhang * @date 2020/1/15 - 0:37 */public class ThreadClassDemo {
public static void main(String [] args) {
Runnable hello = new DisplayMessage("Hello"); Thread thread1 = new Thread(hello); thread1.setDaemon(true); thread1.setName("hello"); System.out.println("Starting hello thread..."); thread1.start(); Runnable bye = new DisplayMessage("Goodbye"); Thread thread2 = new Thread(bye); thread2.setPriority(Thread.MIN_PRIORITY); thread2.setDaemon(true); System.out.println("Starting goodbye thread..."); thread2.start(); System.out.println("Starting thread3..."); Thread thread3 = new GuessANumber(27); thread3.start(); try {
thread3.join(); }catch(InterruptedException e) {
System.out.println("Thread interrupted."); } System.out.println("Starting thread4..."); Thread thread4 = new GuessANumber(75); thread4.start(); System.out.println("main() is ending..."); }}

输出每次都不同:

Starting hello thread...Starting goodbye thread...HelloHelloHelloHelloHelloHelloGoodbyeGoodbyeGoodbyeGoodbyeGoodbye.......

转载地址:http://wypqb.baihongyu.com/

你可能感兴趣的文章
app engine 入门_Google App Engine和PHP:入门
查看>>
限流 php接口限流 代码_有效地使用PHP流
查看>>
使用Pspell查找和纠正拼写错误的单词
查看>>
PHP依赖注入容器性能基准
查看>>
livereload_LiveReload
查看>>
如何在Windows上安装Ghost
查看>>
phpstorm -xmx_PhpStorm 8-新功能
查看>>
Chrome 27的新功能
查看>>
浏览器趋势(2013年5月):IE8降至10%以下
查看>>
谁偷了我的CPU?
查看>>
Microsoft将IE10更新推送到Windows 7
查看>>
liferay_云中的Liferay
查看>>
SQL或NoSQL:Google App Engine-第1部分
查看>>
SitePoint Podcast#178:Web设计过程和创造力
查看>>
移动端获取视频第一帧移动端_后端即服务-第1部分
查看>>
畅谈理想未来为主题的铅笔画_与专家畅谈Node.js
查看>>
SitePoint Podcast#173:释放混乱的猴子
查看>>
php 查询成绩_与专家讨论PHP: 成绩单
查看>>
一年新的一年_一年的云创新
查看>>
使用PHP从Access数据库中提取对象,第2部分
查看>>