Results 1 to 5 of 5
- 10-17-2016, 08:16 PM #1
Member
- Join Date
- Oct 2016
- Posts
- 5
- Rep Power
- 0
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]
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 youLast edited by Norm; 10-17-2016 at 09:26 PM. Reason: added code tags
- 10-17-2016, 09:29 PM #2
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.
- 10-22-2016, 06:28 AM #3
Member
- Join Date
- Oct 2016
- Posts
- 5
- Rep Power
- 0
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
- 10-22-2016, 04:41 PM #4
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-22-2016, 05:19 PM #5
Similar Threads
-
Change the way lucene orders documents after scoring
By siddarth in forum LuceneReplies: 0Last Post: 01-22-2014, 01:06 PM -
how to reduce the thread processing time
By chnaveen in forum Threads and SynchronizationReplies: 3Last Post: 07-16-2011, 10:15 AM -
Question about 'main thread' and the thread it creates
By ggyyree in forum Threads and SynchronizationReplies: 11Last Post: 12-10-2010, 08:33 PM -
using backtracking to print all possible orders of numbers
By yotamoo in forum New To JavaReplies: 6Last Post: 12-08-2010, 01:39 AM
Bookmarks