广州火车站2345网址导航官网下载安装
目录
一、Thread类基本用法
1.1 Thread常见构造方法
1.2 Thread常见属性
二、多线程常用的创建方式
2.1 继承Thread类
2.2 实现Runnable接口
2.3 继承Thread接口,使用匿名内部类
2.4实现Runnable接口,使用匿名内部类
2.5使用lambda表达式
三、线程的启动
3.1 start()方法和run()方法的区别
四、线程终止
4.1通过共享标记来终止
4.2通过调用interrupt()方法来终止
五、多线程等待
六、多线程休眠
七、获取多线程实例对象
一、Thread类基本用法
1.1 Thread常见构造方法
方法 | 说明 |
Thread() | 创建线程对象 |
Thread(Runnable target) | 使用 Runnable 对象创建线程对象 |
Thread(String name) | 创建线程对象,并命名 |
Thread(Runnable target, String name) | 使用 Runnable 对象创建线程对象,并命名 |
Thread(ThreadGroup group, Runnable target) | 线程可以被用来分组管理,分好的组即为线程组,了解即可 |
1.2 Thread常见属性
属性 | 获取方法 |
ID | getId() |
名称 | getName() |
状态 | getState() |
优先级 | getPriority() |
是否后台线程 | isDaemon(),后台线程不影响进程的结束 |
是否存活 | isAlive() |
是否被中断 | isInterrupted() |
二、多线程常用的创建方式
2.1 继承Thread类
class MyThread extends Thread {//重写run方法@Overridepublic void run() {}
}public class test{public static void main(String[] args){MyThread t = new MyThread();t.start();}
}
2.2 实现Runnable接口
class MyRunnable implements Runnable{//重写run方法@Overridepublic void run(){}
}public class test{public static void main(Strings[] args){MyRunnable runnable = new MyRunnable();//这个对象需要放在Thread中才能创建线程Thread t = new Thread(runnable);t.start();}
}
2.3 继承Thread接口,使用匿名内部类
public class test {public static void main(Strings[] args){Thread t = new Thread(){//重写run方法@Overridepublic void run(){}};t.start();}
}
2.4实现Runnable接口,使用匿名内部类
public class test{public static void main(Strings[] args){Thread t = new Thread(new Runnable{//重写run方法@Overridepublic void run(){} });t.start();}
}
2.5使用lambda表达式
public class test{public static void main(String[] args){Thread t = new Thread(()->{//直接写具体的内容 },name);//可以自定义多线程的名字t.start();}
}
三、线程的启动
3.1 start()方法和run()方法的区别
- start方法会调用api接口创建线程
- run方法只是Thread类中的一个方法,不会创建出线程
- start方法执行的过程中会并发执行其他任务
- run方法不能并发执行其他任务,只能等run方法中的任务执行完才能执行下一个任务
四、线程终止
4.1通过共享标记来终止
这里会涉及到一个lambda表达式的变量捕获,当作为局部变量的时候最好是不可变的,作为成员变量的时候要是静态的。
public class Demo6 {//设置标记位public static boolean flag = false;public static void main(String[] args) throws InterruptedException {Thread t = new Thread(()->{while (!flag) {System.out.println("hello Thread");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}});t.start();Thread.sleep(1000);flag = true;//通过将标志位转换达到停止程序的目的System.out.println("程序终止");}
}
4.2通过调用interrupt()方法来终止
调用interrupt()方法终止程序的时候,在唤醒程序后会将标志位进行清除,这里就会留有很大的操作空间供自己操作。包括是否立即终止、执行其他操作、继续执行当前代码。
方法 | 说明 |
public void interrupt() | 中断对象关联的线程,如果线程正在阻塞,则以异常方式通知, 否则设置标志位 |
public static boolean interrupted() | 判断当前线程的中断标志位是否设置,调用后清除标志位 |
public boolean isInterrupted() | 判断对象关联的线程的标志位是否设置,调用后不清除标志位 |
public class Demo7 {public static void main(String[] args) throws InterruptedException {Thread t = new Thread(()->{、//利用isInterrupted来设置标记while (!Thread.currentThread().isInterrupted()) {System.out.println("hello Thread");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();break;}}});t.start();Thread.sleep(3000);t.interrupt();//将标记转换进而终止程序System.out.println("程序终止");}
}
五、多线程等待
多线程的执行是随机的,但是一般程序的编写我们都是希望有顺序的执行和结束,所以这里采用join()方法对线程进行等待。当一个线程加入join()方法时,就要阻塞等待调用该方法的线程执行完才能执行结束当前线程。
方法 | 说明 |
public void join() | 等待线程结束 |
public void join(long millis) | 等待线程结束,最多等 millis 毫秒 |
public void join(long millis, int nanos) | 同理,但可以更高精度 |
public class Demo8 {public static void main(String[] args) {Thread t1 = new Thread(()->{for (int i = 0; i < 3; i++) {System.out.println("t1正在执行");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("t1结束");});Thread t2 = new Thread(()->{for (int i = 0; i < 3; i++) {System.out.println("t2正在执行");try {Thread.sleep(1000);t1.join();//需要等待t1执行完毕} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("t2结束");});t1.start();t2.start();}
}
六、多线程休眠
也是我们比较熟悉一组方法,有一点要记得,因为线程的调度是不可控的,所以,这个方法只能保证实际休眠时间是大于等于参数设置的休眠时间的
方法 | 说明 |
public static void sleep(long millis) throws InterruptedException | 休眠当前线程 millis 毫秒 |
public static void sleep(long millis, int nanos) throws InterruptedException | 可以更高精度的休眠 |
public class ThreadDemo {public static void main(String[] args) throws InterruptedException {System.out.println("开始");Thread.sleep(1000);//线程进行休眠1sSystem.out.println("结束");}
}
七、获取多线程实例对象
方法 | 说明 |
public static Thread currentThread(); | 返回当前线程对象的引用 |
public class ThreadDemo {public static void main(String[] args) {Thread thread = Thread.currentThread();System.out.println(thread.getName());}
}