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);
        }
    }
}
相关推荐
XH华4 小时前
C语言第十一章内存在数据中的存储
c语言·开发语言
叫我阿柒啊5 小时前
Java全栈开发面试实战:从基础到微服务架构
java·vue.js·spring boot·redis·git·full stack·interview
小凡敲代码6 小时前
2025年金九银十Java面试场景题大全:高频考点+深度解析+实战方案
java·程序员·java面试·后端开发·求职面试·java场景题·金九银十
AndrewHZ6 小时前
【python与生活】如何用Python写一个简单的自动整理文件的脚本?
开发语言·python·生活·脚本·文件整理
拉法豆粉6 小时前
在压力测试中如何确定合适的并发用户数?
java·开发语言
枯萎穿心攻击6 小时前
Unity VS UE 性能工具与内存管理
开发语言·游戏·unity·ue5·游戏引擎·虚幻·虚幻引擎
爱上纯净的蓝天6 小时前
迁移面试题
java·网络·c++·pdf·c#
老赵的博客6 小时前
c++ 常用接口设计
开发语言·c++
binbinaijishu886 小时前
Python爬虫入门指南:从零开始的网络数据获取之旅
开发语言·爬虫·python·其他
chenglin0167 小时前
Logstash_Input插件
java·开发语言