Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8611827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:28:12+00:00 2026-06-12T04:28:12+00:00

The background thread is here Just to make objective clear – the user will

  • 0

The background thread is here

Just to make objective clear – the user will upload a large file and must be redirected immediately to another page for proceeding different operations. But the file being large, will take time to be read from the controller’s InputStream. So I unwillingly decided to fork a new Thread to handle this I/O. The code is as follows :

The controller servlet

/**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

        System.out.println("In Controller.doPost(...)");

        TempModel tempModel = new TempModel();
        tempModel.uploadSegYFile(request, response);

        System.out.println("Forwarding to Accepted.jsp");

        /*try {
            Thread.sleep(1000 * 60);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/

        request.getRequestDispatcher("/jsp/Accepted.jsp").forward(request,
                response);
    }

The model class

package com.model;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.utils.ProcessUtils;

public class TempModel {

    public void uploadSegYFile(HttpServletRequest request,
            HttpServletResponse response) {
        // TODO Auto-generated method stub

        System.out.println("In TempModel.uploadSegYFile(...)");

        /*
         * Trigger the upload/processing code in a thread, return immediately
         * and notify when the thread completes
         */
        try {
            FileUploaderRunnable fileUploadRunnable = new FileUploaderRunnable(
                    request.getInputStream());

            /*
             * Future<FileUploaderRunnable> future = ProcessUtils.submitTask(
             * fileUploadRunnable, fileUploadRunnable);
             * 
             * FileUploaderRunnable processed = future.get();
             * 
             * System.out.println("Is file uploaded : " +
             * processed.isFileUploaded());
             */

            Thread uploadThread = new Thread(fileUploadRunnable);
            uploadThread.start();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } /*
         * catch (InterruptedException e) { // TODO Auto-generated catch block
         * e.printStackTrace(); } catch (ExecutionException e) { // TODO
         * Auto-generated catch block e.printStackTrace(); }
         */

        System.out.println("Returning from TempModel.uploadSegYFile(...)");
    }

}

The Runnable

package com.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

public class FileUploaderRunnable implements Runnable {

    private boolean isFileUploaded = false;
    private InputStream inputStream = null;

    public FileUploaderRunnable(InputStream inputStream) {
        // TODO Auto-generated constructor stub
        this.inputStream = inputStream;
    }

    public void run() {
        // TODO Auto-generated method stub

        /* Read from InputStream. If success, set isFileUploaded = true */
        System.out.println("Starting upload in a thread");

        File outputFile = new File("D:/06c01_output.seg");/*
                                                         * This will be changed
                                                         * later
                                                         */
        FileOutputStream fos;
        ReadableByteChannel readable = Channels.newChannel(inputStream);
        ByteBuffer buffer = ByteBuffer.allocate(1000000);

        try {

            fos = new FileOutputStream(outputFile);

            while (readable.read(buffer) != -1) {
                fos.write(buffer.array());
                buffer.clear();
            }

            fos.flush();
            fos.close();

            readable.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("File upload thread completed");
    }

    public boolean isFileUploaded() {
        return isFileUploaded;
    }

}

My queries/doubts :

  1. Spawning threads manually from the Servlet makes sense to me logically but scares me coding wise – the container isn’t aware of these threads after all(I think so!)
  2. The current code is giving an Exception which is quite obvious – the stream is inaccessible as the doPost(…) method returns before the run() method completes :

    In Controller.doPost(...)
    In TempModel.uploadSegYFile(...)
    Returning from TempModel.uploadSegYFile(...)
    Forwarding to Accepted.jsp
    Starting upload in a thread
    Exception in thread "Thread-4" java.lang.NullPointerException
        at org.apache.coyote.http11.InternalInputBuffer.fill(InternalInputBuffer.java:512)
        at org.apache.coyote.http11.InternalInputBuffer.fill(InternalInputBuffer.java:497)
        at org.apache.coyote.http11.InternalInputBuffer$InputStreamInputBuffer.doRead(InternalInputBuffer.java:559)
        at org.apache.coyote.http11.AbstractInputBuffer.doRead(AbstractInputBuffer.java:324)
        at org.apache.coyote.Request.doRead(Request.java:422)
        at org.apache.catalina.connector.InputBuffer.realReadBytes(InputBuffer.java:287)
        at org.apache.tomcat.util.buf.ByteChunk.substract(ByteChunk.java:407)
        at org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:310)
        at org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:202)
        at java.nio.channels.Channels$ReadableByteChannelImpl.read(Unknown Source)
        at com.model.FileUploaderRunnable.run(FileUploaderRunnable.java:39)
        at java.lang.Thread.run(Unknown Source)
    
  3. Keeping in mind the point 1., does the use of Executor framework help me in anyway ?

    package com.utils;
    
    import java.util.concurrent.Future;
    import java.util.concurrent.ScheduledThreadPoolExecutor;
    
    public final class ProcessUtils {
    
        /* Ensure that no more than 2 uploads,processing req. are allowed */
        private static final ScheduledThreadPoolExecutor threadPoolExec = new ScheduledThreadPoolExecutor(
                2);
    
        public static <T> Future<T> submitTask(Runnable task, T result) {
    
            return threadPoolExec.submit(task, result);
        }
    }
    

So how should I ensure that the user doesn’t block and the stream remains accessible so that the (uploaded)file can be read from it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:28:13+00:00Added an answer on June 12, 2026 at 4:28 am

    Actually it doesn’t.You’re trying to spawn thread and read content of POST request, and you’re also trying to forward user to another page with same request object. This confuses servlet container.

    You may

    • use separate frame for upload form and controller
    • use pop-up window with upload form and separate controller
    • use AJAX to load next page in-place, while still upload the content in current page (let browser handle that for you)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the situation: You have one long-running calculation running in a background thread.
Here's the background, I have a webservice from a vendor/partner that returns a large
Lets just say you have a simple operation that runs on a background thread.
If I have a background thread and having Join to wait for completing background
I have a background thread that handles communication with an external service. Each time
I have a background thread running that fires events, but how can I ensure
I have a background thread and the thread calls some methods that update the
I created a background thread that get's data and returns it to the main
Calling the WriteObject Method from a background thread is not possible! Is there a
How can I stop a background thread on keyboard flip in android?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.