问题来源

  发现学习很多技术都提到了线程池的技术,自己的线程池方面没有仔细研究过,现在看了点东西来这里总结下,最近发现写博客是一个很好的锻炼自己并且将学到的东西更加理解的一个方式。

问题探究

  java的多线程技术应用很广,但凡是请求大的应用都会用到,但是线程是一个稀缺资源不能无限的创建,即使可以创建很多个线程,线程之间的切换也是十分浪费时间的,所以java推出了线程池概念,

线程池就是将一些线程资源放进一个池子中,当有请求的时候在池子中找到一个空闲的线程进行执行,这样我们可以高效的利用线程资源,避免了线程资源的浪费。

线程池

 使用方式

  1. public static void main(String[] args) throws InterruptedException, ExecutionException {
  2. ExecutorService threadPool = Executors.newFixedThreadPool(4);
  3. for (int i = 0;i<100;i++) {
  4. //向线程池提交一个任务,交由线程池去执行
  5. //threadPool.execute(new PreRun());
  6. //这个方法也是用来向线程池提交任务的,但是它和execute()方法不同,它能够返回任务执行的结果,
  7. //去看submit()方法的 实现,会发现它实际上还是调用的execute()方法,只不过它利用了Future来获取任务执行结果
  8. Future<Object> f = (Future<Object>)threadPool.submit(new PreRun2());
  9. System.out.println(f.get());
  10. }
  11. //当等待超过设定时间时,会监测ExecutorService是否已经关闭,若关闭则返回true,否则返回false。
  12. //一般情况下会和shutdown方法组合使用
  13. threadPool.awaitTermination(1200, TimeUnit.MILLISECONDS);
  14. //将停止接受新的任务, 同时等待已经提交的任务完成, 包括尚未完成的任务
  15. threadPool.shutdown();
  16. //会启动一个强制的关闭过程, 尝试取消所有运行中的任务和排在队列中尚未开始的任务,并把排队中尚未开始的任务返回,
  17. //对于关闭后提交到ExecutorService中的任务, 会被(拒接执行处理器)rejected execution handler处理,
  18. //它会抛弃任务,或者使得execute方法抛出一个未检查的RejectedExecutionException
  19. threadPool.shutdownNow();
  20. }

runable类:

  1. static class PreRun2 implements Callable<Object>{
  2. private static int count;
  3. private final int id = count++;
  4. @Override
  5. public Object call() throws Exception {
  6. ThreadDemoUtils.sleep(1000);
  7. ThreadDemoUtils.printMessage("运行中");
  8. return id;
  9. }
  10. }

可以看到线程池的创建使用了

  1. Executors.newFixedThreadPool(4);

我们来看下Executors,这个类主要是通过各中配置创建需要的线程池。

先来看看java线程池的类图关系

  可以看到最顶层的接口是Executor 只提供了一个void execute(Runnable command),这个方法是执行的意思,传进去一个参数然后执行,之后是ExecutorService接口,包含一些改变和观测线程池状态的方法,例如shutdownNow(),isShutdown(),还有一些执行线程的方法,例如submit()提交一个线程并且返回一个Future对象,Future可以获取线程运行的结果。AbstractExecutorService负责实现ExecutorService接口的一些方法。ThreadPoolExecutor继承了AbstractExecutorService重写了execute方法。

  接下来我们看看ThreadPoolExecutor的一些参数

  1. public ThreadPoolExecutor(int corePoolSize,
  2. int maximumPoolSize,
  3. long keepAliveTime,
  4. TimeUnit unit,
  5. BlockingQueue<Runnable> workQueue,
  6. ThreadFactory threadFactory,
  7. RejectedExecutionHandler handler) {
  8. if (corePoolSize < 0 ||
  9. maximumPoolSize <= 0 ||
  10. maximumPoolSize < corePoolSize ||
  11. keepAliveTime < 0)
  12. throw new IllegalArgumentException();
  13. if (workQueue == null || threadFactory == null || handler == null)
  14. throw new NullPointerException();
  15. this.corePoolSize = corePoolSize;
  16. this.maximumPoolSize = maximumPoolSize;
  17. this.workQueue = workQueue;
  18. this.keepAliveTime = unit.toNanos(keepAliveTime);
  19. this.threadFactory = threadFactory;
  20. this.handler = handler;
  21. }

