Results 1 to 5 of 5
  1. #1
    armai is offline Member
    Join Date
    Oct 2016
    Posts
    5
    Rep Power
    0

    Default Comprehension Question: Thread processing orders, BufferedOutputStream

    When I have a method which creates a thread like this:
    Java Code:
    [I]public void SendFileThread(final int threadID, final String path){
    
        AsynchUtil.runAsynchronously(new Runnable() {@Override
        public void run() {
    
            final File file = new File(path);
    
            byte[] bytes = new byte[(int) file.length()];
    
            try {
    
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
    
                BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
    
                bis.read(bytes, 0, bytes.length);
    
                bos.write(bytes, 0, bytes.length);
    
                bos.flush();
    
                        ThreadFinish(threadID);
                
            } catch (FileNotFoundException e) {
    
                e.printStackTrace();
    
            } catch (IOException e) {
    
                e.printStackTrace();
    
            } finally {
    
            }
        }
    
        });
    }[/I]
    What will happen, when I call that method from the GUI thread serveral times simultaneously, with different parameters? Will the thread called a moment later start being processed after the first called thread has finished, or will both run simultaneously?

    I testet this by writing a thread code which sends a file from the path which is given by the parameter. Then I wrote an app with a button, which initializes a socket and then starts that thread 2 times simultaneously with different file path parameters (first one is pointing a text file full of "AAAAA", the second one a text file full of "BBBBB"). I clicked that button and then looked on the server side what was coming in. The result was that the second file data (the "B"-army) was arriving after the last "A" of the A army from the first file.

    From this I could assume that the threads are being processed sequentially (one by one) and not simultaneously.

    But I am not sure about this. It is possible that the BufferedOutputStream is blocking the output stream from the second thread until the first one has finished reading the AAA file in. Could this be happening? Is a thread being blocked, while it cant write data into the buffer, or will it just proceed after given the command to the bufferstream handler or what ever (shall I consider the buffered streaming thing like another thread of its own?).

    I really would appreciate some coachings on this.

    Thank you
    Last edited by Norm; 10-17-2016 at 08:26 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Comprehension Question: Thread processing orders, BufferedOutputStream

    What package is the AsynchUtil class in? I don't recognize it as part of the Java SE packages.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    armai is offline Member
    Join Date
    Oct 2016
    Posts
    5
    Rep Power
    0

    Default Re: Comprehension Question: Thread processing orders, BufferedOutputStream

    You are right. It is from the MIT App Inventor 2 package

    import com.google.appinventor.components.runtime.util.Asy nchUtil;

    Anyways, I think it is the same as this:

    https://developer.android.com/refere.../Runnable.html

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Comprehension Question: Thread processing orders, BufferedOutputStream

    I recommend you stick with Java API classes. You are more likely to get help than if someone has to download
    a third party package.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Comprehension Question: Thread processing orders, BufferedOutputStream

    developer.android.com
    Is this part of an Android app? That may have some bearing on what happens.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: 01-22-2014, 12:06 PM
  2. how to reduce the thread processing time
    By chnaveen in forum Threads and Synchronization
    Replies: 3
    Last Post: 07-16-2011, 09:15 AM
  3. Question about 'main thread' and the thread it creates
    By ggyyree in forum Threads and Synchronization
    Replies: 11
    Last Post: 12-10-2010, 07:33 PM
  4. Replies: 6
    Last Post: 12-08-2010, 12:39 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •