• Java——线程&进程


    线程

    :是进程中的单个顺序控制流,是一条执行路径。

    • 单线程:一个进程只有一条执行路径。例:记事本.
    • 多线程:一个进程有多条执行路径。例:扫雷.

    多线程的执行方式:

    1. 继承Thread类 
    • 定义一个MyThread类继承Thread类
    •  在MyThread类中重写run()方法
    • 创建MyThread类对象
    • 启动线程
    1. public class MyThreadDome {
    2. public static void main(String[] args){
    3. MyThread my1=new MyThread();
    4. MyThread my2=new MyThread();
    5. //调用run()方法并没有启动线程
    6. // my1.run();
    7. // my2.run();
    8. //void start()导致此线程开始执行;Java虚拟机调用此线程的run()方法
    9. my1.start();
    10. my2.start();
    11. }
    12. }
    13. public class MyThread extends Thread{
    14. @Override
    15. public void run() {
    16. super.run();
    17. for(int i=0;i<100;i++){
    18. System.out.println(i);
    19. }
    20. }
    21. }

    多线程结果:

     重写run()方法?

    run()是用来封装被线程执行的代码。

    run()与start()方法区别?

    run()是用来封装被线程执行的代码。直接调用,相当于普通方法。

    start():启动线程,然后由JVM调用此线程的run()方法。

    设置和获取线程名称

    Thread类中设置和获取线程名称的方法

    • void setName(String name):将此线程的名称更改为等于参数name
    • String getName():返回此线程的名称
    • 通过构造方法获取名称

    1. public class MyThreadDome {
    2. public static void main(String[] args){
    3. MyThread my1=new MyThread();
    4. MyThread my2=new MyThread();
    5. //调用run()方法并没有启动线程
    6. // my1.run();
    7. // my2.run();
    8. my1.setName("张三");
    9. my2.setName("李四");
    10. // MyThread my1=new MyThread("王五");
    11. // MyThread my2=new MyThread("赵茜");
    12. //void start()导致此线程开始执行;Java虚拟机调用此线程的run()方法
    13. my1.start();
    14. my2.start();
    15. }
    16. }
    17. public class MyThread extends Thread{
    18. // public MyThread(){}
    19. // public MyThread(String name){
    20. // super(name);
    21. // }
    22. @Override
    23. public void run() {
    24. super.run();
    25. for(int i=0;i<100;i++){
    26. System.out.println(getName()+":"+i);
    27. }
    28. }
    29. }
    30. /*
    31. private static synchronized int nextThreadNum() {
    32. return threadInitNumber++;
    33. }
    34. */

     

    •  通过带参构造获取值:
    1. public class MyThreadDome {
    2. public static void main(String[] args){
    3. // MyThread my1=new MyThread();
    4. // MyThread my2=new MyThread();
    5. //
    6. // //调用run()方法并没有启动线程
    7. // // my1.run();
    8. // // my2.run();
    9. //
    10. // my1.setName("张三");
    11. // my2.setName("李四");
    12. MyThread my1=new MyThread("王五");
    13. MyThread my2=new MyThread("赵茜");
    14. //void start()导致此线程开始执行;Java虚拟机调用此线程的run()方法
    15. my1.start();
    16. my2.start();
    17. }
    18. }
    19. public class MyThread extends Thread{
    20. public MyThread(){}
    21. public MyThread(String name){
    22. super(name);
    23. }
    24. @Override
    25. public void run() {
    26. super.run();
    27. for(int i=0;i<100;i++){
    28. System.out.println(getName()+":"+i);
    29. }
    30. }
    31. }
    32. /*
    33. private static synchronized int nextThreadNum() {
    34. return threadInitNumber++;
    35. }
    36. */

    //static Thread currentThread()返回对当前正在执行的线程对象的引用:

    1. public class MyThreadDome {
    2. public static void main(String[] args){
    3. // MyThread my1=new MyThread();
    4. // MyThread my2=new MyThread();
    5. //
    6. // //调用run()方法并没有启动线程
    7. // // my1.run();
    8. // // my2.run();
    9. //
    10. // my1.setName("张三");
    11. // my2.setName("李四");
    12. // MyThread my1=new MyThread("王五");
    13. // MyThread my2=new MyThread("赵茜");
    14. // //void start()导致此线程开始执行;Java虚拟机调用此线程的run()方法
    15. // my1.start();
    16. // my2.start();
    17. System.out.println(Thread.currentThread().getName());
    18. }
    19. }
    20. public class MyThread extends Thread{
    21. public MyThread(){}
    22. public MyThread(String name){
    23. super(name);
    24. }
    25. @Override
    26. public void run() {
    27. super.run();
    28. for(int i=0;i<100;i++){
    29. System.out.println(getName()+":"+i);
    30. }
    31. }
    32. }
    33. /*
    34. private static synchronized int nextThreadNum() {
    35. return threadInitNumber++;
    36. }
    37. */

    • 返回来了main,说明在mian线程中执行的。

    实现多线程

    线程调度

    • 分时调度模型

    • 抢占式调度模型(Java使用) 

    Thread类中设置和获取线程优先级的方法:

    》》public final int getPriority():返回此线程的优先级

    》》public final void setPriority(int newPriority):更改此线程的优先级 

    1. public class MyThreadDome {
    2. public static void main(String[] args){
    3. ThreadPriority tpy1=new ThreadPriority();
    4. ThreadPriority tpy2=new ThreadPriority();
    5. ThreadPriority tpy3=new ThreadPriority();
    6. tpy1.setName("张三");
    7. tpy2.setName("李四");
    8. tpy3.setName("王五");
    9. tpy1.start();
    10. tpy2.start();
    11. tpy3.start();
    12. }
    13. }
    14. public class ThreadPriority extends Thread{
    15. public void run(){
    16. for(int i=0;i<100;i++)
    17. {
    18. System.out.println(getName()+":"+i);
    19. }
    20. }
    21. }


    1. public class MyThreadDome {
    2. public static void main(String[] args){
    3. ThreadPriority tpy1=new ThreadPriority();
    4. ThreadPriority tpy2=new ThreadPriority();
    5. ThreadPriority tpy3=new ThreadPriority();
    6. tpy1.setName("张三");
    7. tpy2.setName("李四");
    8. tpy3.setName("王五");
    9. // //public final int getPriority():返回此线程的优先级 获取优先级——都是5
    10. // System.out.println(tpy1.getPriority());//5
    11. // System.out.println(tpy2.getPriority());//5
    12. // System.out.println(tpy3.getPriority());//5
    13. //public final void setPriority(int newPriority):更改此线程的优先级
    14. //获取优先级的范围:
    15. System.out.println("优先级的范围:");
    16. System.out.println(Thread.MAX_PRIORITY);
    17. System.out.println(Thread.MIN_PRIORITY);
    18. System.out.println(Thread.NORM_PRIORITY);
    19. System.out.println("正确设置优先级:");
    20. tpy1.setPriority(5);
    21. tpy2.setPriority(10);
    22. tpy3.setPriority(1);
    23. tpy1.start();
    24. tpy2.start();
    25. tpy3.start();
    26. }
    27. }
    28. public class ThreadPriority extends Thread{
    29. public void run(){
    30. for(int i=0;i<100;i++)
    31. {
    32. System.out.println(getName()+":"+i);
    33. }
    34. }
    35. }

     线程的控制

     

     

  • 相关阅读:
    大众点评字体反爬解析
    AI 律助 Alpha GPT 线上实操发布会,重磅发布!
    AJAX请求的优缺点
    Word 插入的 Visio 图片显示为{EMBED Visio.Drawing.11} 解决方案
    数据库------E-R图和关系模型
    Vue中常见的loader的作用
    C/C++内存管理详解
    Go :验证编译器是否强制执行了复制参数要求(附完整源码)
    power point导出pdf保留字体
    基于springboot的医院体检预约管理系统
  • 原文地址:https://blog.csdn.net/m0_63064861/article/details/126266786