我们来分析下这些参数

  1. corePoolSize

核心线程池大小,这个参数标识线程池所可以容纳的最大的核心线程池数量,这个不是可以创建的最多的线程的数量,只是核心数量,我们之后会再次分析。

 maximumPoolSize

 线程池可以创建线程的最大数量。

  1. keepAliveTime

 如果核心线程数已经满了,没有空闲的线程的时候,新的任务可以等待的最长时间。

  1. workQueue

当核心线程数量满了,没有空闲线程时,新任务放在这个指定的任务队列中。

  1. threadFactory
     
    创建线程的工厂类。
  1. handler

当无法创建更多的线程时拒绝任务的处理策略

我们看到上面使用线程池的例子中,通过

  1. submit方法提交一个任务,然后得到一个Future对象,这个方法到底是怎么执行的呢,Future到底是什么?我们一起来看下
  1. public Future<?> submit(Runnable task) {
  2. if (task == null) throw new NullPointerException();
  3. RunnableFuture<Void> ftask = newTaskFor(task, null);
  4. execute(ftask);
  5. return ftask;
  6. }

这个方法会先创建一个RunableFuture对象,就是将我们传进来的Runable封装成一个统一的任务对象

  

  1. protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
  2. return new FutureTask<T>(runnable, value);
  3. }
  4.  
  5. public FutureTask(Runnable runnable, V result) {
  6. this.callable = Executors.callable(runnable, result);
  7. this.state = NEW; // ensure visibility of callable
  8. }

看到会创建一个FutureTask对象,这个对象主要包含Callable和result ,如果传进来的是Runable,Executors.callable()方法会将runable转换为Callable对象(通过适配器模式)

也就是说不管传进来的是Runable还是Callable,他们都会将这个对象转换为FutreTask对象。

我们继续向下看

  1. execute(ftask);
  1. public void execute(Runnable command) {
  2. if (command == null)
  3. throw new NullPointerException();
  4. /*
  5. * Proceed in 3 steps:
  6. *
  7. * 1. If fewer than corePoolSize threads are running, try to
  8. * start a new thread with the given command as its first
  9. * task. The call to addWorker atomically checks runState and
  10. * workerCount, and so prevents false alarms that would add
  11. * threads when it shouldn't, by returning false.
  12. *
  13. * 2. If a task can be successfully queued, then we still need
  14. * to double-check whether we should have added a thread
  15. * (because existing ones died since last checking) or that
  16. * the pool shut down since entry into this method. So we
  17. * recheck state and if necessary roll back the enqueuing if
  18. * stopped, or start a new thread if there are none.
  19. *
  20. * 3. If we cannot queue task, then we try to add a new
  21. * thread. If it fails, we know we are shut down or saturated
  22. * and so reject the task.
  23. */
  24. int c = ctl.get();
  25. if (workerCountOf(c) < corePoolSize) {
  26. if (addWorker(command, true))
  27. return;
  28. c = ctl.get();
  29. }
  30. if (isRunning(c) && workQueue.offer(command)) {
  31. int recheck = ctl.get();
  32. if (! isRunning(recheck) && remove(command))
  33. reject(command);
  34. else if (workerCountOf(recheck) == 0)
  35. addWorker(null, false);
  36. }
  37. else if (!addWorker(command, false))
  38. reject(command);
  39. }

