1. 线程的命名与获取
① 通过构造方法在创建线程时设置线程名字
Public Thread(String name)
Public Thread(Runnable target,String name)
② 取得线程名字
public final String getName()
③ 设置线程名字
public final synchronized void setName(String name)
eg:
public class threadTest extends Thread{
public static void main(String[] args) {
Thread thread = new Thread("构造方法设置名字");
System.out.println(thread.getName());
thread.setName("新名字");
System.out.println(thread.getName());
}
}
如果我们不起名字,线程也是有默认名字的。
public class threadTest extends Thread{
public static void main(String[] args) {
Thread thread = new Thread();
System.out.println(thread.getName());
Thread thread1 = new Thread();
System.out.println(thread.getName());
Thread thread2 = new Thread();
System.out.println(thread.getName());
thread.start();
thread1.start();
thread2.start();
可以看到,线程的默认名字就是Thread-0
在看别的方法之前,先来看一张线程状态图
当我们创建了一个线程后并调用了start()启动线程后,线程不会立刻被执行,而是进入就绪状态,等待系统资源调度的分配,分配完了后才能从就绪态变为运行态开始执行,若当前线程一直到执行完毕都没有被打断,它就会终止。若是线程在执行过程中被阻塞了,他就会进入阻塞状态,等待阻塞解除进入就绪状态,再次等资源调度分配好后进入运行状态继续执行。
了解了这些后,我们再来看下面的方法。
2. 线程休眠方法 ---- sleep()
线程休眠:让调用该方法的线程暂停执行,等到了预计时间后再开始执行
使当前线程从运行状态 --> 阻塞态
休眠时间结束,从阻塞态 --> 运行态
public static native void sleep(long millis) throws InterruptedException:
millis是线程休眠的时间,注意,单位是毫秒。
public static void sleep(long millis, int nanos):millis是休眠时间,单位是纳秒
eg:
import java.text.SimpleDateFormat;
import java.util.Date;
class myThread implements Runnable{
@Override
public void run() {
for(int i = 0; i < 5; i++){
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat
("yyyy-MM-dd HH:mm:ss");
System.out.println(simpleDateFormat.format(date)); //打印当前时间
try {
Thread.sleep(2000); // 让当前线程休眠2秒
} catch (InterruptedException e) { // 休眠方法会抛出异常,
e.printStackTrace(); // 捕获一下就好
}
}
}
}
public class threadTest extends Thread{
public static void main(String[] args) {
Thread thread = new Thread(new myThread());
thread.start();
}
}
3. 线程让步 ---- yield()
线程让步是暂停执行当前线程的对象,并让该对象去执行别的线程。
线程让步:从运行状态 --> 就绪状态
yield()方法被调用后会交出CPU资源,并且只会让拥有相同优先级的线程获取到CPU资源。
4. 线程等待 ---- join()
顾名思义,join()方法就是等待其他线程执行完了后再执行,是线程间通信的一种方法。(就是我知道你执行完了后,我才会去执行)
如果在主线程中调用join(),会让主线程休眠,等待调用该方法的线程执行完,主线程再恢复执行。
join:运行状态 --> 阻塞状态
eg:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
class myThread implements Runnable{
@Override
public void run() {
System.out.println("主线程休眠开始");
printTime();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("主线程休眠结束");
printTime();
}
public static void printTime(){
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = dateFormat.format(date);
System.out.println(s);
}
}
public class threadTest{
public static void main(String[] args) throws InterruptedException {
System.out.println("主线程开始");
Thread thread = new Thread(new myThread());
thread.start();
thread.join(); // 让主线程休眠,子线程执行完才能打印下面的主线程结束这句话
System.out.println("主线程结束");
}
}
主线程一定等子线程执行完了,才能恢复执行自己的最后一句打印语句
如果没有join()方法,有可能主线程已经执行完了,子线程才开始执行,就像下面的结果一样。
5. 线程停止
我们一般有三种方法,让线程停止下来:
① 设置一个标记位:只有当标记位为真时才会执行,若要让线程停止,我们把标记位改为假,不满足判断条件了,线程就会停止了
② stop():public final void stop()
但是这个方法由于线程不安全,已经被废弃了,因为stop是一种强制停止的方法,有可能线程正在进行重要操作,却被强制打断了
③ 调用Thread的interrupt()方法
interrupt()方法只是将线程状态置为中断状态而已,它不会中断一个正在运行的线程。此方法只是给线程传递一个中断信号,程序可以根据此信号来判断是否需要终止。相当于系统设置的标志位。
6. 线程优先级(1–10)
线程优先级是指优先级越高越有可能先执行,但仅仅是有可能而已。具体谁先执行还是由系统决定。
设置优先级
public final void setPriority(int newPriority)
取得优先级
public final int getPriority()
MAX_PRIORITY = 10; 优先级最高
NORM_PRIORITY = 5; 优先级中等
MIN_PRIORITY = 1; 优先级最低