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);
        }
    }
}
相关推荐
小夏子_riotous4 小时前
openstack的使用——5. Swift服务的基本使用
linux·运维·开发语言·分布式·云计算·openstack·swift
千码君20164 小时前
kotlin:Jetpack Compose 给APP添加声音(点击音效/背景音乐)
android·开发语言·kotlin·音效·jetpack compose
星乐a4 小时前
String vs StringBuilder vs StringBuffer深度解析
java
萧逸才5 小时前
【learn-claude-code-4j】S14FeiShu - 飞书群聊智能体
java·人工智能·ai·飞书
吴声子夜歌5 小时前
ES6——对象的扩展详解
开发语言·javascript·es6
aq55356005 小时前
编程语言对比:从汇编到PHP的四大层级解析
开发语言·汇编·php
kyle~5 小时前
工程数学---Eigen库(C++唯一标配线性代数库)
开发语言·c++·线性代数
CoderCodingNo5 小时前
【GESP】C++五、六级练习题 luogu-P1886 【模板】单调队列 / 滑动窗口
开发语言·c++·算法
好家伙VCC5 小时前
**发散创新:基于Rust的轻量级权限管理库设计与开源许可证实践**在现代分布式系统中,**权限控制(RBAC
java·开发语言·python·rust·开源
xiaoshuaishuai85 小时前
C# 方言识别
开发语言·windows·c#