实现多线程的2中方式

方式1:继承Thread类,然后重载run()方法

package thread;

import org.junit.jupiter.api.Test;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @program: springdemo
 * @description: ThreadMode1
 * @author: wangwenguan6
 * @date: 2024-04-10 20:14:20
 * @doc:
 **/
public class ThreadMode1 {

    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

    public static void main(String[] args) {
        ThreadMode1 thread = new ThreadMode1();
        thread.new MyThread1().start();
        thread.new MyThread1().start();
        thread.new MyThread2().start();
    }


    class MyThread1 extends Thread {
        @Override
        public void run() {
            //业务逻辑xxxx
            System.out.println(sdf.format(new Date()) + "\t线程:" + Thread.currentThread().getName()+"启动");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(sdf.format(new Date()) + "\t线程:" + Thread.currentThread().getName()+"执行完毕");
        }
    }

    class MyThread2 extends Thread {
        @Override
        public void run() {
            //业务逻辑xxxx
            System.out.println(sdf.format(new Date()) + "\t线程:" + Thread.currentThread().getName()+"启动");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(sdf.format(new Date()) + "线程:" + Thread.currentThread().getName()+"执行完毕");
        }
    }

}

执行结果为:

2024-04-10 20:28:21.842	线程:Thread-1启动
2024-04-10 20:28:21.842	线程:Thread-2启动
2024-04-10 20:28:21.842	线程:Thread-0启动
2024-04-10 20:28:24.848线程:Thread-2执行完毕
2024-04-10 20:28:26.847	线程:Thread-1执行完毕
2024-04-10 20:28:26.847	线程:Thread-0执行完毕

方式2:实现Runnable接口

package thread;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @program: springdemo
 * @description: ThreadMode2
 * @author: wangwenguan6
 * @date: 2024-04-10 20:31:15
 * @doc:
 **/
public class ThreadMode2 {

    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

    public static void main(String[] args) {
        ThreadMode2 th = new ThreadMode2();

        Thread1 thread1_1 = th.new Thread1();
        Thread1 thread1_2 = th.new Thread1();
        Thread2 thread2 = th.new Thread2();

        new Thread(thread1_1).start();
        new Thread(thread1_2).start();
        new Thread(thread2).start();

    }

    public class Thread1 implements Runnable {
        @Override
        public void run() {
            //业务逻辑xxxx
            System.out.println(sdf.format(new Date()) + "\t线程:" + Thread.currentThread().getName() + "启动");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(sdf.format(new Date()) + "\t线程:" + Thread.currentThread().getName() + "执行完毕");
        }
    }

    public class Thread2 implements Runnable {
        @Override
        public void run() {
            //业务逻辑xxxx
            System.out.println(sdf.format(new Date()) + "\t线程:" + Thread.currentThread().getName() + "启动");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(sdf.format(new Date()) + "\t线程:" + Thread.currentThread().getName() + "执行完毕");
        }
    }

}

执行结果为:

2024-04-10 20:36:43.649	线程:Thread-0启动
2024-04-10 20:36:43.649	线程:Thread-2启动
2024-04-10 20:36:43.649	线程:Thread-1启动
2024-04-10 20:36:46.650	线程:Thread-2执行完毕
2024-04-10 20:36:48.650	线程:Thread-0执行完毕
2024-04-10 20:36:48.650	线程:Thread-1执行完毕

两种方式的差异及区别:使用集成Thread或者使用实现Runnable方式本质是没有特殊区别,如果在业务场景上有复杂的线程操作需求,那就选择继承Thread,如果只是简单的执行一个任务,那就实现runnable

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值