小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。
线程的常用方法
1. currentThread()方法
Thread.currentThread()方法可以获得当前线程
Java 中的任何一段代码都是执行在某个线程当中的. 执行当前代码的线程就是当前线程.同一段代码可能被不同的线程执行, 因此当前线程是相对的,Thread.currentThread()方法的返回值是在代码实际运行时候的线程对象
package com.wkcto.threadmehtod;
/**
* 定义线程类
* 分别在构造方法中和 run 方法中打印当前线程
* Author : 老崔
*/
public class SubThread1 extends Thread {
public SubThread1(){
System.out.println(" 构 造 方 法 打 印 当 前 线 程 的 名 称 : " +
Thread.currentThread().getName());
}
@Override
public void run() {
System.out.println("run 方 法 打 印 当 前 线 程 名 称 :" +
Thread.currentThread().getName());
}
}
package com.wkcto.threadmehtod;
/**
* 测试当前线程
* Author : 老崔
*/
public class Test01CurrentThread {
public static void main(String[] args) {
System.out.println("main 方 法 中 打 印 当 前 线 程 :" +
Thread.currentThread().getName());
//创建子线程, 调用 SubThread1()构造方法, 在 main 线程中调用构造方法,所以构造
方法中 的当前线程就是 main 线程
SubThread1 t1 = new SubThread1();
// t1.start(); //启动子线程,子线程会调用 run()方法,所以 run()方法中 的当
前线程就是 Thread-0 子线程
t1.run(); //在 main 方法中直接调用 run()方法,没有开启新的线程,所以在 run 方法
中的当前线程就是 main 线程
}
}
package com.wkcto.threadmehtod;
/**
* 当前线程的复杂案例
* Author : 老崔
*/
public class SubThread2 extends Thread {
public SubThread2(){
System.out.println(" 构 造 方 法 中 ,Thread.currentThread().getName() : " +
Thread.currentThread().getName() );
System.out.println("构造方法,this.getName() : " + this.getName());
}
@Override
public void run() {
System.out.println("run 方 法 中 ,Thread.currentThread().getName() : " +
Thread.currentThread().getName() );
System.out.println("run 方法,this.getName() : " + this.getName());
}
}
package com.wkcto.threadmehtod;
/**
* Author : 老崔
*/
public class Test02CurrentThread {
public static void main(String[] args) throws InterruptedException {
//创建子线程对象
SubThread2 t2 = new SubThread2();
t2.setName("t2"); //设置线程的名称
t2.start();
Thread.sleep(500); //main 线程睡眠 500 毫秒
//Thread(Runnable)构造方法形参是 Runnable 接口,调用时传递的实参是接口的实现
类对象
Thread t3 = new Thread(t2);
t3.start();
}
}
2. setName()/getName()
thread.setName(线程名称), 设置线程名称
thread.getName()返回线程名称
通过设置线程名称,有助于程序调试,提高程序的可读性, 建议为每个线程都设置一个能够体现线程功能的名称
3. isAlive()
thread.isAlive()判断当前线程是否处于活动状态
活动状态就是线程已启动并且尚未终止
package com.wkcto.threadmehtod.p2IsAlive;
/**
* Author : 老崔
*/
public class SubThread3 extends Thread {
@Override
public void run() {
System.out.println("run 方法, isalive = " + this.isAlive()); //运行状态,true
}
}
package com.wkcto.threadmehtod.p2IsAlive;
/**
* 测试线程的活动状态
* Author : 老崔
*/
public class Test {
public static void main(String[] args) {
SubThread3 t3 = new SubThread3();
System.out.println("begin==" + t3.isAlive()); //false,在启动线程之前
t3.start();
System.out.println("end==" + t3.isAlive()); //结果不一定,打印这一行时,如果 t3
线程还没结束就返回 true, 如果 t3 线程已结束,返回 false
}
}
4. sleep()
Thread.sleep(millis); 让当前线程休眠指定的毫秒数
当前线程是指 Thread.currentThread()返回的线程
package com.wkcto.threadmehtod.p3sleep;
/**
* 子线程休眠
* Author : 老崔
*/
public class SubThread4 extends Thread {
@Override
public void run() {
try {
System.out.println("run, threadname=" + Thread.currentThread().getName()
+ " ,begin= " + System.currentTimeMillis());
Thread.sleep(2000); //当前线程睡眠 2000 毫秒
System.out.println("run, threadname=" + Thread.currentThread().getName()
+ " ,end= " + System.currentTimeMillis());
} catch (InterruptedException e) {
//在子线程的 run 方法中, 如果有受检异常(编译时异常)需要处理,只有选择捕
获处理,不能抛出处理
e.printStackTrace();
}
}
}
package com.wkcto.threadmehtod.p3sleep;
/**
* Author : 老崔
*/
public class Test {
public static void main(String[] args) {
SubThread4 t4 = new SubThread4();
System.out.println("main__begin = " + System.currentTimeMillis());
// t4.start(); //开启新的线程
t4.run(); //在 main 线程中调用实例方法 run(),没有开启新的线程
System.out.println("main__end = " + System.currentTimeMillis());
}
}
package com.wkcto.threadmehtod.p3sleep;
/**
* 使用线程休眠 Thread.sleep 完成一个简易的计时器
* Author : 蛙课网老崔
*/
public class SimpleTimer {
public static void main(String[] args) {
int remaining = 60; //从 60 秒开始计时
//读取 main 方法的参数
if (args.length == 1){
remaining = Integer.parseInt(args[0]);
}
while(true){
System.out.println("Remaining: " + remaining);
remaining--;
if (remaining < 0 ){
break;
}
try {
Thread.sleep(1000); //线程休眠
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Done!!");
}
}