All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jtopenlite.com.ibm.jtopenlite.ddm.DDMThreadedReader Maven / Gradle / Ivy

There is a newer version: 20.0.7
Show newest version
///////////////////////////////////////////////////////////////////////////////
//
// JTOpenLite
//
// Filename:  DDMThreadedReader.java
//
// The source code contained herein is licensed under the IBM Public License
// Version 1.0, which has been approved by the Open Source Initiative.
// Copyright (C) 2011-2012 International Business Machines Corporation and
// others.  All rights reserved.
//
///////////////////////////////////////////////////////////////////////////////

package com.ibm.jtopenlite.ddm;

/**
 * A special kind of {@link DDMReadCallback DDMReadCallback} you can use when you want multiple
 * threads to simultaneously process data being read out of the same file and connection.  The data is read by the main thread,
 * but the conversion is done by one or more processing threads.  Subclass this class and implement the
 * {@link #process process()} method to read record data off-thread from the main I/O thread.
 * This gives the performance advantage of streaming data from the server in parallel with processing said data.
 * It is important to note that using more than one thread will likely cause the records to be processed out-of-order.
**/
public abstract class DDMThreadedReader implements DDMReadCallback
{
  private final DDMFile file_;
  private final DDMReaderRunner[] runners_;
  private final Thread[] threads_;
  private boolean done_;

  private long sequence_;

  /**
   * Constructs a multi-threaded reader to process data being read from the specified file
   * using the specified record format.
   * @param format The record format to copy and give to each thread for it to pass to {@link #process process()}.
   * @param file The file being read.
   * @param numThreads The number of threads to use. This number is capped by the number of buffers in the file object, so
   * that each thread always has at least one buffer to process, to avoid contention. Having more than one buffer per thread is fine.
  **/
  public DDMThreadedReader(final DDMRecordFormat format, final DDMFile file, int numThreads)
  {
    file_ = file;
    done_ = false;

    final int numBuffers = file.getBufferCount();
    if (numThreads > numBuffers) numThreads = numBuffers;
    runners_ = new DDMReaderRunner[numThreads];
    threads_ = new Thread[numThreads];
    for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy