java 多线程 Thread

java 复制代码
package com.data.entity;

public class MyThread extends Thread{
    public String name;

    public MyThread() {}

    public MyThread(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        //super.run();
        for (int i=0;i<=100;i++) {
            System.out.println(this.name+"===="+i);
        }
    }
}
java 复制代码
package com.data.entity;

public class MyRunnable implements Runnable{
    public String name;

    public MyRunnable() {}

    public MyRunnable(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        for (int i=1;i<=100;i++) {
            System.out.println(this.name+"====="+i);
        }
    }
}
java 复制代码
import com.data.entity.MyRunnable;
import com.data.entity.MyThread;

public class text7 {
    public static void main(String[] args) {
        //继承Thread, 重写run方法
//        Thread t1 = new MyThread("t1");
//        Thread t2 = new MyThread("t2");
//        t1.start();
//        t2.start();
//        //t1.run();
//        //t2.run();


        //实现Runnable,重写run方法. 在Thread对象的构造中传入任务对象  线程任务
        //推荐使用
//        Runnable r = new MyRunnable("t1");   //先创建任务对象
//        Thread t1 = new Thread(r);   //将任务对象传入线程对象
//        t1.start();
//
//        Runnable r2 = new MyRunnable("t2");   //先创建任务对象
//        Thread t2 = new Thread(r2);   //将任务对象传入线程对象
//        t2.start();


        //任务只会执行一次时,可以通过匿名内部类或者lambda简化书写
        Thread t1=new Thread(new Runnable() {
            @Override
            public void run() {link_for("t1:: ===");}
        });
        Thread t2=new Thread(()->{link_for("t2> ===");});
        Thread t3=new Thread(()->{link_for("t3> ===");});

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

    }

    public static void link_for(String ss){
        for (int i = 101; i <=200 ; i++) {
            System.out.println(ss+i);
        }
    }
}
相关推荐
qeen871 小时前
【C++】智能指针介绍
开发语言·c++·笔记·指针
hanchenxing1 小时前
本地AI绘画网关: 用 30 行 Python 把 NVIDIA FLUX.2 Klein 接入 OpenAI 兼容客户端Python
开发语言·python·ai作画·ai绘画·nvidia
摆烂z1 小时前
定时任务同步数据--续跑&重试
java·mybatis·ai编程
CAE虚拟与现实2 小时前
docker desktop中的build功能是要build什么
java·docker·容器
PHP实战开发录3 小时前
PHP脚本手动能跑定时任务却失败
开发语言·php·开发
郝学胜-神的一滴4 小时前
Effective Python 条款 10 :海象运算符_=
开发语言·python·程序人生·开源
wuyk5554 小时前
Python零基础入门第十四章:异常处理(try-except)
开发语言·python
wuyk5554 小时前
【Socket 进阶之路】第 3 章 TCP 三次握手 & 四次挥手深度剖析|连接建立、断开、状态机、TIME_WAIT 核心工程问题
服务器·开发语言·网络·物联网·网络协议·tcp/ip
allnlei4 小时前
s6-overlay - 装在 Docker 容器里的轻量级管家
java·docker·容器
彧azz10 小时前
图的存储结构详解:邻接矩阵的原理、实现与应用
开发语言·数据结构·学习·php