我们先来翻译一下这个注释

  

  1. /*
  2. * Proceed in 3 steps:
  3. *
  4. * 1. If fewer than corePoolSize threads are running, try to
  5. * start a new thread with the given command as its first
  6. * task. The call to addWorker atomically checks runState and
  7. * workerCount, and so prevents false alarms that would add
  8. * threads when it shouldn't, by returning false.
  9. *
  10. * 2. If a task can be successfully queued, then we still need
  11. * to double-check whether we should have added a thread
  12. * (because existing ones died since last checking) or that
  13. * the pool shut down since entry into this method. So we
  14. * recheck state and if necessary roll back the enqueuing if
  15. * stopped, or start a new thread if there are none.
  16. *
  17. * 3. If we cannot queue task, then we try to add a new
  18. * thread. If it fails, we know we are shut down or saturated
  19. * and so reject the task.
      有3个步骤:
        1.如果当前创建的线程数量小于核心线程数量,就为传进来的任务创建一个新的线程,
         这个过程会调用addWorker方法原子的检查运行状态和工作线程来保证线程确实应该创建
         如果不应该创建就返回false,继续下一个步骤
        2. 如果任务成功入队队列,任然需要检查我们应该添加一个线程或者这个线程池是不是已经挂了
        3.如果我们不能入队,我们就尝试创建一个新的线程,如果创建失败,我们就会开启一个新的线程,
          如果失败就会启动拒绝策略处理
  20.  
  21. */
  22.  

我们一个个方法来看,首先我们了解下线程池需要用到的所有状态

  

  1. /**
  2. * The main pool control state, ctl, is an atomic integer packing
  3. * two conceptual fields
  4. * workerCount, indicating the effective number of threads
  5. * runState, indicating whether running, shutting down etc
  6. *
  7. * In order to pack them into one int, we limit workerCount to
  8. * (2^29)-1 (about 500 million) threads rather than (2^31)-1 (2
  9. * billion) otherwise representable. If this is ever an issue in
  10. * the future, the variable can be changed to be an AtomicLong,
  11. * and the shift/mask constants below adjusted. But until the need
  12. * arises, this code is a bit faster and simpler using an int.
  13. *
  14. * The workerCount is the number of workers that have been
  15. * permitted to start and not permitted to stop. The value may be
  16. * transiently different from the actual number of live threads,
  17. * for example when a ThreadFactory fails to create a thread when
  18. * asked, and when exiting threads are still performing
  19. * bookkeeping before terminating. The user-visible pool size is
  20. * reported as the current size of the workers set.
  21. *
  22. * The runState provides the main lifecycle control, taking on values:
  23. *
  24. * RUNNING: Accept new tasks and process queued tasks
  25. * SHUTDOWN: Don't accept new tasks, but process queued tasks
  26. * STOP: Don't accept new tasks, don't process queued tasks,
  27. * and interrupt in-progress tasks
  28. * TIDYING: All tasks have terminated, workerCount is zero,
  29. * the thread transitioning to state TIDYING
  30. * will run the terminated() hook method
  31. * TERMINATED: terminated() has completed
  32. *
  33. * The numerical order among these values matters, to allow
  34. * ordered comparisons. The runState monotonically increases over
  35. * time, but need not hit each state. The transitions are:
  36. *
  37. * RUNNING -> SHUTDOWN
  38. * On invocation of shutdown(), perhaps implicitly in finalize()
  39. * (RUNNING or SHUTDOWN) -> STOP
  40. * On invocation of shutdownNow()
  41. * SHUTDOWN -> TIDYING
  42. * When both queue and pool are empty
  43. * STOP -> TIDYING
  44. * When pool is empty
  45. * TIDYING -> TERMINATED
  46. * When the terminated() hook method has completed
  47. *
  48. * Threads waiting in awaitTermination() will return when the
  49. * state reaches TERMINATED.
  50. *
  51. * Detecting the transition from SHUTDOWN to TIDYING is less
  52. * straightforward than you'd like because the queue may become
  53. * empty after non-empty and vice versa during SHUTDOWN state, but
  54. * we can only terminate if, after seeing that it is empty, we see
  55. * that workerCount is 0 (which sometimes entails a recheck -- see
  56. * below).
  57. */
  1. private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
  2. private static final int COUNT_BITS = Integer.SIZE - 3;
  3. private static final int CAPACITY = (1 << COUNT_BITS) - 1;
  4.  
  5. // runState is stored in the high-order bits
  6. private static final int RUNNING = -1 << COUNT_BITS;// 111 00000000000000000000000000000
  7. private static final int SHUTDOWN = 0 << COUNT_BITS;// 000 00000000000000000000000000000
  8. private static final int STOP = 1 << COUNT_BITS;// 001 00000000000000000000000000000
  9. private static final int TIDYING = 2 << COUNT_BITS;// 010 00000000000000000000000000000
  10. private static final int TERMINATED = 3 << COUNT_BITS;// 100 00000000000000000000000000000
  11.  
  12. // Packing and unpacking ctl
  13. private static int runStateOf(int c) { return c & ~CAPACITY; }//最高3位
  14. private static int workerCountOf(int c) { return c & CAPACITY; }//后29位
  15. private static int ctlOf(int rs, int wc) { return rs | wc; }

