• 关于线程的那些事


    1. 线程创建
    2. 线程中断
    3. 线程等待
    4. 线程休眠
    5. 线程状态
    6. 线程实例

    一.线程的创建---这里给出五种方法

    方法一:

    继承Thread 重写run方法
    1. class MyThread extends Thread{
    2. @Override
    3. public void run() {
    4. System.out.println("thread");
    5. }
    6. }
    7. public class Text1 {
    8. public static void main(String[] args) {
    9. MyThread myThread = new MyThread();
    10. myThread.start();
    11. }
    12. }

    方法二:

    实现MyRunnable接口,重写run
    1. class MyRunnable implements Runnable {
    2. @Override
    3. public void run() {
    4. System.out.println("thread");
    5. }
    6. }
    7. public class Text2 {
    8. public static void main(String[] args) {
    9. MyRunnable myRunnable = new MyRunnable();
    10. Thread thread = new Thread(myRunnable);
    11. thread.start();
    12. }
    13. }

    方法三:

    使用匿名内部类 实现创建Thread子类
    1. public class Text3 {
    2. public static void main(String[] args) {
    3. Thread thread = new MyThread(){
    4. @Override
    5. public void run() {
    6. System.out.println("thread");
    7. }
    8. };
    9. thread.start();
    10. }
    11. }

    方法四:

    使用匿名内部类 实现 实现Runnable接口
    1. public class Text4 {
    2. public static void main(String[] args) {
    3. Thread thread = new Thread(new Runnable() {
    4. @Override
    5. public void run() {
    6. System.out.println("thread");
    7. }
    8. });
    9. thread.start();
    10. }
    11. }

    方法五:

    lambda 表达式
    1. public class Text5 {
    2. public static void main(String[] args) {
    3. Thread thread = new Thread(() -> {
    4. System.out.println("thread");
    5. });
    6. thread.start();
    7. }
    8. }

    注意一下,线程真正的创建是thread.start();

    二.线程中断

    线程中断--其实是让线程快一点结束(run执行完了,线程就执行完了),不是让它半路停下!

    介绍两个方法

    1.自己设置一个 -举个栗子~~

    1. public class Text6 {
    2. private static boolean t = true;
    3. public static void main(String[] args) {
    4. Thread thread = new Thread(() ->{
    5. while(t){
    6. System.out.println("thread");
    7. try {
    8. sleep(1000);
    9. } catch (InterruptedException e) {
    10. e.printStackTrace();
    11. }
    12. }
    13. System.out.println("线程执行完了");
    14. });
    15. thread.start();
    16. try {
    17. Thread.sleep(3000);
    18. } catch (InterruptedException e) {
    19. e.printStackTrace();
    20. }
    21. t = false;
    22. System.out.println("设置线程执行完了");
    23. }
    24. }

    2.用标准库里自带的

    1. public class Text7 {
    2. public static void main(String[] args) {
    3. Thread thread = new Thread(() ->{
    4. while(!Thread.currentThread().isInterrupted()){
    5. System.out.println("thread");
    6. try {
    7. sleep(1000);
    8. } catch (InterruptedException e) {
    9. e.printStackTrace();
    10. //break;
    11. }
    12. }
    13. System.out.println("线程执行完了");
    14. });
    15. thread.start();
    16. try {
    17. Thread.sleep(3000);
    18. } catch (InterruptedException e) {
    19. e.printStackTrace();
    20. }
    21. thread.interrupt();
    22. System.out.println("设置线程执行完了");
    23. }
    24. }

    我们发现它还是没停下来,没关系,加一个break就行

    从中我们也能发现Java中断线程也不是强制的,它是 立即处理,不理会,装作不理会 (像回消息~~)

    三.线程等待

            用join 在main函数中调用thread.join效果就是--main函数阻塞等待,thread线程执行完了,main才继续

    1. public class Text8 {
    2. public static void main(String[] args) throws InterruptedException {
    3. Thread thread = new Thread(() -> {
    4. while(true){
    5. System.out.println("thread");
    6. try {
    7. sleep(1000);
    8. } catch (InterruptedException e) {
    9. e.printStackTrace();
    10. }
    11. }
    12. });
    13. thread.start();
    14. thread.join();
    15. System.out.println("main等待");
    16. while(true){
    17. System.out.println("main");
    18. try {
    19. sleep(1000);
    20. } catch (InterruptedException e) {
    21. e.printStackTrace();
    22. }
    23. }
    24. }
    25. }

    四.线程休眠

            线程休眠就是sleep,调用了则这个PCB就被移动到,另外一个 '阻塞队列' 中,时间到了,就在回到, '就绪队列' 的原来位置

    五线程状态

    NEW安排了工作, 但还未开始
    RUNNABLE可工作 (正在工作中和即将开始工作)
    BLOCKED排队等着其他事情
    WAITING排队等着其他事情
    TIMED_WAITING排队等着其他事情
    TERMINATED工作完成了

     简单来说:

    1. public class Text9 {
    2. public static void main(String[] args) {
    3. Thread thread = new Thread(() -> {
    4. for (int i = 0; i < 50; i++) {
    5. }
    6. });
    7. System.out.println(thread.getState());
    8. thread.start();
    9. while(thread.isAlive()){
    10. System.out.println(thread.getState());
    11. }
    12. System.out.println(thread.getState());
    13. }

    1. public class Text {
    2. public static void main(String[] args) throws InterruptedException {
    3. Thread thread = new Thread(() -> {
    4. for (int i = 0; i < 3; i++) {
    5. System.out.println("thread");
    6. try {
    7. Thread.sleep(1000);
    8. } catch (InterruptedException e) {
    9. e.printStackTrace();
    10. }
    11. }
    12. });
    13. thread.start();
    14. System.out.println(thread.getState());
    15. try {
    16. Thread.sleep(10);
    17. } catch (InterruptedException e) {
    18. e.printStackTrace();
    19. }
    20. System.out.println(thread.getState());
    21. thread.join();
    22. System.out.println(thread.getState());
    23. }
    24. }

     

    六.线程实例

    1. public class Text10 {
    2. public static void main(String[] args) {
    3. Thread thread = new MyThread(){
    4. @Override
    5. public void run() {
    6. System.out.println(this.getName());
    7. }
    8. };
    9. thread.start();
    10. }
    11. }

  • 相关阅读:
    分布式文件系统-minio
    外包干了一个月,技术明显进步。。。。。
    大数据学习3.1 Hadoop环境准备
    Java认识异常(超级详细)
    JavaScript处理数组数据-数据匹配-剔除
    Flask--认识flask与环境准备
    【华为OD机试-计算疫情扩散时间-Java】
    基于stm32单片机矿井瓦斯天然气温湿度检测报警系统
    计算机毕业设计ssm“逢遇”个人博客平台qaoxv系统+程序+源码+lw+远程部署
    uCOSIII实时操作系统 十一 消息传递
  • 原文地址:https://blog.csdn.net/m0_63501066/article/details/126694747