创建线程的7种方法&单线程 VS 多线程

创建线程的7种方法

继承 Thread 类

第一种

/*
    继承Thread类
 */
public class ThreadDemo1 {
    static class MyThread extends Thread {
        @Override
        public void run() {
            //线程执行任务
            System.out.println("当前线程名称:" + Thread.currentThread().getName());
        }
    }
    public static void main(String[] args) {
        //创建线程
        Thread t1 = new MyThread();
        //运行线程
        t1.start();

        System.out.println("主线程名称:" + Thread.currentThread().getName());
    }
}


//执行结果
主线程名称:main
当前线程名称:Thread-0

第二种

public class ThreadDemo2 {
    public static void main(String[] args) {
        Thread thread = new Thread(){
            @Override
            public void run() {
                System.out.println("当前线程名称:" + Thread.currentThread().getName());
            }
        };

        //执行线程
        thread.start();
    }
}

//执行结果
当前线程名称:Thread-0

实现 Runnable 接口

第三种

public class ThreadDemo3 {
    static class MyRunnable implements Runnable {
        @Override
        public void run() {
            System.out.println("当前线程名称:" + Thread.currentThread().getName());
        }
    }

    public static void main(String[] args) {
        //新建Runnable
        MyRunnable runnable = new MyRunnable();
        //创建线程
        Thread thread = new Thread(runnable);
        //启动线程
        thread.start();
    }
}


//执行结果
当前线程名称:Thread-0

第四种

//最主流的写法
public class ThreadDemo4 {
    public static void main(String[] args) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("当前线程名称:" + Thread.currentThread().getName());
            }
        });

        //启动线程
        thread.start();
    }
}


//执行结果
当前线程名称:Thread-0

第五种

public class ThreadDemo5 {
    public static void main(String[] args) {
        //lambda + 匿名 Runnable 的方法  (JDK 8 后版本可使用此方法)
        Thread thread = new Thread(()->{
            System.out.println("当前线程名称:" + Thread.currentThread().getName());
        });

        thread.start();
    }
}

//执行结果
当前线程名称:Thread-0

第六种

public class ThreadDemo5 {
    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("当前线程名称:" + Thread.currentThread().getName());
            }
        };

        Thread thread = new Thread(runnable);
        thread.start();
    }
}

//执行结果
当前线程名称:Thread-0

实现 Callable 接口

第七种

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

/*
    创建并得到线程的执行结果
    实现 Callable 接口 + Future 的方式
 */
public class ThreadDemo6 {
    //创建了线程的任务
    static class MyCallable implements Callable<Integer> {

        @Override
        public Integer call() throws Exception {
            //生成一个随机数
            int num = new Random().nextInt(10);
            System.out.println("子线程:" + Thread.currentThread().getName() + ",随机数:" + num);
            return num;
        };
    }
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //创建 Callable
        MyCallable callable = new MyCallable();

        //创建 FutureTask 对象接收返回值
        FutureTask<Integer> future = new FutureTask<>(callable);

        //创建Thread
        Thread thread = new Thread(future);

        //执行线程
        thread.start();

        int result = future.get();

        System.out.println(String.format("线程名:%s,数字:%d",Thread.currentThread().getName(),result));

    }
}

//执行结果
子线程:Thread-0,随机数:6
线程名:main,数字:6

单线程 VS 多线程

public class ThreadText {
    public static final Long count = 5_0000_0000L;
    public static void main(String[] args) {

        //调用多线程的方法
        concorrency();

        //调用单线程的执行方法
        single();
    }

    //单线程的方法
    private static void single() {
        //开始时间
        Long sTime = System.currentTimeMillis();

        int a = 0;
        for (int i = 0; i < 3 * count; i++) {
            a++;
        }

        //结束时间
        Long eTime = System.currentTimeMillis();
        System.out.println("单线程的执行时间:" + (eTime - sTime));
    }

    //多线程的方法
    private static void concorrency() {


        //开始时间
        Long sTime = System.currentTimeMillis();

        //创建线程任务1
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                int a = 0;
                for (int i = 0; i < count; i++) {
                    a++;
                }
            }
        });

        //开始执行线程1
        t1.start();

        //创建线程任务2
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                int b = 0;
                for (int i = 0; i < count; i++) {
                    b++;
                }
            }
        });
        //开始执行线程2
        t2.start();

        //主线程执行
        int c = 0;
        for (int i = 0; i < count; i++) {
            c++;
        }


        //结束时间
        Long eTime = System.currentTimeMillis();
        //结果
        System.out.println("多线程执行时间:" + (eTime - sTime));
    }
}

//执行结果
多线程执行时间:332
单线程的执行时间:899
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值