亚洲精品中文字幕无乱码_久久亚洲精品无码AV大片_最新国产免费Av网址_国产精品3级片

java語言

java中的線程池實例代碼

時間:2023-08-27 01:25:32 志升 java語言 我要投稿
  • 相關(guān)推薦

java中通用的線程池實例代碼

  在日常學(xué)習(xí)、工作和生活中,大家對java都再熟悉不過了吧,下面是小編為大家整理的java中通用的線程池實例代碼,希望對大家有所幫助。

  復(fù)制代碼 代碼如下:

  package com.smart.frame.task.autoTask;

  import java.util.Collection;

  import java.util.Vector;

  /**

  * 任務(wù)分發(fā)器

  */

  public class TaskManage extends Thread

  {

  protected Vectortasks = new Vector();

  protected boolean running = false;

  protected boolean stopped = false;

  protected boolean paused = false;

  protected boolean killed = false;

  private ThreadPool pool;

  public TaskManage(ThreadPool pool)

  {

  this.pool = pool;

  }

  public void putTask(Runnable task)

  {

  tasks.add(task);

  }

  public void putTasks(Runnable[] tasks)

  {

  for (int i = 0; i < tasks.length; i++)

  this.tasks.add(tasks[i]);

  }

  public void putTasks(Collectiontasks)

  {

  this.tasks.addAll(tasks);

  }

  protected Runnable popTask()

  {

  if (tasks.sixx() > 0) return (Runnable) tasks.remove(0);

  else return null;

  }

  public boolean isRunning()

  {

  return running;

  }

  public void stopTasks()

  {

  stopped = true;

  }

  public void stopTasksSync()

  {

  stopTasks();

  while (isRunning())

  {

  try

  {

  sleep(5);

  }

  catch (InterruptedException e)

  {

  TaskException.getResultMessage(e);

  }

  }

  }

  public void pauseTasks()

  {

  paused = true;

  }

  public void pauseTasksSync()

  {

  pauseTasks();

  while (isRunning())

  {

  try

  {

  sleep(5);

  }

  catch (InterruptedException e)

  {

  TaskException.getResultMessage(e);

  }

  }

  }

  public void kill()

  {

  if (!running) interrupt();

  else killed = true;

  }

  public void killSync()

  {

  kill();

  while (isAlive())

  {

  try

  {

  sleep(5);

  }

  catch (InterruptedException e)

  {

  TaskException.getResultMessage(e);

  }

  }

  }

  public synchronized void startTasks()

  {

  running = true;

  this.notify();

  }

  public synchronized void run()

  {

  try

  {

  while (true)

  {

  if (!running || tasks.si ze() = = 0)

  {

  pool.notifyForIdleThread();

  this.wait();

  }

  else

  {

  Runnable task;

  while ((task = popTask()) != null)

  {

  task.run();

  if (stopped)

  {

  stopped = false;

  if (tasks.sixx() > 0)

  {

  tasks.clear();

  System.out.println(Thread.currentThread().getId() + ": Tasks are stopped");

  break;

  }

  }

  if (paused)

  {

  paused = false;

  if (tasks.sixx() > 0)

  {

  System.out.println(Thread.currentThread().getId() + ": Tasks are paused");

  break;

  }

  }

  }

  running = false;

  }

  if (killed)

  {

  killed = false;

  break;

  }

  }

  }

  catch (InterruptedException e)

  {

  TaskException.getResultMessage(e);

  return;

  }

  }

  }

  復(fù)制代碼 代碼如下:

  package com.smart.frame.task.autoTask;

  import java.util.Collection;

  import java.util.Iterator;

  import java.util.Vector;

  /**

  * 線程池

  */

  public class ThreadPool

  {

  protected int maxPoolSize = TaskConfig.maxPoolSize;

  protected int initPoolSize = TaskConfig.initPoolSize;

  protected Vectorthreads = new Vector();

  protected boolean initialized = false;

  protected boolean hasIdleThread = false;

  public ThreadPool()

  {

  super();

  }

  public ThreadPool(int maxPoolSize, int initPoolSize)

  {

  this.maxPoolSize = maxPoolSize;

  this.initPoolSize = initPoolSize;

  }

  public void init()

  {

  initialized = true;

  for (int i = 0; i < initPoolSize; i++)

  {

  TaskManage thread = new TaskManage(this);

  thread.start();

  threads.add(thread);

  }

  }

  public void setMaxPoolSize(int maxPoolSize)

  {

  this.maxPoolSize = maxPoolSize;

  if (maxPoolSize < getPoolSize()) setPoolSize(maxPoolSize);

  }

  /**

  * 重設(shè)當(dāng)前線程數(shù) 若需殺掉某線程,線程不會立刻殺掉,而會等到線程中的事

  * 務(wù)處理完成 但此方法會立刻從線程池中移除該線程,不會等待事務(wù)處理結(jié)束

  */

  public void setPoolSize(int size)

  {

  if (!initialized)

  {

  initPoolSize = size;

  return;

  }

  else if (size > getPoolSize())

  {

  for (int i = getPoolSize(); i < size && i < maxPoolSize; i++)

  {

  TaskManage thread = new TaskManage(this);

  thread.start();

  threads.add(thread);

  }

  }

  else if (size < getPoolSize())

  {

  while (getPoolSize() > size)

  {

  TaskManage th = (TaskManage) threads.remove(0);

  th.kill();

  }

  }

  }

  public int getPoolSize()

  {

  return threads.sixx();

  }

  protected void notifyForIdleThread()

  {

  hasIdleThread = true;

  }

  protected boolean waitForIdleThread()

  {

  hasIdleThread = false;

  while (!hasIdleThread && getPoolSize() >= maxPoolSize)

  {

  try

  {

  Thread.sleep(5);

  }

  catch (InterruptedException e)

  {

  TaskException.getResultMessage(e);

  return false;

  }

  }

  return true;

  }

  public synchronized TaskManage getIdleThread()

  {

  while (true)

  {

  for (Iteratoritr = threads.iterator(); itr.hasNext();)

  {

  TaskManage th = (TaskManage) itr.next();

  if (!th.isRunning()) return th;

  }

  if (getPoolSize() < maxPoolSize)

  {

  TaskManage thread = new TaskManage(this);

  thread.start();

  threads.add(thread);

  return thread;

  }

  if (waitForIdleThread() == false) return null;

  }

  }

  public void processTask(Runnable task)

  {

  TaskManage th = getIdleThread();

  if (th != null)

  {

  th.putTask(task);

  th.startTasks();

  }

  }

  public void processTasksInSingleThread(Runnable[] tasks)

  {

  TaskManage th = getIdleThread();

  if (th != null)

  {

  th.putTasks(tasks);

  th.startTasks();

  }

  }

  public void processTasksInSingleThread(Collectiontasks)

  {

  TaskManage th = getIdleThread();

  if (th != null)

  {

  th.putTasks(tasks);

  th.startTasks();

  }

  }

  }

  復(fù)制代碼 代碼如下:

  package com.smart.frame.task.autoTask;

  public class TopTask implements Runnable

  {

  private ThreadPool pool;

  public TopTask()

  {

  super();

  }

  public TopTask(ThreadPool pool)

  {

  super();

  this.pool = pool;

  }

  @Override

  public void run()

  {

  init();

  start();

  }

  /**

  * 初始化驗證權(quán)限、參數(shù)之類

  */

  public void init()

  {

  }

  /**

  * 開始自動任務(wù)

  */

  public void start()

  {

  for (int i = 0; i < 10; i++)

  {

  pool.processTask(new BeginAuto());

  }

  }

  }

  /**

  * 實現(xiàn)類

  */

  class BeginAuto implements Runnable

  {

  @Override

  public void run()

  {

  System.out.println(Thread.currentThread().getId() + "..................");

  }

  }

  復(fù)制代碼代碼如下:

  package com.smart.frame.task.autoTask;

  import java.util.Collection;

  import java.util.Vector;

  /**

  * 任務(wù)分發(fā)器

  */

  public class TaskManage extends Thread

  {

  protected Vector tasks = new Vector();

  protected boolean running = false;

  protected boolean stopped = false;

  protected boolean paused = false;

  protected boolean killed = false;

  private ThreadPool pool;

  public TaskManage(ThreadPool pool)

  {

  this.pool = pool;

  }

  public void putTask(Runnable task)

  {

  tasks.add(task);

  }

  public void putTasks(Runnable[] tasks)

  {

  for (int i = 0; i < tasks.length; i++)

  this.tasks.add(tasks[i]);

  }

  public void putTasks(Collection tasks)

  {

  this.tasks.addAll(tasks);

  }

  protected Runnable popTask()

  {

  if (tasks.sixx() > 0) return (Runnable) tasks.remove(0);

  else return null;

  }

  public boolean isRunning()

  {

  return running;

  }

  public void stopTasks()

  {

  stopped = true;

  }文章來源地址https://www.yii666.com/blog/74008.html

  public void stopTasksSync()

  {

  stopTasks();

  while (isRunning())

  {

  try

  {

  sleep(5);

  }

  catch (InterruptedException e)

  {

  TaskException.getResultMessage(e);

  }

  }

  }

  public void pauseTasks()

  {

  paused = true;

  }文章地址https://www.yii666.com/blog/74008.html

  public void pauseTasksSync()

  {

  pauseTasks();

  while (isRunning())

  {

  try

  {

  sleep(5);

  }

  catch (InterruptedException e)

  {

  TaskException.getResultMessage(e);

  }

  }

  }

  public void kill()

  {

  if (!running) interrupt();

  else killed = true;

  }

  public void killSync()

  {

  kill();

  while (isAlive())

  {

  try

  {

  sleep(5);

  }

  catch (InterruptedException e)

  {

  TaskException.getResultMessage(e);

  }

  }

  }

  public synchronized void startTasks()

  {

  running = true;

  this.notify();

  }

  public synchronized void run()

  {

  try

  {

  while (true)

  {

  if (!running || tasks.si ze () == 0)

  {

  pool.notifyForIdleThread();

  this.wait();

  }

  else

  {

  Runnable task;

  while ((task = popTask()) != null)

  {

  task.run();

  if (stopped)

  {

  stopped = false;

  if (tasks.sixx() > 0)

  {

  tasks.clear();

  System.out.println(Thread.currentThread().getId() + ": Tasks are stopped");

  break;

  }

  }

  if (paused)

  {

  paused = false;

  if (tasks.sixx() > 0)

  {

  System.out.println(Thread.currentThread().getId() + ": Tasks are paused");

  break;

  }

  }

  }

  running = false;

  }

  if (killed)

  {

  killed = false;

  break;

  }

  }

  }

  catch (InterruptedException e)

  {

  TaskException.getResultMessage(e);

  return;

  }

  }

  }

【java中的線程池實例代碼】相關(guān)文章:

java線程的幾種狀態(tài)12-14

讓網(wǎng)站變灰的css代碼實例12-02

C語言快速排序?qū)嵗a01-27

高級Java多線程面試題及回答04-02

Java創(chuàng)建線程的三種方法03-01

C語言合并排序及實例代碼詳解01-19

java中l(wèi)ength和length()的區(qū)別04-12

JAVA中如何執(zhí)行DOS命令10-13

Java編程中異常處理的方法12-16

Java中創(chuàng)建對象的5種方法03-04