使用Executor创建线程的几种方式

方式一:newCachedThreadPool

package com.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Run {
    public static void main(String[] args) throws InterruptedException {
        Run run = new Run();
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 5; i++) {
            executorService.execute(run.new MyRunnable(" "+(i+1)));
        }
        Thread.sleep(3000);
        System.out.println();
        System.out.println();
        for (int i = 0; i < 5; i++) {
            executorService.execute(run.new MyRunnable(" "+(i+1)));
        }
    }
    
    
    public class MyRunnable implements Runnable{
        private String username;
        
        public MyRunnable(String username) {
            this.username = username;
        }

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName()+" username="+username+" begin "+System.currentTimeMillis());
            System.out.println(Thread.currentThread().getName()+" username="+username+" end   "+System.currentTimeMillis());
        }
        
    }
}

方式二:newFixedThreadPool

/*
 * 此方法创建的是有界线程池,也就是池中的线程的个数可以指定最大值
 * */
package com.Executors;


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

public class RunNewFixedThreadPool {
    public static void main(String[] args) {
    	RunNewFixedThreadPool run = new RunNewFixedThreadPool();
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        for (int i = 0; i < 5; i++) {
            executorService.execute(run.new MyRunnable(" "+(i+1)));
        }
    }
    
    public class MyRunnable implements Runnable{
        private String username;
        
        public MyRunnable(String username) {
            this.username = username;
        }


        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName()+" username="+username+" begin "+System.currentTimeMillis());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+" username="+username+" end   "+System.currentTimeMillis());
        }
        
    }
}

方式三:newSingleThreadExecutor

/*
 * 此方法可以创建单一线程池,线程池里只有一个线程,单一线程池可以实现以队列的方式来执行任务
 * */
package com.Executors;


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

public class RunNewSingleThreadExecutor {
    public static void main(String[] args) {
    	RunNewSingleThreadExecutor run = new RunNewSingleThreadExecutor();
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 5; i++) {
            executorService.execute(run.new MyRunnable(" "+(i+1)));
        }
    }
    
    
    public class MyRunnable implements Runnable{
        private String username;
        
        public MyRunnable(String username) {
            this.username = username;
        }


        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName()+" username="+username+" begin "+System.currentTimeMillis());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+" username="+username+" end   "+System.currentTimeMillis());
        }
        
    }
}

 

 

 

 

你可能感兴趣的:(多线程)