zoukankan      html  css  js  c++  java
  • java线程的方法

    本文将讲述关于java线程的以下方法:

    1.取得线程的名称:Thread.currentThread().getName()

    2.判断线程是否启动:isAlive()

    3.线程的强制执行:join()

    4.线程的休眠:Thread.sleep(2000)

    5.线程的中端:interrupt()

    6.后台线程:setDaemon(true)

    7.线程的优先级:setPriority(8)

    8.线程的礼让:yield()

    一、取得线程的名称

     1 /**
     2  * 取得线程的名称
     3  * */
     4 class hello implements Runnable {
     5     public void run() {
     6         for (int i = 0; i < 3; i++) {
     7             System.out.println(Thread.currentThread().getName());
     8         }
     9     }
    10 
    11     public static void main(String[] args) {
    12         hello he = new hello();
    13         new Thread(he, "A").start();
    14         new Thread(he, "B").start();
    15         new Thread(he).start();
    16     }
    17 }

    结果可能如下:

    A
    Thread-0
    B
    Thread-0
    A
    Thread-0
    B
    A
    B

    说明:如果我们没有指定名字的话,系统自动提供名字。

    二、判断线程是否启动

    提醒一下大家:main方法其实也是一个线程。在java中所有的线程都是同时启动的,至于什么时候,哪个先执行,完全看谁先得到CPU的资源。

    在java中,每次程序运行至少启动2个线程。一个是main线程,一个是垃圾收集线程。因为每当使用java命令执行一个类的时候,实际上都会启动一个JVM,每一个JVM实际就是在操作系统中启动了一个进程。

     1 /**
     2  * 判断线程是否启动
     3  */
     4 class hello implements Runnable {
     5     public void run() {
     6         for (int i = 0; i < 3; i++) {
     7             System.out.println(Thread.currentThread().getName());
     8         }
     9     }
    10  
    11     public static void main(String[] args) {
    12         hello he = new hello();
    13         Thread t = new Thread(he);
    14         System.out.println("线程启动之前---" + t.isAlive());
    15         t.start();
    16         System.out.println("线程启动之后---" + t.isAlive());
    17     }
    18 }

    运行结果:

    线程启动之前---false
    线程启动之后---true
    Thread-0
    Thread-0
    Thread-0

    主线程也有可能在子线程结束之前结束。并且子线程不受影响,不会因为主线程的结束而结束。

    三、线程的强制执行

     1 /**
     2  * 线程的强制执行
     3  * */
     4 class hello implements Runnable {
     5     public void run() {
     6         for (int i = 0; i < 3; i++) {
     7             System.out.println(Thread.currentThread().getName());
     8         }
     9     }
    10 
    11     public static void main(String[] args) {
    12         hello he = new hello();
    13         Thread t = new Thread(he, "A");
    14         t.start();
    15         for (int i = 0; i < 50; ++i) {
    16             if (i > 10) {
    17                 try {
    18                     t.join(); // 强制执行t
    19                 } catch (Exception e) {
    20                     e.printStackTrace();
    21                 }
    22             }
    23             System.out.println("main 线程执行--" + i);
    24         }
    25     }
    26 }

    结果:

    main 线程执行--0
    A
    main 线程执行--1
    A
    main 线程执行--2
    A
    main 线程执行--3
    main 线程执行--4
    main 线程执行--5
    main 线程执行--6
    main 线程执行--7

    .......

    四、线程的休眠

     1 /**
     2  * 线程的休眠
     3  * */
     4 class hello implements Runnable {
     5     public void run() {
     6         for (int i = 0; i < 3; i++) {
     7             try {
     8                 Thread.sleep(2000);
     9             } catch (Exception e) {
    10                 e.printStackTrace();
    11             }
    12             System.out.println(Thread.currentThread().getName() + i);
    13         }
    14     }
    15  
    16     public static void main(String[] args) {
    17         hello he = new hello();
    18         Thread demo = new Thread(he, "线程");
    19         demo.start();
    20     }
    21 }

    【运行结果】:(结果每隔2s输出一个)

    线程0

    线程1

    线程2

    五、线程的中端

     1 /**
     2  * 线程的中断
     3  * */
     4 class hello implements Runnable {
     5     public void run() {
     6         System.out.println("执行run方法");
     7         try {
     8             Thread.sleep(10000);
     9             System.out.println("线程完成休眠");
    10         } catch (Exception e) {
    11             System.out.println("休眠被打断");
    12             return;  //返回到程序的调用处
    13         }
    14         System.out.println("线程正常终止");
    15     }
    16  
    17     public static void main(String[] args) {
    18         hello he = new hello();
    19         Thread demo = new Thread(he, "线程");
    20         demo.start();
    21         try{
    22             Thread.sleep(2000);
    23         }catch (Exception e) {
    24             e.printStackTrace();
    25         }
    26         demo.interrupt(); //2s后中断线程
    27     }
    28 }

    【运行结果】:

    执行run方法

    休眠被打断

    六、后台线程

     1 /**
     2  * 后台线程
     3  * */
     4 class hello implements Runnable {
     5     public void run() {
     6         while (true) {
     7             System.out.println(Thread.currentThread().getName() + "在运行");
     8         }
     9     }
    10  
    11     public static void main(String[] args) {
    12         hello he = new hello();
    13         Thread demo = new Thread(he, "线程");
    14         demo.setDaemon(true);
    15         demo.start();
    16     }
    17 }

    虽然有一个死循环,但是程序还是可以执行完的。因为在死循环中的线程操作已经设置为后台运行了。

    七、线程的优先级

     1 /**
     2  * 线程的优先级
     3  * */
     4 class hello implements Runnable {
     5     public void run() {
     6         for(int i=0;i<3;++i){
     7             System.out.println(Thread.currentThread().getName()+"运行"+i);
     8         }
     9     }
    10  
    11     public static void main(String[] args) {
    12         Thread h1=new Thread(new hello(),"A");
    13         Thread h2=new Thread(new hello(),"B");
    14         Thread h3=new Thread(new hello(),"C");
    15         h1.setPriority(8);
    16         h2.setPriority(2);
    17         h3.setPriority(6);
    18         h1.start();
    19         h2.start();
    20         h3.start();
    21          
    22     }
    23 }

    【运行结果】

    A运行0
    A运行1
    C运行0
    B运行0
    B运行1
    B运行2
    C运行1
    A运行2
    C运行2

    但是请读者不要误以为优先级越高就先执行。谁先执行还是取决于谁先去的CPU的资源

    另外,主线程的优先级是5.

    八、线程的礼让

    在线程操作中,也可以使用yield()方法,将一个线程的操作暂时交给其他线程执行。

     1 /**
     2  * 线程的优先级
     3  * */
     4 class hello implements Runnable {
     5     public void run() {
     6         for(int i=0;i<5;++i){
     7             System.out.println(Thread.currentThread().getName()+"运行"+i);
     8             if(i==3){
     9                 System.out.println("线程的礼让");
    10                 Thread.currentThread().yield();
    11             }
    12         }
    13     }
    14  
    15     public static void main(String[] args) {
    16         Thread h1=new Thread(new hello(),"A");
    17         Thread h2=new Thread(new hello(),"B");
    18         h1.start();
    19         h2.start();
    20          
    21     }
    22 }

    【运行结果】可能是

    A运行0
    B运行0
    A运行1
    B运行1
    A运行2
    B运行2
    A运行3
    B运行3
    线程的礼让
    B运行4
    线程的礼让
    A运行4

  • 相关阅读:
    c/c++字节序转换(转)
    O_DIRECT与O_SYNC区别(转)
    TCMalloc小记(转)
    内存优化总结:ptmalloc、tcmalloc和jemalloc(转)
    不依赖三方库从图像数据中获取宽高-gif、bmp、png、jepg
    Linux查看物理CPU个数、核数、逻辑CPU个数
    unix环境高级编程-3.10-文件共享(转)
    TF_Server gRPC failed, call return code:8:Received message larger than max (45129801 vs. 4194304)
    google doc上的云转换
    telnet 退出命令
  • 原文地址:https://www.cnblogs.com/lnluckybamboo/p/3951266.html
Copyright © 2011-2022 走看看