以下是线程池状态的讲解

  1. RUNNING: Accept new tasks and process queued tasks 运行时状态,可以接收新的任务
  2. * SHUTDOWN: Don't accept new tasks, but process queued tasks 关闭状态,不在接收新的任务,会运行队列中的任务
  3. * STOP: Don't accept new tasks, don't process queued tasks, 立即关闭状态,不在接收新的任务,也不会执行队列中的任务,并且会中断运行中的任务
  4. * and interrupt in-progress tasks
  5. * TIDYING: All tasks have terminated, workerCount is zero, 整理状态,所有的任务都结束了,工作任务为0,当STOP完成时。
  6. * the thread transitioning to state TIDYING
  7. * will run the terminated() hook method
  8. * TERMINATED: terminated() has completed 线程池完全结束

下面我们来看下addWorker()

  1. private boolean addWorker(Runnable firstTask, boolean core) {
  2. retry:
  3. for (;;) {
  4. int c = ctl.get();
  5. int rs = runStateOf(c);
  6.  
  7. // Check if queue empty only if necessary.
  8. if (rs >= SHUTDOWN &&
  9. ! (rs == SHUTDOWN &&
  10. firstTask == null &&
  11. ! workQueue.isEmpty()))
  12. return false;
  13.  
  14. for (;;) {
  15. int wc = workerCountOf(c);
  16. if (wc >= CAPACITY ||
  17. wc >= (core ? corePoolSize : maximumPoolSize))
  18. return false;
  19. if (compareAndIncrementWorkerCount(c))
  20. break retry;
  21. c = ctl.get(); // Re-read ctl
  22. if (runStateOf(c) != rs)
  23. continue retry;
  24. // else CAS failed due to workerCount change; retry inner loop
  25. }
  26. }
  27.  
  28. boolean workerStarted = false;
  29. boolean workerAdded = false;
  30. Worker w = null;
  31. try {
  32. w = new Worker(firstTask);
  33. final Thread t = w.thread;
  34. if (t != null) {
  35. final ReentrantLock mainLock = this.mainLock;
  36. mainLock.lock();
  37. try {
  38. // Recheck while holding lock.
  39. // Back out on ThreadFactory failure or if
  40. // shut down before lock acquired.
  41. int rs = runStateOf(ctl.get());
  42.  
  43. if (rs < SHUTDOWN ||
  44. (rs == SHUTDOWN && firstTask == null)) {
  45. if (t.isAlive()) // precheck that t is startable
  46. throw new IllegalThreadStateException();
  47. workers.add(w);
  48. int s = workers.size();
  49. if (s > largestPoolSize)
  50. largestPoolSize = s;
  51. workerAdded = true;
  52. }
  53. } finally {
  54. mainLock.unlock();
  55. }
  56. if (workerAdded) {
  57. t.start();
  58. workerStarted = true;
  59. }
  60. }
  61. } finally {
  62. if (! workerStarted)
  63. addWorkerFailed(w);
  64. }
  65. return workerStarted;
  66. }

这个方法整体分为两大部分,上面一部分是要将判断是不是可以添加一个线程,并且通过

compareAndIncrementWorkerCount(c)

