• Java创建线程四种方式


    1、继承Thread类

    public class MyThread extends Thread {
        public MyThread() {
            
        }
        public void run() {
            for(int i=0;i<10;i++) {
                System.out.println(Thread.currentThread()+":"+i);
            }
        }
        public static void main(String[] args) {
            MyThread mThread1=new MyThread();
            MyThread mThread2=new MyThread();
            MyThread myThread3=new MyThread();
            mThread1.start();
            mThread2.start();
            myThread3.start();
        }
    }

    2、实现Runnable接口

    public class MyThread implements Runnable{
        public static int count=20;
        public void run() {
            while(count>0) {
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"-当前剩余票数:"+count--);
            }
        }
        public static void main(String[] args) {
            MyThread Thread1=new MyThread();
            Thread mThread1=new Thread(Thread1,"线程1");
            Thread mThread2=new Thread(Thread1,"线程2");
            Thread mThread3=new Thread(Thread1,"线程3");
            mThread1.start();
            mThread2.start();
            myThread3.start();
        }
    }

    3、实现Callable接口

    package com.yanshu;
    
    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.FutureTask;
    
    /**
     * @author :yangyuanyuan
     * @description:TODO
     * @date :2021/1/22 14:05
     */
    
    class  Mythread implements Callable<Integer>{
    
        @Override
        public Integer call() throws Exception {
            System.out.println("COME IN");
    
            return 1024;
        }
    }
    public class CallableDemo {
        public static void main(String[] args) throws ExecutionException, InterruptedException {
            FutureTask<Integer> futureTask = new FutureTask(new Mythread());
            new Thread(futureTask,"A").start();
            Integer integer = futureTask.get();
            System.out.println(integer);
    
        }
    }

    4、使用线程池

    package com.yanshu;
    
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    /**
     * @author :yangyuanyuan
     * @description:TODO
     * @date :2021/1/22 14:38
     */
    public class ThreadPool {
        public static void main(String[] args) {
    
            /*
            *FixThreadPool(int n); 固定大小的线程池
            *  使用于为了满足资源管理需求而需要限制当前线程数量的场合。使用于负载比较重的服务器。
            * pool-1-thread-1    0
              pool-1-thread-1    1
              pool-1-thread-2    0
              pool-1-thread-2    1
              pool-1-thread-3    0
              pool-1-thread-3    1
              pool-1-thread-4    0
              pool-1-thread-4    1
              pool-1-thread-5    0
              pool-1-thread-5    1
            *
            * */
    //        ExecutorService ex=Executors.newFixedThreadPool(5);
    //        for(int i=0;i<5;i++) {
    //            ex.submit(new Runnable() {
    //
    //                @Override
    //                public void run() {
    //                    for(int j=0;j<2;j++) {
    //                        System.out.println(Thread.currentThread().getName()+"	"+j);
    //                    }
    //
    //                }
    //            });
    //        }
    //        ex.shutdown();
    
    
            /*
            SingleThreadPoolExecutor :单线程池
            需要保证顺序执行各个任务的场景
             *
             * pool-1-thread-1    0
               pool-1-thread-1    1
               pool-1-thread-1    0
               pool-1-thread-1    1
               pool-1-thread-1    0
               pool-1-thread-1    1
               pool-1-thread-1    0
               pool-1-thread-1    1
               pool-1-thread-1    0
               pool-1-thread-1    1
             * /
            ExecutorService ex=Executors.newSingleThreadExecutor();
    
            for(int i=0;i<5;i++) {
                ex.submit(new Runnable() {
    
                    @Override
                    public void run() {
                        for(int j=0;j<2;j++) {
                            System.out.println(Thread.currentThread().getName()+"	"+j);
                        }
    
                    }
                });
            }
            ex.shutdown();
    
    
            /*
            * CashedThreadPool(); 缓存线程池
            当提交任务速度高于线程池中任务处理速度时,缓存线程池会不断的创建线程
            适用于提交短期的异步小程序,以及负载较轻的服务器
            *
            * pool-1-thread-1    0
              pool-1-thread-1    1
              pool-1-thread-2    0
              pool-1-thread-2    1
              pool-1-thread-1    0
              pool-1-thread-1    1
              pool-1-thread-1    0
              pool-1-thread-1    1
              pool-1-thread-3    0
              pool-1-thread-3    1
            * */
            ExecutorService ex=Executors.newCachedThreadPool();
    
            for(int i=0;i<5;i++) {
                ex.submit(new Runnable() {
    
                    @Override
                    public void run() {
                        for(int j=0;j<2;j++) {
                            System.out.println(Thread.currentThread().getName()+"	"+j);
                        }
    
                    }
                });
            }
            ex.shutdown();
    
    
    
        }
    }
  • 相关阅读:
    PHP常用代码大全(新手入门必备)
    一代大商孟洛川的经商之道
    Photoshop调出田园照片唯美手绘油画效果
    Photoshop调出外景婚片蓝色小清新艺术效果
    photoshop快速把新照片制作成老照片教学
    Photoshop调出清晰的阴雨天气山水风景照
    PS调出清新风格社区街拍照片
    PS调出韩式米黄色室内婚纱照片
    PS调出唯美紫蓝色天空背景女生照片
    PS快速调出天蓝色清新外景
  • 原文地址:https://www.cnblogs.com/qfdy123/p/14313600.html
Copyright © 2020-2023  润新知