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);
        }
    }
}
相关推荐
阳光阿盖尔3 分钟前
EasyExcel的基本使用——Java导入Excel数据
java·开发语言·excel
二十雨辰4 分钟前
[苍穹外卖]-12Apache POI入门与实战
java·spring boot·mybatis
程序员皮皮林5 分钟前
开源PDF工具 Apache PDFBox 认识及使用(知识点+案例)
java·pdf·开源·apache
蔚一5 分钟前
Java设计模式—面向对象设计原则(三) -----> 依赖倒转原则DIP(完整详解,附有代码+案例)
java·开发语言·设计模式·intellij-idea·依赖倒置原则
liang899911 分钟前
SpringSecurity原理解析(七):权限校验流程
java·开发语言
LQS202011 分钟前
基于Python实现一个浪漫烟花秀
开发语言·python
QXH20000012 分钟前
数据结构—单链表
c语言·开发语言·数据结构
梅如你13 分钟前
python批量对遥感影像进行归一化与数据清洗
开发语言·python
imaima66614 分钟前
数据结构----栈和队列
开发语言·数据结构
sinat_2765225718 分钟前
C++中move的使用
开发语言·c++