来原子性的增加工作线程数量。下面一部分就是创建一个Worker工作线程,然后加锁将这个新建的worker放进workers中,并且启动这个工作线程,接下来我们看下Worker类

  1. private final class Worker
  2. extends AbstractQueuedSynchronizer
  3. implements Runnable
  4. {
  5. /**
  6. * This class will never be serialized, but we provide a
  7. * serialVersionUID to suppress a javac warning.
  8. */
  9. private static final long serialVersionUID = 6138294804551838833L;
  10.  
  11. /** Thread this worker is running in. Null if factory fails. */
  12. final Thread thread;
  13. /** Initial task to run. Possibly null. */
  14. Runnable firstTask;
  15. /** Per-thread task counter */
  16. volatile long completedTasks;
  17.  
  18. /**
  19. * Creates with given first task and thread from ThreadFactory.
  20. * @param firstTask the first task (null if none)
  21. */
  22. Worker(Runnable firstTask) {
  23. setState(-1); // inhibit interrupts until runWorker
  24. this.firstTask = firstTask;
  25. this.thread = getThreadFactory().newThread(this);
  26. }
  27.  
  28. /** Delegates main run loop to outer runWorker */
  29. public void run() {
  30. runWorker(this);
  31. }

这个Worker实现了Runable接口,并持有一个Thread对象,当调用构造函数时这个Worker会创建一个线程并把Worker作为参数传进去

当该线程调用start方法时,就会调用系统api开辟一个新的线程,然后执行run方法

  1. final void runWorker(Worker w) {
  2. Thread wt = Thread.currentThread();
  3. Runnable task = w.firstTask;
  4. w.firstTask = null;
  5. w.unlock(); // allow interrupts
  6. boolean completedAbruptly = true;
  7. try {
  8. while (task != null || (task = getTask()) != null) {
  9. w.lock();
  10. // If pool is stopping, ensure thread is interrupted;
  11. // if not, ensure thread is not interrupted. This
  12. // requires a recheck in second case to deal with
  13. // shutdownNow race while clearing interrupt
  14. if ((runStateAtLeast(ctl.get(), STOP) ||
  15. (Thread.interrupted() &&
  16. runStateAtLeast(ctl.get(), STOP))) &&
  17. !wt.isInterrupted())
  18. wt.interrupt();
  19. try {
  20. beforeExecute(wt, task);
  21. Throwable thrown = null;
  22. try {
  23. task.run();
  24. } catch (RuntimeException x) {
  25. thrown = x; throw x;
  26. } catch (Error x) {
  27. thrown = x; throw x;
  28. } catch (Throwable x) {
  29. thrown = x; throw new Error(x);
  30. } finally {
  31. afterExecute(task, thrown);
  32. }
  33. } finally {
  34. task = null;
  35. w.completedTasks++;
  36. w.unlock();
  37. }
  38. }
  39. completedAbruptly = false;
  40. } finally {
  41. processWorkerExit(w, completedAbruptly);
  42. }
  43. }

看到runworker方法,他会不断的得到task,如果taks是null,就结束这个工作线程

  1. processWorkerExit(w, completedAbruptly)
  1. private void processWorkerExit(Worker w, boolean completedAbruptly) {
        
  2. if (completedAbruptly) // If abrupt, then workerCount wasn't adjusted
  3. decrementWorkerCount();
  4.  
  5. final ReentrantLock mainLock = this.mainLock;
  6. mainLock.lock();
  7. try {
  8. completedTaskCount += w.completedTasks;
  9. workers.remove(w);
  10. } finally {
  11. mainLock.unlock();
  12. }
  13.  
  14. tryTerminate();
  15.  
  16. int c = ctl.get();
  17. if (runStateLessThan(c, STOP)) {
  18. if (!completedAbruptly) {
  19. int min = allowCoreThreadTimeOut ? 0 : corePoolSize;
  20. if (min == 0 && ! workQueue.isEmpty())
  21. min = 1;
  22. if (workerCountOf(c) >= min)
  23. return; // replacement not needed
  24. }
  25. addWorker(null, false);
  26. }
  27. }

我们看到第一行,如果工作线程突然退出,就减少工作线程数量,然后将线程从workers中移除出去,然后开始尝试着结束线程池。

现在回到addWorker方法,这个方法是比较重要的方法,他负责创建并启动新的线程。能够保证线程数量不会超过限制。

继续看下execute方法

  1. public void execute(Runnable command) {
  2. if (command == null)
  3. throw new NullPointerException();
  4. int c = ctl.get();
  5. if (workerCountOf(c) < corePoolSize) {
  6. if (addWorker(command, true))
  7. return;
  8. c = ctl.get();
  9. }
  10. if (isRunning(c) && workQueue.offer(command)) {
  11. int recheck = ctl.get();
  12. if (! isRunning(recheck) && remove(command))
  13. reject(command);
  14. else if (workerCountOf(recheck) == 0)
  15. addWorker(null, false);
  16. }
  17. else if (!addWorker(command, false))
  18. reject(command);
  19. }

我们看到,如果线程数量小于核心线程数量,就继续创建线程,并将当前任务放置到创建的线程中运行,如果返回false说明线程池挂了或者线程数量超过了限制。

接下来判断线程池是否运行中,如果线程池运行中就执行入队列操作,将任务放在队列中,等待空闲的线程来执行该任务。我们接下来就看看队列的事

我们看到BlockingQueue有很多的实现,不同的实现对应不同的业务场景,我们来看下用的最多的LinkedBlockingQueue队列。

  1. public boolean offer(E e) {
  2. if (e == null) throw new NullPointerException();
  3. final AtomicInteger count = this.count;
  4. if (count.get() == capacity)
  5. return false;
  6. int c = -1;
  7. Node<E> node = new Node<E>(e);
  8. final ReentrantLock putLock = this.putLock;
  9. putLock.lock();
  10. try {
  11. if (count.get() < capacity) {
  12. enqueue(node);
  13. c = count.getAndIncrement();
  14. if (c + 1 < capacity)
  15. notFull.signal();
  16. }
  17. } finally {
  18. putLock.unlock();
  19. }
  20. if (c == 0)
  21. signalNotEmpty();
  22. return c >= 0;
  23. }

看下这个入队方法,如果队列的数量等于最大限制数量就返回false,入队失败,不然就加上一个入队的锁,然后再次判断是不是超过了最大限制数量,

如果没有超过就执行enqueue方法执行真正的入队方法。

  1. private void enqueue(Node<E> node) {
  2. // assert putLock.isHeldByCurrentThread();
  3. // assert last.next == null;
  4. last = last.next = node;
  5. }

其实就是将节点追加到尾结点。

执行完入队操作后再次检查是否等于最大限制量,如果不等于就让非满条件进行通知。最后在finally中释放锁,方法最后检查是否入队成功并且只队列中只有一个,然后让非空条件进行通知。出队列也是一样的分析过程。

继续来看,入队之后会再次检查线程池的状态,是否可以继续运行,还会检查workerCountOf(c)==0,这个的目的是有可能在入队的时候其他的线程退出了导致没有线程可以使用,这个时候就要加入一个备用线程让进入队列的线程的运行得到保障。

最后判断addWorker(task,false)是否成功,如果成功就退出方法,如果失败就执行拒绝策略。

java线程池的初探的更多相关文章

  1. Java线程池详解

    一.线程池初探 所谓线程池,就是将多个线程放在一个池子里面(所谓池化技术),然后需要线程的时候不是创建一个线程,而是从线程池里面获取一个可用的线程,然后执行我们的任务.线程池的关键在于它为我们管理了多 ...

  2. Java线程池详解(一)

    一.线程池初探 所谓线程池,就是将多个线程放在一个池子里面(所谓池化技术),然后需要线程的时候不是创建一个线程,而是从线程池里面获取一个可用的线程,然后执行我们的任务.线程池的关键在于它为我们管理了多 ...

  3. Java 线程池框架核心代码分析--转

    原文地址:http://www.codeceo.com/article/java-thread-pool-kernal.html 前言 多线程编程中,为每个任务分配一个线程是不现实的,线程创建的开销和 ...

  4. Java线程池使用说明

    Java线程池使用说明 转自:http://blog.csdn.net/sd0902/article/details/8395677 一简介 线程的使用在java中占有极其重要的地位,在jdk1.4极 ...

  5. (转载)JAVA线程池管理

    平时的开发中线程是个少不了的东西,比如tomcat里的servlet就是线程,没有线程我们如何提供多用户访问呢?不过很多刚开始接触线程的开发攻城师却在这个上面吃了不少苦头.怎么做一套简便的线程开发模式 ...

  6. Java线程池的那些事

    熟悉java多线程的朋友一定十分了解java的线程池,jdk中的核心实现类为java.util.concurrent.ThreadPoolExecutor.大家可能了解到它的原理,甚至看过它的源码:但 ...

  7. 四种Java线程池用法解析

    本文为大家分析四种Java线程池用法,供大家参考,具体内容如下 http://www.jb51.net/article/81843.htm 1.new Thread的弊端 执行一个异步任务你还只是如下 ...

  8. Java线程池的几种实现 及 常见问题讲解

    工作中,经常会涉及到线程.比如有些任务,经常会交与线程去异步执行.抑或服务端程序为每个请求单独建立一个线程处理任务.线程之外的,比如我们用的数据库连接.这些创建销毁或者打开关闭的操作,非常影响系统性能 ...

  9. Java线程池应用

    Executors工具类用于创建Java线程池和定时器. newFixedThreadPool:创建一个可重用固定线程数的线程池,以共享的无界队列方式来运行这些线程.在任意点,在大多数 nThread ...

随机推荐

  1. React Router学习

    React Router教程 本教程引用马伦老师的的教程 React项目的可用的路由库是React-Router,当然这也是官方支持的.它也分为: react-router 核心组件 react-ro ...

  2. 《LINUX内核设计与实现》第三周读书笔记——第一二章

    <Linux内核设计与实现>读书笔记--第一二章 20135301张忻 估算学习时间:共2小时 读书:1.5 代码:0 作业:0 博客:0.5 实际学习时间:共2.5小时 读书:2.0 代 ...

  3. 关于cocos2dx 关键字的问题

    今天码代码,在创建新场景的时候,.h文件里  class Game : public cocos2d::Layer没有问题,在Game类里面,声明了它的成员之后,开始在.cpp文件里面实现这个类,到重 ...

  4. BaseServlet 继承 httpServlet

    BaseServlet   核心 package cn.core; import java.io.IOException; import java.lang.reflect.Method; impor ...

  5. 数学口袋精灵app(小学生四则运算app)开发需求

    数学口袋精灵APP,摒除了传统乏味无趣学习数学四则运算的模式,采用游戏的形式,让小朋友在游戏中学习,培养了小朋友对数学的兴趣,让小朋友在游戏中运算能力得到充分提升.快乐学习,成长没烦恼! 项目名字:“ ...

  6. Supervised Hashing with Kernels, KSH

    Notation 该论文中应用到较多符号,为避免混淆,在此进行解释: n:原始数据集的大小 l:实验中用于监督学习的数据集大小(矩阵S行/列的大小) m:辅助数据集,用于得到基于核的哈希函数 r:比特 ...

  7. JDBC学习笔记——PreparedStatement的使用

    PreparedStatement public interface PreparedStatement extends Statement;可以看到PreparedStatement是Stateme ...

  8. Android-TabLayout设置内容宽度以及下划线宽度

    默认图: 效果图: 项目中使用到需要像今日头条那种实现顶部横向滑动标题功能,本人项目中使用TabLayout+ViewPager实现,但是,实现后默认的TabLayout间距特别大,并且下划线,文字大 ...

  9. Alpha 冲刺二

    团队成员 051601135 岳冠宇 051604103 陈思孝 031602629 刘意晗 031602248 郑智文 031602234 王淇 会议照片 项目燃尽图 项目进展 暂无进展, 项目描述 ...

  10. 实体框架自定义代码优先约定(EF6以后)

    仅限EF6仅向前 - 此页面中讨论的功能,API等在实体框架6中引入.如果您使用的是早期版本,则部分或全部信息不适用. 使用Code First时,您的模型是使用一组约定从您的类计算的.默认的Code ...