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);
        }
    }
}
相关推荐
Aurorar0rua26 分钟前
C Primer Plus Notes 09
java·c语言·算法
nongcunqq1 小时前
abap 操作 excel
java·数据库·excel
R-G-B2 小时前
【02】C#入门到精通——C# 变量、输入/输出、类型转换
开发语言·c#·c# 变量·c#输入/输出·c#类型转换
星河队长2 小时前
C# 软件加密方法,有使用时间限制,同时要防止拷贝
开发语言·c#
史迪奇_xxx2 小时前
10、一个简易 vector:C++ 模板与 STL
java·开发语言·c++
2301_801252222 小时前
Java中的反射
java·开发语言
Kiri霧3 小时前
Rust开发环境搭建
开发语言·后端·rust
weixin-a153003083163 小时前
[数据抓取-1]beautifulsoup
开发语言·python·beautifulsoup
遇印记3 小时前
大二java学习笔记:二维数组
java·笔记·学习
小杨同学yx4 小时前
有关maven的一些知识点
java·开发语言