线程与线程池的创建

线程

3种:thread、runnable、callable

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/**
 * @Author: FA
 * @Date: 2021/6/11 13:04
 * @Email: 
 * @Description:
 */
public class TreadTest {

    public static void main(String[] args) {
        System.out.println("===Thread==");
        for (int i = 0; i < 10; i++) {
            ByThread byThread = new ByThread();
            byThread.start();
        }
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("==============================");

        System.out.println("===runnable==");
        for (int i = 0; i < 10; i++) {
            Runnable byRunnable = new ByRunnable();
            Thread thread = new Thread(byRunnable);
            thread.start();
        }
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("==============================");

        System.out.println("===callable==");
        for (int i = 0; i < 10; i++) {
            Callable<Integer> byCallable = new ByCallable();
            FutureTask<Integer> futureTask = new FutureTask<Integer>(byCallable);
            Thread thread = new Thread(futureTask);
            thread.start();
        }
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("==============================");
    }
}
//创建线程的三种方式:thread、runnable、callable
class ByThread extends Thread{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"正在被执行");
    }
}
class ByRunnable implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"正在被执行");
    }
}

class ByCallable implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        System.out.println(Thread.currentThread().getName()+"正在被执行");
        return 1;
    }
}

线程池

五种

  • newCachedThreadPool
  • newFixedThreadPool
  • newScheduledThreadPool
  • newSingleThreadExecutor
  • newSingleThreadScheduledExecutor
public class TreadPoolTest {
    public static void main(String[] args) {
        //可缓存线程池
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(1000);
            }catch (InterruptedException e){
                e.printStackTrace();

            }
            cachedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName()+"正在被执行");
                    try {
                        Thread.sleep(1000);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }

                }
            });
        }
        //可重用固定个数的线程池
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
        for (int i = 0; i < 10; i++) {
            fixedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName()+"正在被执行");
                    try {
                        Thread.sleep(2000);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }

                }
            });
        }
        //支持周期性任务执行的线程池
        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(3);
        scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"正在被执行");
            }
        },1,3, TimeUnit.SECONDS);


        //newSingleThreadExecutor 和上面差不多只不过限制了一个线程
        ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 10; i++) {
            final int index = i;
            singleThreadExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println(Thread.currentThread().getName()+"正在被执行,打印的值是:"+index);
                        Thread.sleep(1000);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
            });
        }
        //newSingleThreadScheduledExecutor  同上 单线程,延迟10秒,间隔4秒执行一次
        ScheduledExecutorService singleThreadScheduledExecutor = Executors.newSingleThreadScheduledExecutor();
        singleThreadScheduledExecutor.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+"正在被执行");
            }
        },10,4, TimeUnit.SECONDS);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值