jtopenlite.com.ibm.jtopenlite.ddm.DDMThreadedReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jt400-jdk9 Show documentation
Show all versions of jt400-jdk9 Show documentation
The Open Source version of the IBM Toolbox for Java
///////////////////////////////////////////////////////////////////////////////
//
// 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 - 2025 Weber Informatics LLC | Privacy Policy