Java入门12(多线程)

打印 上一主题 下一主题

主题 862|帖子 862|积分 2586

多线程

线程的实现方式


  • 继承 Thread 类:一旦继承了 Thread 类,就不能再继承其他类了,可拓展性差
  • 实现 Runnable 接口:仍然可以继承其他类,可拓展性较好
  • 使用线程池
继承Thread 类

​        不能通过线程对象调用 run() 方法,需要通过 t1.start() 方法,使线程进入到就绪状态,只要进入到就绪状态的线程才有机会被JVM调度选中
  1. // 这是一个简单的栗子
  2. public class StudentThread extends Thread{
  3.     public StudentThread(String name) {
  4.         super(name);
  5.     }
  6.     @Override
  7.     public void run() {
  8.         for (int i = 0; i < 2; i++) {
  9.             System.out.println("This is a test thread!");
  10.             System.out.println(this.getName());
  11.         }
  12.     }
  13. }
  14. // 启动类
  15. public static void main(String[] args) {
  16.     Thread t1 = new StudentThread();
  17.     // 不能通过线程对象调用run()方法
  18.     // 通过 t1.start() 方法,使线程进入到就绪状态,只要进入到就绪状态的线程才有机会被JVM调度选中
  19.     t1.start();
  20. }
复制代码
实现 Runable 接口

​        实现方式需要借助 Thread 类的构造函数,才能完成线程对象的实例化
  1. // 介还是一个简单的栗子
  2. public class StudentThreadRunnable implements Runnable{
  3.     @Override
  4.     public void run() {
  5.         for (int i = 0; i < 2; i++) {
  6.             System.out.println("This is a test thread!");
  7.             System.out.println(Thread.currentThread().getName());
  8.         }
  9.     }
  10. }
  11. // 启动类
  12. public static void main(String[] args) {
  13.     // 实现方式需要借助 Thread 类的构造函数,才能完成线程对象的实例化
  14.     StudentThreadRunnable studentThreadRunnable = new StudentThreadRunnable();
  15.     Thread t01 = new Thread(studentThreadRunnable);
  16.     t01.setName("robot010");
  17.     t01.start();
  18. }
复制代码
匿名内部类实现

​        在类中直接书写一个当前类的子类,这个类默认不需要提供名称,类名由JVM临时分配
  1. public static void main(String[] args) {
  2.     Thread t01 = new Thread(){
  3.         @Override
  4.         public void run() {
  5.             for (int i = 0; i < 2; i++) {
  6.                 System.out.println("This is a test thread!");
  7.             }
  8.             System.out.println(Thread.currentThread().getName()); // 线程名
  9.             System.out.println(this.getClass().getName()); // 匿名线程类类名
  10.         }
  11.     };
  12.     t01.start();
  13. }
复制代码
线程的休眠(sleep方法)

​        sleep方法,会使当前线程暂停运行指定时间,单位为毫秒(ms),其他线程可以在sleep时间内,获取JVM的调度资源
  1. // 这是一个计时器
  2. public class TimeCount implements Runnable{
  3.     @Override
  4.     public void run() {
  5.         int count = 0;
  6.         while(true){
  7.             System.out.println(count);
  8.             count++;
  9.             try {
  10.                 Thread.sleep(1000);
  11.             } catch (InterruptedException e) {
  12.                 throw new RuntimeException(e);
  13.             }
  14.         }
  15.     }
  16. }
  17. // 测试类
  18. public static void main(String[] args) {
  19.     System.out.println("这是main方法运行的时候,开启的主线程~~~");
  20.     TimeCount timeCount = new TimeCount();
  21.     Thread timeThread = new Thread(timeCount);
  22.     System.out.println("开启计时器");
  23.     timeThread.start();
  24.     System.out.println("主线程即将休眠>>>>>>>>>>>");
  25.     try {
  26.         Thread.sleep(20000);
  27.     } catch (InterruptedException e) {
  28.         e.printStackTrace();
  29.     }
  30.     System.out.println(">>>>>>>>>>>主线程休眠结束~~~~~");
  31. }
复制代码
线程的加入(join方法)

​        被 join 的线程会等待 join 的线程运行结束之后,才能继续运行自己的代码
  1. public static void main(String[] args) {
  2.     Thread thread01 = new Thread(){
  3.         @Override
  4.         public void run(){
  5.             for (int i = 0; i < 10; i++) {
  6.                 System.out.println("This is thread-01!");
  7.             }
  8.         }
  9.     };
  10.     thread01.start();
  11.     try {
  12.         thread01.join();
  13.     } catch (InterruptedException e) {
  14.         e.printStackTrace();
  15.     }
  16.     Thread thread02 = new Thread(){
  17.         @Override
  18.         public void run(){
  19.             for (int i = 0; i < 10; i++) {
  20.                 System.out.println("This is thread-02!");
  21.             }
  22.         }
  23.     };
  24.     thread02.start();
  25. }
  26. // thread02 会等待 thread01 完全跑完,才会开始自己的线程
复制代码
线程的优先级(priority方法)

​        优先级高的线程会有更大的几率竞争到JVM的调度资源,但是高优先级并不代表绝对,充满玄学✨
  1. public static void main(String[] args) {
  2.     Thread thread01 = new Thread(){
  3.         @Override
  4.         public void run(){
  5.             for (int i = 0; i < 10; i++) {
  6.                 System.out.println("This is thread-01! " + Thread.currentThread().getPriority());
  7.             }
  8.         }
  9.     };
  10.     Thread thread02 = new Thread(){
  11.         @Override
  12.         public void run(){
  13.             for (int i = 0; i < 10; i++) {
  14.                 System.out.println("This is thread-02! " + Thread.currentThread().getPriority());
  15.             }
  16.         }
  17.     };
  18.     thread01.setPriority(1);
  19.     thread02.setPriority(10);
  20.     // 尽管thread02优先级高于thread01,但是也有可能
  21.     thread01.start();
  22.     thread02.start();
  23. }
复制代码
线程的让步(yield方法)

​        立刻让出JVM的调度资源,并且重新参与到竞争中
[code]public static void main(String[] args) {    Thread thread01 = new Thread(){        @Override        public void run(){            for (int i = 1; i
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

雁过留声

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表