1. newFixedThreadPool(int nThreads)

创建一个固定长度的线程池,每当提交一个任务就创建一个线程,直到达到线程池的最大数量,这时线程规模将不再变化,当线程发生未预期的错误而结束时,线程池会补充一个新的线程。

2. newCachedThreadPool()

创建一个可缓存的线程池,如果线程池的规模超过了处理需求,将自动回收空闲线程,而当需求增加时,则可以自动添加新线程,线程池的规模不存在任何限制。

3. newSingleThreadExecutor()

这是一个单线程的Executor,它创建单个工作线程来执行任务,如果这个线程异常结束,会创建一个新的来替代它;它的特点是能确保依照任务在队列中的顺序来串行执行。

4. newScheduledThreadPool(int corePoolSize)

创建了一个固定长度的线程池,而且以延迟或定时的方式来执行任务,类似于Timer。


1. newFixedThreadPool(int nThreads)

import java.util.concurrent.*;

ExecutorService fixed= Executors.newFixedThreadPool(3);
fixed.execute(new bb());
static class bb implements Runnable{}

2.newCachedThreadPool()

ExecutorService Cached= Executors.newCachedThreadPool();
Cached.execute(new bb());
static class bb implements Runnable{}

3.newSingleThreadExecutor()

ExecutorService Single= Executors.newSingleThreadExecutor();
Single.execute(new bb());
static class bb implements Runnable{}

现行大多数GUI程序都是单线程的。单线程可用于数据库操作,文件操作,应用批量安装,应用批量删除等不适合并发但可能IO阻塞性及影响UI线程响应的操作。

4. newScheduledThreadPool(int corePoolSize)

ScheduledExecutorService Scheduled= Executors.newScheduledThreadPool(1);
//延迟执行 (1是延迟时长,TimeUnit.SECONDS时长类型)
Scheduled.schedule(new Runnable() {
    @Override
    public void run() {
      System.out.println("我延迟一秒执行");
    }
},1,TimeUnit.SECONDS);
//周期执行(延迟2秒执行每3秒执行一次)
Scheduled.scheduleAtFixedRate(new Runnable() {
    @Override
    public void run() {
        System.out.println(new Date().toString());
    }
},2,3,TimeUnit.SECONDS);
//周期任务完成后在延迟3秒再执行一个周期(比如12点启动,延迟1秒启动+3秒执行任务30分钟,30分钟执行完成后,延迟3秒再进行下一次的任务)
Scheduled.scheduleWithFixedDelay(new Runnable() {
    @Override
    public void run() {
        //30min
        try {
            Thread.sleep(60000 * 30);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("" + new Date() +"重复执行2");
    }
},1, 3, TimeUnit.SECONDS);

execute和submit 

1.execute() 参数 Runnable ;submit() 参数 (Runnable) 或 (Runnable 和 结果 T) 或 (Callable)

2.execute() 没有返回值;而 submit() 有返回值

3.submit() 的返回值 Future 调用get方法时,可以捕获处理异常

sleep()和wait()


1.这两个方法来自不同的类分别是Thread和Object  

2.最主要是sleep方法没有释放锁,而wait方法释放了锁,使得其他线程可以使用同步控制块或者方法(锁代码块和方法锁)。

3.wait,notify和notifyAll只能在同步控制方法或者同步控制块里面使用,而sleep可以在任何地方使用(使用范围)  

4.sleep必须捕获异常,而wait,notify和notifyAll不需要捕获异常  


public class test3 {
    public static void main(String[] args) {
        Thread cc=new Thread(new aa());
        cc.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Thread dd=new Thread(new bb());
        dd.start();
    }
    static class aa implements Runnable{
        @Override
        public void run() {
            synchronized (test3.class) {
                System.out.println("运行1");
                try {
                    test3.class.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("运行2");
            }
        }
    }
    static class bb implements Runnable{
        @Override
        public void run() {
            synchronized (test3.class) {
                test3.class.notify();
            }
        }
    }
}

notify和notifyAll

notify和notifyAll之间的关键区别在于notify()只会唤醒一个线程,而notifyAll方法将唤醒所有线程。