【Java代码-A003】通过Java完成多线程的创建与使用,快速上手

前言

  • 通过"Java"完成多线程的创建与使用,快速上手;

实操

【前提】

  • 使用"Windows 11"系统通过"IntelliJ IDEA"软件完成;

【目录】

  • “方式1”:通过继承"Thread"完成-正常操作;
  • “方式2”:通过实现"Runnable"完成-正常操作;
  • “方式3”:通过继承"Thread"完成-匿名内部类;
  • “方式4”:通过实现"Runnable"完成-匿名内部类;
  • “方式5”:通过继承"Thread"完成-箭头函数;
  • “方式6”:通过实现"Runnable"完成-箭头函数;

【方式1】

代码

【类名A】

ThreadRunningA

【代码A】

public class ThreadRunningA {
 public static void main(String[] args) {
     Thread t1 = new MyThreadA();
     Thread t2 = new MyThreadB();
     Thread t3 = new MyThreadC();

     t1.start();
     t2.start();
     t3.start();
 }
}

【类名B】

MyThreadA

【代码B】

public class MyThreadA extends Thread{
 public void run() {
     for (int i = 0; i < 100; i++) {
         System.out.print("A、");
     }
 }
}

【类名C】

MyThreadB

【代码C】

public class MyThreadB extends Thread {
 public void run() {
     for (int i = 0; i < 100; i++) {
         System.out.print("B、");
     }
 }
}

【类名D】

MyThreadC

【代码D】

public class MyThreadC extends Thread {
 public void run() {
     for (int i = 0; i < 100; i++) {
         System.out.print("C、");
     }
 }
}

结果

在这里插入图片描述

【方式2】

代码

【类名A】

ThreadRunningB

【代码A】

public class ThreadRunningB {
public static void main(String[] args) {
  Runnable r1 = new MyRunnableA();
  Runnable r2 = new MyRunnableB();
  Runnable r3 = new MyRunnableC();

  Thread t1 = new Thread(r1);
  Thread t2 = new Thread(r2);
  Thread t3 = new Thread(r3);

  t1.start();
  t2.start();
  t3.start();
}
}

【类名B】

MyRunnableA

【代码B】

public class MyRunnableA implements Runnable {
public void run(){
  for(int i = 0;i<100;i++){
      System.out.print("A、");
  }
}
}

【类名C】

MyRunnableB

【代码C】

public class MyRunnableB implements Runnable {
public void run(){
  for(int i = 0;i<100;i++){
      System.out.print("B、");
  }
}
}

【类名D】

MyRunnableC

【代码D】

public class MyRunnableC implements Runnable {
public void run(){
  for(int i = 0;i<100;i++){
      System.out.print("C、");
  }
}
}

结果

在这里插入图片描述

【方式3】

代码

【类名A】

ThreadRunningC

【代码A】

public class ThreadRunningC {
    // 使用内名内部类完成线程的创建
    public static void main(String[] args) {
        Thread t1 = new Thread() {
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.print("A、");
                }
            }
        };
        Thread t2 = new Thread() {
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.print("B、");
                }
            }
        };
        Thread t3 = new Thread() {
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.print("C、");
                }
            }
        };

        t1.start();
        t2.start();
        t3.start();
    }
}

结果

在这里插入图片描述

【方式4】

代码

【类名A】

ThreadRunningD

【代码A】

public class ThreadRunningD {
   // 使用内名内部类完成线程的创建
   public static void main(String[] args) {
       Runnable r1 = new Runnable() {
           public void run() {
               for (int i = 0; i < 100; i++) {
                   System.out.print("A、");
               }
           }
       };
       Runnable r2 = new Runnable() {
           public void run() {
               for (int i = 0; i < 100; i++) {
                   System.out.print("B、");
               }
           }
       };
       Runnable r3 = new Runnable() {
           public void run() {
               for (int i = 0; i < 100; i++) {
                   System.out.print("C、");
               }
           }
       };

       Thread t1 = new Thread(r1);
       Thread t2 = new Thread(r2);
       Thread t3 = new Thread(r3);

       t1.start();
       t2.start();
       t3.start();
   }
}

结果

在这里插入图片描述

【方式5】

代码

【类名A】

ThreadRunningE

【代码A】

public class ThreadRunningE {
    // 使用箭头函数完成线程的创建
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                System.out.print("A、");
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                System.out.print("B、");
            }
        });
        Thread t3 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                System.out.print("C、");
            }
        });

        t1.start();
        t2.start();
        t3.start();
    }
}

结果

在这里插入图片描述

【方式6】

代码

【类名A】

ThreadRunningF

【代码A】

public class ThreadRunningF {
   // 使用箭头函数完成线程的创建
   public static void main(String[] args) {
       Runnable r1 = () -> {
           for (int i = 0; i < 100; i++) {
               System.out.print("A、");
           }
       };
       Runnable r2 = () -> {
           for (int i = 0; i < 100; i++) {
               System.out.print("B、");
           }
       };
       Runnable r3 = () -> {
           for (int i = 0; i < 100; i++) {
               System.out.print("C、");
           }
       };

       Thread t1 = new Thread(r1);
       Thread t2 = new Thread(r2);
       Thread t3 = new Thread(r3);

       t1.start();
       t2.start();
       t3.start();
   }
}

结果

在这里插入图片描述

后记

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SUNxRUN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值