Thread类的基本用法
1. 创建子类,继承自Thread.重写Thread类中的run()方法,在new的时候newThread的子类,在run()中写在县城中具体实现的代码,描述了这个线程内部要执行哪些代码.在代码中,并非定义了子类,一写run方法,线程就创建出来,线程的具体实现,需要调用start方法,线程才被创建出来。调用start之前,系统中并没有创建出线程。
class MyThread extends Thread{
@Override
public void run() {
System.out.println("hello thread");
}
}
public class Demo1 {
public static void main(String[] args) {
Thread t = new MyThread();
t.start();
}
}
如果在一个循环中不加任何限制,这个循环的速度非常非常快,在线程中可以加上一个sleep操作,来强制让这个线程休眠一段时间,这个休眠操作,就是强制让线程进入阻塞状态,单位是ms,相当于一秒之内这个线程不会到cpu上执行
一个进程中至少会有一个线程.在一个java进程中,也是至少会有一个调用main方法的线程(系统自生成),此时t线程和main线程,就是并发执行的关系(此处的并发=并行+并发),现在两个线程都是打印一条休眠一秒,当一秒时间到了之后.系统先唤醒哪个线程的顺序是随机的.对于操作系统来说,内部对于线程之间的调度顺序,在宏观上也可以认为是随机的~~~(抢占式执行)
class MyThread1 extends Thread{
@Override
public void run() {
while (true){
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Demo2 {
public static void main(String[] args) {
Thread t = new MyThread1();
t.start();
while (true){
System.out.println("hello world");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
2. 创建一个类,实现Runnable接口,再创建Runnable实例传给ThreadRunnable就是在描述一个"任务",通过Runnable来描述一个任务,进一步的再把描述好的任务交给Thread实例
class MyRunnable implements Runnable{
@Override
public void run() {
System.out.println("hello thread");
}
}
public class Demo3 {
public static void main(String[] args) {
Thread t = new Thread(new MyRunnable());
t.start();
}
}
3. 使用了匿名内部类,继承自Thread类,同时重写run方法,同时在new出这个匿名内部类的实例
public class Demo4 {
public static void main(String[] args) {
Thread t = new Thread(){//匿名内部类
@Override
public void run(){
System.out.println("hello thread");
}
}
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("hello thread!");
}
});
t.start();
t1.start();
}
}
4. 使用lambda表达式(使用lambda代替了Runnable)
public class Demo6 {
public static void main(String[] args) {
Thread t = new Thread(() ->{
System.out.println("hello thread");
});
t.start();
}
}
Thread中的一些重要方法
- start():启动线程,使其进入可运行状态,系统会自动调用线程的run()方法。
public class MyThread extends Thread {
public void run() {
System.out.println("Thread is running");
}
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}
}
- run():线程的执行代码都应该放在这个方法中。当线程被启动时,run()方法会被调用。
public class MyThread extends Thread {
public void run() {
System.out.println("Thread is running");
}
public static void main(String[] args){
MyThread thread = new MyThread();
thread.run();
}
}
- sleep(long millis):让当前线程休眠指定的时间,以毫秒为单位。休眠期间线程不会执行任何操作。
public class MyThread extends Thread {
public void run() {
try {
System.out.println("Thread is sleeping");
Thread.sleep(2000);
System.out.println("Thread has finished sleeping");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
MyThread thread = new MyThread();
thread.start();
}
}
- join():等待线程执行完毕,直到该线程结束后才继续执行当前线程。
public class MyThread extends Thread {
public void run() {
System.out.println("Thread is running");
}
public static void main(String[] args){
MyThread thread = new MyThread();
thread.start();
try {
thread.join();
System.out.println("Thread has finished execution");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
- interrupt():中断线程的执行。当线程处于阻塞状态(如睡眠、等待输入输出、等待获取锁等)时,通过调用线程的interrupt()方法可以使线程立即抛出InterruptedException异常。
public class MyThread extends Thread {
public void run() {
while (!Thread.currentThread().isInterrupted()) {
System.out.println("Thread is running");
}
System.out.println("Thread has been interrupted");
}
public static void main(String[] args){
MyThread thread = new MyThread();
thread.start();
ry {
Thread.sleep(1000);
thread.interrupt();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
- isAlive():判断线程是否存活,即线程是否启动且尚未终止。
public class MyThread extends Thread {
public void run() {
System.out.println("Thread is running");
}
public static void main(String[] args){
MyThread thread = new MyThread();
System.out.println("Before starting, isAlive: " + thread.isAlive());
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("After execution, isAlive: " + thread.isAlive());
}
}
- getName():获取线程的名称。
- setName(String name):设置线程的名称。
public class MyThread extends Thread {
public void run() {
System.out.println("Thread name: " + getName());
}
public static void main(String[] args){
MyThread thread = new MyThread();
thread.setName("MyThread");
thread.start();
}
}
- getPriority():获取线程的优先级。
- setPriority(int priority):设置线程的优先级,数值越大表示优先级越高。
public class MyThread extends Thread {
public void run() {
System.out.println("Thread priority: " + getPriority());
}
public static void main(String[] args){
MyThread thread = new MyThread();
thread.setPriority(Thread.MAX_PRIORITY);
thread.start();
}
}