线程的定义
线程(轻量级进程)是指一个程序执行一次的过程,存在于进程中,是实现某一功能的指令序列。
线程的创建
1:通过创建Thread的子类,重写run()方法来创建线程
2:通过实现Runnable接口,实现run()抽象方法来实现
第一种方式
public class CounterThread extends Thread {
public CounterThread() {
}
public void run() {
try {
for (int i=1; i<=10; i++) {
System.out.println(this.getName()+"运行第"+i+"次");
Thread.sleep(1000); //让程序睡眠1秒钟,1秒钟后恢复
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class Test {
public static void main(String[] args) {
CounterThread counterThread = new CounterThread();
counterThread.start(); //通过调用state()方法启动线程
}
}
第二种方式
创建一个每个一秒输出当前时间的线程:
public class TimeThread implements Runnable{
String threadName = "时间线程";
@Override
public void run() {
while(true){
SimpleDateFormat time= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = time.format(new Date());
System.out.println(threadName+",当前时间:"+currentTime);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Test {
public static void main(String[] args) {
TimeThread thread = new TimeThread();
Thread timeThread = new Thread(thread);
timeThread.start();; //通过调用state()方法启动线程
}
}
线程的生命周期
1:以上两种方法中,通过(每)创建对象(每)新建一个线程,调用的start()方法使线程进入就绪状态,就绪后的线程获得CPU的使用权,进入运行状态,
运行的线程执行sleep()或join()方法,或者发出了I/O请求时,JVM会把该线程置为阻塞状态,当上述方法执行完成后,线程又将进入就绪状态。
线程常用方法
join():执行该方法的线程进入阻塞,调用该方法的线程结束后,线程进入就绪状态。
定义一个输出时间的线程
class TimeThread extends Thread{
@Override
public void run() {
for(int i=0;i<=2; i++){
System.out.println("时间线程:"+new Date());
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
定义一个计数的线程,并在该线程的run()方法里让时间线程使用join()方法,
class CounterThread extends Thread {
private TimeThread timeThread;
public CounterThread(TimeThread timeThread){
this.timeThread = timeThread;
}
@Override
public void run() {
for(int i=1;i<=3; i++){
if(i==2){
try {
timeThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("计数器线程:"+i);
}
}
}
public class Program {
public static void main(String[] args) {
TimeThread timeThread = new TimeThread();
timeThread.start();
new CounterThread(timeThread).start();
}
}
结果如下:
计数器线程:1
时间线程:Fri May 03 16:55:52 CST 2019
时间线程:Fri May 03 16:56:02 CST 2019
时间线程:Fri May 03 16:56:12 CST 2019
计数器线程:2
计数器线程:3
interrupt()方法
结束 wait()或join()方法使线程进入的阻塞状态,并产生InterruptedException异常
public class Program {
public static void main(String[] args) {
TimeThread timeThread = new TimeThread();
timeThread.start();
CounterThread counterThread = new CounterThread(timeThread);
counterThread.start();
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
e.printStackTrace();
}
counterThread.interrupt(); //调用该方法后线程将提前结束阻塞状态,运行计数器线程
}
}
currentThread方法:返回当前正在执行的线程对象。
isAlive方法:判定该线程是否处于就绪、运行或阻塞状态,如果是则返回true,否则返回false。
setDaemon方法:用于将一个尚未调用线程start方法的线程设置为守护线程。什么是守护线程:守护线程主要为其他线程提供服务,如你想让程序在运行时一直监视C盘的剩余空间,就可以将一个线程设置为守护线程,当C盘空间不足1GB时发出报警。守护线程会随着最后一个非守护线程的终止而终止。
getPriority()方法:获得当前线程的优先级。
setPriority(int newPriority)方法:设置当前线程的优先级,线程优先级越高,线程获得执行的次数越多,Java线程的优先级用整数表示,取值范围是1~10
yield()方法:静态方法,当前线程放弃占用CPU资源,回到就绪状态