命名
定义
public final String getName();//返回此线程的名称。
栗子
package com.test;
class MyThread implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " 线程运行 ...");
}
}
public class JavaDemo {
public static void main(String[] args) throws Exception {
MyThread threadBody = new MyThread() ;
new Thread(threadBody).start();//未命名的线程
new Thread(threadBody,"线程 - A").start();//已命名的线程
new Thread(threadBody).start();//未命名的线程
new Thread(threadBody,"线程 - B").start();//已命名的线程
}
}
运行结果:
此时,已命名的线程使用自定义的名称,而未命名的线程使用程序定义的不重复的名称。
休眠
定义
- 当前正在执行的线程休眠(暂时停止执行)指定的毫秒数
public static void sleep(long millis) throws InterruptedException;
- 当前正在执行的线程休眠(暂时停止执行)指定的毫秒数加上指定的纳秒数
public static void sleep(long millis, int nanos) throws InterruptedException;
栗子
package com.test;
class MyThread implements Runnable {
@Override
public void run() {
for (int x = 0 ; x < 5 ; x ++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "运行,x = " + x);
}
}
}
public class JavaDemo {
public static void main(String[] args) throws Exception {
MyThread body = new MyThread() ;
new Thread(body,"运行线程").start();
body.run() ;
}
}
中断
定义
- 测试此线程是否已被中断
public boolean isInterrupted();
- 中断此线程
public void interrup();
栗子
package com.test;
public class JavaDemo {
public static void main(String[] args) throws Exception {
Thread thread = new Thread(()->{
try {
Thread.sleep(20000);
System.out.println("吃饱睡足,开始学习");
} catch (InterruptedException e) {
System.out.println("休息被中断了,于是开始xuexi");
}
}) ;
thread.start();
System.out.println(thread.getName() + "线程的状态:" + thread.isInterrupted());
Thread.sleep(2000);
thread.interrupt(); // 由主线程中断了一个子线程
System.out.println(thread.getName() + "线程的状态:" + thread.isInterrupted());
}
}
运行结果:
强制运行
定义
public final void join() throws InterruptedException;
栗子
public class JavaDemo {
public static void main(String[] args) throws Exception {
Thread mainThread = Thread.currentThread() ; // 获得主线程对象
Thread thread = new Thread(()->{
for (int x = 0 ; x < 10 ; x ++) {
if (x == 2) { // 设置一个强制的触发条件
try {
mainThread.join(); // 主线程强制执行
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + "执行,x = " + x);
}
},"循环线程") ;
thread.start();
for (int x = 0 ; x < 5 ; x ++) {
Thread.sleep(100); // 延缓程序的执行速度
System.out.println(Thread.currentThread().getName() + "执行,x = " + x);
}
}
}
运行结果:
感觉就是等待“ mainThread ”线程死亡。
礼让
定义
public static void yield();
栗子
public class JavaDemo {
public static void main(String[] args) throws Exception {
Thread mainThread = Thread.currentThread() ; // 获得主线程对象
Thread thread = new Thread(()->{
for (int x = 0 ; x < 10 ; x ++) {
if (x % 2 == 0) {
Thread.yield(); // 礼让一次
System.out.println("线程礼让," + Thread.currentThread().getName());
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "执行,x = " + x);
}
},"循环线程") ;
thread.start();
for (int x = 0 ; x < 10 ; x ++) {
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + "执行,x = " + x);
}
}
}
运行结果:
优先级
定义
MAX_PRIORITY
最大优先级MIN_PRIORITY
最小优先级NORM_PRIORITY
默认优先级(中等)
栗子
package com.test;
public class JavaDemo {
public static void main(String[] args) throws Exception {
Runnable run = ()->{
for (int x = 0 ; x < 5 ; x ++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "运行,x = " + x);
}
} ;
Thread threadA = new Thread(run,"线程1") ;
Thread threadB = new Thread(run,"线程2") ;
Thread threadC = new Thread(run,"线程3") ;
threadA.setPriority(Thread.MIN_PRIORITY);
threadB.setPriority(Thread.MIN_PRIORITY);
threadC.setPriority(Thread.MAX_PRIORITY);
threadA.start();
threadB.start();
threadC.start();
}
}
运行结果: