Results 1 to 8 of 8
Thread: Java Blocking Output
- 02-23-2012, 11:15 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 3
- Rep Power
- 0
Java Blocking Output
How do I get Java to use blocking output on Linux/Unix?
This program does not work as expected:
import java.io.*;
public class fifo {
public static void main(String[] args) {
try {
BufferedWriter myout = new BufferedWriter(new FileWriter("fifo1"));
for(int i=1;i<=20000;++i) {
myout.write(i + " Hello World\n");
System.out.print(i + " Hello World\n");
myout.flush();
}
}
catch (IOException e) {
System.err.println(e);
}
}
}
mkfifo fifo1
java fifo
Then run this C program:
main()
{
char buf[4096];
while(gets(buf)) {
puts(buf);
sleep(1);
}
}
cc -o loop loop.c
loop <fifof1
As soon as the 1st line read the Java program will blast out 4000 or so lines and stop.
I want one line out per line read in by the loop progam
But I can't figure out how to do a blocking write this way. I have to use fifos like this.
- 02-24-2012, 03:54 AM #2
Re: Java Blocking Output
Not really suited to New to Java. Moving to Advanced Java.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-26-2012, 05:02 AM #3
Member
- Join Date
- Oct 2011
- Posts
- 33
- Rep Power
- 0
Re: Java Blocking Output
@mdblack98 , May be I didn't get your point, do you want to serialize execution of Java and C program so that when Java write one line than wait until C reads that line ? I don't anything with related to file system. if you can use Socket than use it , socket read is a blocking call and you can effectively communicate following client server paradigm.
- 02-27-2012, 05:50 PM #4
Member
- Join Date
- Feb 2012
- Posts
- 3
- Rep Power
- 0
Re: Java Blocking Output
Yes...I do want serialized output as you describe it.
But I shouldn't have to resort to sockets which means I have to write another layer of complexity and completely change my architecture on input to the system. This is ultimately talking to another C program which works fine with multiple FIFOs running under C...just not the Java one for this reason.
For some odd reason read()'s are blocking, but write()'s are not. I can find tons of references on non-blocking I/O but I want the opposite (which is what a FIFO is supposed to be under Unix....blocking).
My other choice is to get the Java source code and build my own class that behaves correctly.
I consider this a bug...but that assumes there is not some "correct" way to do this that I just can't find.
- 02-27-2012, 07:30 PM #5
Re: Java Blocking Output
What class is the write() method in?
What does blocking of a write mean? That the output buffer is full and the OS won't empty it.
Is the program a producer/consumer problem where the consumer must remove an item from a finite sized queue before the producer can add an item? When the queue is full, the producer needs to block until there is room.Last edited by Norm; 02-27-2012 at 07:32 PM.
- 02-28-2012, 02:41 PM #6
Member
- Join Date
- Feb 2012
- Posts
- 3
- Rep Power
- 0
Re: Java Blocking Output
I don't care what class write() is in. I've tried FileWriter and BufferedWriter...both are the same.
A blocking write means just that -- no matter what data you write to it that it won't finish until the reader reads it. Completely serialized and synchronous. Just like FIFOs have behaved under Unix for decades. Seems most people aren't familiar with them.
Yes...it's a producer/consumer problem....the producer runs faster than the consumer and must not overrun the queue.
If you're not familiar with FIFOs do this:
mkfifo fifo1
echo hello >fifo1 **** You'll not that this does NOT finish...i.e. it's blocked. Start another terminal window.
cat <fifo1
This is what is called a blocking write which, from what I can find, Java doesn't know how to do with any kind of file output.
- 02-28-2012, 03:27 PM #7
Re: Java Blocking Output
If you want a blocking queue application you will have to write it yourself or get someone else's.
I don't know of any write() method in the Java SE that will block. But I have never looked for that so it could be there.
- 02-28-2012, 04:23 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,475
- Rep Power
- 16
Re: Java Blocking Output
It won't be the write() that blocks, it'll be the flush().
The write() will simply stick the line into the buffer in the BufferedReader (and any other buffer further down).
The flush should write to disk, though I have no idea as that starts to head into com.sun territory (StreamEncoder).
FileOutputStream does write on a write(), depending on what the native code underneath is of course.Please do not ask for code as refusal often offends.
Similar Threads
-
My new post: Producer Consumer solution using blocking Queue in java
By javin.paul in forum Reviews / AdvertisingReplies: 0Last Post: 02-22-2012, 10:15 AM -
non-blocking SSL socket server
By e_scape in forum NetworkingReplies: 0Last Post: 04-12-2011, 05:18 PM -
Java, output string, getting correct output? HELP!
By computerboyo in forum New To JavaReplies: 2Last Post: 02-25-2009, 11:44 PM -
[SOLVED] Blocking Queues - how?
By sebo in forum New To JavaReplies: 4Last Post: 12-08-2008, 12:12 PM -
Non Blocking Network
By mathias in forum NetworkingReplies: 1Last Post: 08-07-2007, 06:49 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks