【本文足够食用】线程很简单的入门到精通,演化方式差不多

一、创建线程

1、通过继承Thread类,重写run()方法实现

// 通过继承创建线程类
public class Thread1 extends Thread{
    @Override
    public void run() {
        try {

            while (true){
                Thread.sleep(1000);
                System.out.println("1通过重写run方法,实现线程,线程名"+Thread.currentThread().getName()+
                        "线程id"+ currentThread().getId());
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

public class ThreadTest1 extends Thread{

    public static void main(String[] args) {
        Thread1 threadt1=new Thread1();
        Thread thread1 = new Thread(threadt1);
        thread1.start();


        Thread thread2 = new Thread() {

            @Override
            public void run() {

                try {

                    while (true){
                        Thread.sleep(1000);
                        System.out.println("2通过重写run方法,实现线程,线程名"+Thread.currentThread().getName()+
                                "线程id"+ currentThread().getId());
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }


            }
        };

        thread2.start();
    public ThreadTest1() {
        super();
    }


}

2、通过实现Runnable接口创建线程

public class ThreadTest1{

    public static void main(String[] args) {
        Thread thread3 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {

                    while (true){
                        Thread.sleep(1000);
                        System.out.println("3通过重写run方法,实现线程,线程名"+Thread.currentThread().getName()+
                                "线程id"+ currentThread().getId());
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        thread3.start();
    }
}

二、创建线程池

1、(1)创建固定的线程池,需要指定线程数;(2)利用缓存流线程池,创建可变的线程池。


import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ThreadPoolTest1 {
    public static void main(String[] args) {
        // 创建固定的线程池
        // ExecutorService executorService = Executors.newFixedThreadPool(3);
        // 创建缓存流线程池,不需要指定
        ExecutorService executorService = Executors.newCachedThreadPool();
        for(int i=1;i<=10;i++){
            final int task = i;
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    for(int j=1;j<=10;j++){
                        try {
                            Thread.sleep(10);
                            System.out.println("线程的任务 = " + Thread.currentThread().getName()+" is looping of "+j+" is task of "+task);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            });
        }
        System.out.println("all of 10 tasks is have committed");
        // executorService.shutdown();
        boolean executorServiceShutdown = executorService.isShutdown();
        System.out.println("executorServiceShutdown = " + executorServiceShutdown);
    }
}

2、用线程池启动定时器

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ThreadPoolTest1 {
    public static void main(String[] args) {
    // 创建有三个线程的线程池,同时指定关联的进程任务,在6秒后开始运行
    Executors.newScheduledThreadPool(3).schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("bombing!");
            }
        },6,TimeUnit.SECONDS);// 指定时间格式TimeUnit.格式


    // 创建有三个线程的线程池,同时指定关联的进程任务,在6秒后开始运行,并且设置固定每两秒运行一次
    Executors.newScheduledThreadPool(3).scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println("bombing!");
            }
        },6,2, TimeUnit.SECONDS);

    }
}
  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程的一拳超人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值