线程中的Thread类中的方法

/**
* 线程中常用的方法
* 1.start():启动线程,运行run()方法
* 2.run():是Thread类重写的方法,将线程需要执行的操作写在此方法中、
* 3.currentThread():返回当前线程
* 4.setName():设置线程名
* 5.getName():获取线程名
* 6.yield():线程礼让
* 7.join():线程抢占
* 8.stop():强制结束线程
* 9.sleep(时间):睡眠
* 参数
* MAX_PRIORITY:10
* MIN_PRIORITY:1
* NORM__PRIORITY:5 默认
*
* 设置与获取线程的优先级
* setPriority(XXX_PRIORITY)
* getPriority()
* @Author: ITYW
* @Create 2020 - 10 - 25 - 22:00
*/
public class ThreadMethod {
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.setName("MyThread");
Thread.currentThread().setName("main");
/* myThread.setPriority(Thread.MAX_PRIORITY);
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);*/
myThread.start();

for (int i = 0; i <= 10000; i++) {
/* try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
System.out.println(Thread.currentThread().getName()+"main"+i);
/*if (i == 10){
try {
myThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}*/
}
}
}
class MyThread extends Thread{

@Override
public void run() {
for (int i = 0; i <= 1000; i++) {
/* try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
System.out.println(Thread.currentThread().getName()+"MyThread"+i);
if (i %10==0){
yield();
}
}
}
}
原文地址:https://www.cnblogs.com/ITYW/p/13875928.html