Java 多线程------测试 Thread中的常用方法 + 线程的优先级:

  1 package com.bytezero.threadexer;
  2 
  3 import javax.sound.midi.Soundbank;
  4 
  5 /**
  6  * 测试 Thread中的常用方法
  7  * 1.start(); :启动当前线程,调用当前线程的run()
  8  * 2.run(); :通常需要重写Thread类中的此方法,将创建的线程的操作声明在此方法中
  9  * 3.currentThread():静态方法,返回执行当前代码的线程
 10  * 4.getName():获取当前线程的名字
 11  * 5.setName():设置当前线程的名字   2种
 12  * 6.yield():释放当前CPU的执行权
 13  * 7.join():在线程a中调用线程b的jion(),此时线程a就进入了阻塞状态,直到线程b完全执行
 14  * 以后,线程a才会结束阻塞状态,继续执行.
 15  * 8.stop(): 已过时.当执行此方式时,强制结束当前线程
 16  * 9.sleep(long millitime):让当前线程"睡眠"指定的millitime毫秒.在指定的millitime
 17  * 毫秒时间内,当前线程是阻塞状态.
 18  *10. isAlive(): 判断当前线程是否在存活
 19  *
 20  *
 21  * 线程的优先级:
 22  * 1.
 23  * MAX_PRIORITY:10
 24  * MIN_PRIORITY:1
 25  * NORM_PRIORITY:5  ---->默认优先级
 26  * 2.如何获取和设置当前线程的优先级
 27  *   getPriority(): 获取线程的优先级
 28  *   setPriority(int p):  设置线程的优先级
 29  * 高优先级的线程要抢占低优先级线程CPU的执行器,但是只是从概率上讲,高优先级的线程高
 30  * 概率的情况下被执行并不意味着只有当高优先级的线程执行完以后,低优先级的线程才执行
 31  *
 32  *
 33  *
 34  *
 35  *
 36  *
 37  * @author Bytezero1·zhenglei!    Email:420498246@qq.com
 38  * create  2021-10-15 10:58
 39  */
 40 //测试线程的方法
 41 class MethodThread extends Thread{
 42     @Override
 43     public void run() {
 44         for (int i = 0; i < 100; i++) {
 45             if(i % 2 == 0){
 46 
 47 //                try {
 48 //                    sleep(10);
 49 //                } catch (InterruptedException e) {
 50 //                    e.printStackTrace();
 51 //                }
 52 
 53                 System.out.println(Thread.currentThread().getName()+":"+Thread.currentThread().getPriority()+":" + i);
 54             }
 55 //            if(i % 20 ==0){
 56 ////                this.yield();
 57 //                Thread.currentThread().yield();
 58 //            }
 59 
 60         }
 61     }
 62 
 63     //重写 Thread 里面的构造器
 64     //通过构造器给线程命名
 65     public MethodThread(String name){
 66         super(name);
 67     }
 68 }
 69 
 70 public class ThreadMethodTest {
 71     public static void main(String[] args) {
 72 
 73         MethodThread m1 = new MethodThread("Thread: 1");
 74 //        m1.setName("线程一");
 75 
 76         //设置分线程的优先级
 77         m1.setPriority(Thread.MAX_PRIORITY);
 78 
 79         m1.start();
 80 
 81 
 82         //给主线程命名
 83         Thread.currentThread().setName("主线程");
 84         Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
 85         for (int i = 0; i < 100; i++) {
 86             if(i % 2 == 0){
 87                 System.out.println(Thread.currentThread().getName()+Thread.currentThread().getPriority()+":" + i);
 88             }
 89 
 90 //            if(i == 20){
 91 //                try {
 92 //                    m1.join();
 93 //                } catch (InterruptedException e) {
 94 //                    e.printStackTrace();
 95 //                }
 96 //            }
 97         }
 98 //        System.out.println(m1.isAlive());
 99     }
100 
101 }

 

 ............................

posted on 2021-10-15 19:02  Bytezero!  阅读(158)  评论(0编辑  收藏  举报