疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

Java多线程 生成服务器pdf


 

      应需要要在服务器上生成十几万的pdf文件,所有首先想到的是java多线程,先上代码请仔细阅读,

  import java.util.concurrent.ArrayBlockingQueue;

  import java.util.concurrent.BlockingQueue;

  import java.util.concurrent.LinkedBlockingQueue;

  import java.util.concurrent.ThreadPoolExecutor;

  import java.util.concurrent.TimeUnit;

  public class TestThreadPool {

  private static int produceTaskSleepTime = 3000;

  private static int produceTaskMaxNumber = 10;

  public static void main(String[] args) {

  // 构造一个线程池

  //corePoolSize: 线程池维护线程的最少数量

  //maximumPoolSize:线程池维护线程的最大数量

  //keepAliveTime: 线程池维护线程所允许的空闲时间

  //unit: 线程池维护线程所允许的空闲时间的单位

  //workQueue: 线程池所使用的缓冲队列

  //handler: 线程池对拒绝任务的处理策略

  // 如果此时线程池中的数量小于corePoolSize,即使线程池中的线程都处于空闲状态,也要创建新的线程来处理被添加的任务。

  // l 如果此时线程池中的数量等于 corePoolSize,但是缓冲队列 workQueue未满,那么任务被放入缓冲队列。

  // l 如果此时线程池中的数量大于corePoolSize,缓冲队列workQueue满,并且线程池中的数量小于maximumPoolSize,建新的线程来处理被添加的任务。

  // l 如果此时线程池中的数量大于corePoolSize,缓冲队列workQueue满,并且线程池中的数量等于maximumPoolSize,那么通过 handler所指定的策略来处理此任务。也就是:处理任务的优先级为:核心线程corePoolSize、任务队列workQueue、最大线程maximumPoolSize,如果三者都满了,使用handler处理被拒绝的任务。

  //l 当线程池中的线程数量大于 corePoolSize时,如果某线程空闲时间超过keepAliveTime,线程将被终止。这样,线程池可以动态的调整池中的线程数

  // ThreadPoolExecutor.AbortPolicy()

  // 抛出java.util.concurrent.RejectedExecutionException异常

  //

  // ThreadPoolExecutor.CallerRunsPolicy()

  // 重试添加当前的任务,他会自动重复调用execute()方法

  //

  // ThreadPoolExecutor.DiscardOldestPolicy()

  // 抛弃旧的任务

  //

  // ThreadPoolExecutor.DiscardPolicy()

  // 抛弃当前的任务

  //总共50个线程,因为核心线程数为10,10个线程被立即运行,线程队列大小为10,

  //所以10个线程被加入队列,最大线程数为5000,还能运行50-10=40个,其50个线程的其余10个线程又立即运行了

  //一个线程池相当于一个cpu

  //两个cpu同时跑缓存队列为10

  //ArrayBlockingQueue queue = new ArrayBlockingQueue(1); //固定为4的线程队列

  BlockingQueue queue = new LinkedBlockingQueue();

  //个人认为这个的地方的参数是根据cpu的个数来定的

  ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2,4,1,

  TimeUnit.SECONDS, queue,

  new ThreadPoolExecutor.DiscardOldestPolicy());

  //多线程

  try {

  //生成100pdf

  int tasks=100;

  long t1=System.currentTimeMillis();

  System.out.println("开始时间:"+t1);

  System.out.println("正在生产数据...");

  for(int i=0;i

  threadPool.execute(new ThreadPoolTask(i));

  }

  int threadSize = queue.size();

  System.out.println("线程队列大小为-->"+threadSize);

  //Thread.sleep(produceTaskSleepTime); //休眠是为了让该线程不至于执行完毕后从线程池里释放

  threadPool.shutdown();

  long t2=System.currentTimeMillis();

  System.out.println("运行时间:"+(t2-t1)+"ms");

  System.out.println("退出生产者线程!");

  } catch (Exception e) {

  e.printStackTrace();

  }

  //单线程

  // try {

  // int tasks=50;

  // threadPool.execute(new ThreadPoolTask(50));

  // Thread.sleep(produceTaskSleepTime); //休眠是为了让该线程不至于执行完毕后从线程池里释放

  // threadPool.shutdown();

  // } catch (Exception e) {

  // e.printStackTrace();

  // }

  }

  }

  构建程序类ThreadPoolTask

  import java.io.File;

  import java.io.IOException;

  import java.io.Serializable;

  import java.util.Random;

  import com.fr.util.Convert;

  public class ThreadPoolTask implements Runnable, Serializable {

  private Object attachData;

  ThreadPoolTask(Object tasks) {

  this.attachData = tasks;

  }

  public void run() {

  synchronized (this) {

  int taskscount = Convert.ToInt32(this.getTask());

  System.out.println(Thread.currentThread().getName());

  String newFile = indexof.getBaseApplicationPath()+"gb\"+"yjqj\";//构建的新目录

  File file = new File(newFile);

  if(!file.exists()){

  boolean flage =file.mkdirs();

  }

  String data = null;

  try {

  data = "data" + this.getTask();

  String pdfFile = newFile+data+".pdf";

  File f = new File(pdfFile);

  try {

  f.createNewFile();

  } catch (IOException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  } catch (Exception e) {

  e.printStackTrace();

  Thread.currentThread().interrupt();

  } finally {

  }

  }

  }

  public Object getTask() {

  return this.attachData;

  }

  }

  运行结果

  开始时间:1367992613865

  正在生产数据...

  线程队列大小为-->98

  运行时间:33ms

  退出生产者线程!

  pool-1-thread-2

  pool-1-thread-1

  pool-1-thread-2

  pool-1-thread-2

  pool-1-thread-2

  pool-1-thread-2

  pool-1-thread-2

  pool-1-thread-2

  pool-1-thread-2

  pool-1-thread-2

  pool-1-thread-2

  pool-1-thread-2

  pool-1-thread-2

  pool-1-thread-2

  pool-1-thread-2

  pool-1-thread-2

  pool-1-thread-2

  pool-1-thread-2

  pool-1-thread-2

  pool-1-thread-2

  pool-1-thread-2

  pool-1-thread-2

  pool-1-thread-2

  pool-1-thread-2

  pool-1-thread-2

  pool-1-thread-2

  pool-1-thread-2

  pool-1-thread-2

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-2

  pool-1-thread-1

  pool-1-thread-2

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-2

  pool-1-thread-1

  pool-1-thread-2

  pool-1-thread-1

  pool-1-thread-2

  pool-1-thread-1

  pool-1-thread-2

  pool-1-thread-1

  pool-1-thread-2

  pool-1-thread-1

  pool-1-thread-2

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-2

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-2

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-2

  pool-1-thread-1

  pool-1-thread-2

  pool-1-thread-1

  pool-1-thread-2

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-2

  pool-1-thread-2

  pool-1-thread-2

  pool-1-thread-2

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-1

  pool-1-thread-2

  问题总结

  1、在调试模块运行生成pdf个数是正确的,但是在发布到tomcat上面pdf个数就减少了

  2、在设置的多线程参数的时候应该根据服务器的内存、cpu的大小来设置