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()+"\t"+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()+"\t"+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()+"\t"+j);
                    }

                }
            });
        }
        ex.shutdown();



    }
}

 

posted @ 2021-01-22 15:29  勤奋的园  阅读(242)  评论(0编辑  收藏  举报