Java多线程3:Thread中的方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yuming226/article/details/82846581
获取当前线程的对象

currentThread()方法可返回代码段正在被哪个线程调用的信息。

public class Demo2_Current {
     public static void main(String[] args) {
          new Thread() {
              public void run() {
                   System.out.println(getName() + "...aaaaaaaas");
              }
          }.start();
          
          new Thread(new Runnable() {
              @Override
              public void run() {
                   //Thread.CurrentThread()获取当前正在执行的线程
                   System.out.println(Thread.currentThread().getName() + "...bb");
                   
              }
          }).start();
          
          Thread.currentThread().setName("我是主线程");
          System.out.println(Thread.currentThread().getName());
     }
}
sleep ()方法

sleep()方法的作用是在指定的毫秒数内让当前“正在执行的线程”休眠(暂停执行)。这个“正在执行的线程”是指currentThread()返回的结果。

public class Dmeo3_Sleep {
     
     public static void main(String[] args) throws InterruptedException {
          new Thread() {
              public void run() {
                   for(int i = 0;i < 10 ;i ++){
                        try {
                             Thread.sleep(1000);
                        } catch (InterruptedException e) {
                             e.printStackTrace();
                        }
                   
                        System.out.println(getName() + "...aaaaaaaa");
                   }
              }
          }.start();
          new Thread() {
              public void run() {
                   for(int i = 0 ; i < 20 ;i ++) {
                        try {
                             Thread.sleep(1000);
                        } catch (InterruptedException e) {
                             e.printStackTrace();
                        }
                   
                        System.out.println(getName() + "...bb");
                   }
              }
          }.start();
     }
     public static void demo() throws InterruptedException {
          for(int i = 20 ; i >= 0 ; i --) {
              Thread.sleep(1000);
              System.out.println("倒计时 " + i + "秒");
          }
     }
}
守护线程

setDaemon(),设置一个线程为守护线程,该线程不会单独执行,当其他非守护线程都执行结束后,自动退出。典型的守护线程就是垃圾回收线程,当进程中没有非守护线程了,则垃圾回收线程也就没有存在的必要了,自动销毁。

public class Dmeo4_Daemon {
     /**
      * 守护线程
      */
     public static void main(String[] args) {
          
          Thread t1 = new Thread() {
              public void run() {
                   for(int i = 0 ; i < 2 ; i ++) {
                        System.out.println(getName() + "...aaaaaaaaa");
                   }
              }
          };
          
          Thread t2 = new Thread(){
              public void run() {
                   for(int i = 0 ; i < 50 ; i ++) {
                        System.out.println(getName() + "...bb");
                   }
              }
          };
          
          t2.setDaemon(true);
          t1.start();
          t2.start();
     }
}

加入线程

join(),当前线程暂停,等待指定的线程执行结束后,当前线程再继续。
join(int),可以等待指定的毫秒之后继续。

public class Demo5_Join {
     /**
      * join(),当前线程暂停,等待指定的线程执行结束后,当前线程再继续
      */
     public static void main(String[] args) {
          Thread t1 = new Thread() {
              public void run(){
                   for(int i = 0;i < 10; i++){
                        System.out.println(getName() + "...aaaaaaaaaaaaa");
                   }
              }
          };
          Thread t2 = new Thread() {
              public void run() {
                   for(int i = 0; i < 10; i++) {
                        if(i == 2) {
                             try {
                                  //t1.join();
                                  t1.join(1);        //插队指定的时间,过了指定时间后两条线程交替执行
                             } catch (InterruptedException e) {
                                  e.printStackTrace();
                             }
                        }
                        System.out.println(getName() + "...bb");
                   }
              }
          };
          t1.start();
          t2.start();
          
          
          
     }
}

礼让线程

yield让出CPU:放弃当前的CPU资源,将它让给其他的任务去占用CPU执行时间。但放弃的时间不确定,有可能刚刚放弃,马上又获得CPU时间片。

public class Demo6_Yield {
     /**
      * yield让出cpu,礼让线程
      * 注意:效果不是很明显
      */
     
     public static void main(String[] args) {
          new MyThread1().start();
          new MyThread1().start();
     }
}
class MyThread1 extends Thread {
     public void run() {
          for(int i = 1; i <= 1000; i++) {
              if(i % 10 == 0) {
                   Thread.yield();//让出cpu
              }
              System.out.println(getName() + "..." + i);
          }
     }
}
设置线程的优先级

setPriority()设置线程的优先级;在Java中,线程的优先级分为1 ~ 10这10个等级,如果小于1或大于10,则JDK抛出异常throw new IllegalArgumentException()。

import java.time.chrono.MinguoChronology;
public class Demo7_Priority {
     /**
      * 设置线程的优先级
      * @param args
      */
     public static void main(String[] args) {
          Thread t1 = new Thread() {
              public void run() {
                   for(int i =0; i < 100; i++) {
                        System.out.println(getName() + "...aaaaaaaaaaaaaa");
                   }
              }
          };
          Thread t2 = new Thread() {
              public void run() {
                   for(int i =0; i < 100; i++) {
                        System.out.println(getName() + "...bb");
                   }
              }
          };
          //t1.setPriority(10);
          //t2.setPriority(1);
          
          t1.setPriority(Thread.MIN_PRIORITY);
          t2.setPriority(Thread.MAX_PRIORITY);
          
          t1.start();
          t2.start();
          
     }
}
isAlive()方法

动能是判断当前的线程是否处于活动状态。什么是活动状态呢?活动状态就是线程已经启动且尚未终止。线程处于正在运行或准备开始运行的状态,就认为线程是“存活”的。

package unit1;

public class Demo5_Run {
	public static void main(String[] args) {
		Demo5_Thread thread = new Demo5_Thread();
		System.out.println("begin ==" + thread.isAlive());
		thread.start();
		System.out.println("end ==" + thread.isAlive());
		
	}
}

class Demo5_Thread extends Thread {
	public void run() {
		System.out.println("run=" + this.isAlive());
	}
}

猜你喜欢

转载自blog.csdn.net/yuming226/article/details/82846581