常用线程方法:
对象.setName("线程名称")——线程设置名称
对象.getName()——获取指定线程名称
对象.setPriority(Thread.MIN_PRIORITY)——设置线程优先级, 注意优先级指的不是优先级高的就是先运行,运行的顺序不同的操作系统有不同的算法计算
对象.getPriority()——获取指定线程的优先级
Thread.sleep(1000)——线程休眠1秒, 括号中单位ms(1s = 1000ms)
*Thread.currentThread().方法——对当前执行的线程进行操作
线程优先级:
yield:
线程礼让, 当多个线程都在执行时, 执行了yield()的线程可能会停下让其他线程先执行, 不一定礼让成功。举个例子:
这就解释了为什么会出现礼让不成功的现象。实际情况就是线程礼让成功,但是由于其他cpu是空闲状态于是去了其他的cpu继续执行
代码演示——yield:
public class ThreadMethod {
public static void main(String[] args) throws InterruptedException {
TestMethod testMethod = new TestMethod();
testMethod.start();
for (int i = 0; i < 5; i++) {
Thread.sleep(500);
System.out.println("小猫喵~" + i);
}
}
}
class TestMethod extends Thread {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
if (i == 5) {
Thread.yield();// 礼让, 先别的线程先行, 不一定成功
}
try {
System.out.println("小狗汪汪叫" + i);
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("小狗不叫了");
}
}
运行结果:
join:
线程插队, 当多个线程都在执行时, 执行了join()的线程会强制让其它线程停下, 自己执行完毕其他线程继续执行
代码演示——join:
public class ThreadMethod {
public static void main(String[] args) throws InterruptedException {
TestMethod testMethod = new TestMethod();
testMethod.start();
for (int i = 0; i < 5; i++) {
if (i == 2) {
testMethod.join();
}
Thread.sleep(500);
System.out.println("小猫喵~" + i);
}
}
}
class TestMethod extends Thread {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
System.out.println("小狗汪汪叫" + i);
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("小狗不叫了");
}
}
运行结果:
interrupt:
中断线程, 中断休眠, 注意不是结束线程
代码演示——interrupt:
public class ThreadMethodDemo {
public static void main(String[] args) throws InterruptedException {
TestMethod testMethod = new TestMethod();
testMethod.setPriority(Thread.MIN_PRIORITY);// 设置线程优先级
testMethod.setName("子线程");
testMethod.start();// 启动子线程
// 主线程打印 5次, 然后中断子线程的休眠
for (int i = 0; i < 5; i++) {
Thread.sleep(1000);
System.out.println("主线程" + i);
}
System.out.println(testMethod.getName() + "线程的优先级是:" + testMethod.getPriority());// 获取优先级
testMethod.interrupt();// 中断子线程, 中断它的休眠
}
}
class TestMethod extends Thread {
@Override
public void run() {
for (int j = 0; j < 2 ; j++) {
for (int i = 0; i < 5; i++) {
// Thread.currentThread().getName() 获取当前线程的名称
System.out.println(Thread.currentThread().getName() + "-----" + i);
}
try {
System.out.println(Thread.currentThread().getName() + "休眠中~");
Thread.sleep(20000);// 20s
} catch (InterruptedException e) {
// 当线程执行到一个 interrupt方法时, 就会 catch一个异常, 可以加入自己的业务代码
// InterruptedException 捕获到一个中断异常
System.out.println(Thread.currentThread().getName() + "被interrupt了");
}
}
}
}
运行结果:
setDaemon:
设置守护线程, 执行了setDaemon(true)的线程将被设置为守护线程, 在下面的代码示例中, 将子线程设置为守护线程, 相对的主线程就是用户线程, 当用户线程结束后, 守护线程将会自动结束
代码演示——setDaemon:
public class ThreadMethodDemo {
public static void main(String[] args) throws InterruptedException {
TestMethod testMethod = new TestMethod();
// 如果希望 main线程结束后, 子线程自动结束
// 只需要将子线程设置为守护线程即可
// 守护线程会在用户线程结束后自动结束
testMethod.setDaemon(true);
testMethod.start();
for (int i = 0; i < 5; i++) {
System.out.println("主线程-----" + i);
Thread.sleep(1000);
}
}
}
class TestMethod extends Thread {
private int count = 0;
@Override
public void run() {
// 无限循环
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("========" + ++count + "========");
}
}
}