Results 1 to 8 of 8
  1. #1
    mdblack98 is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default 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.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Java Blocking Output

    Not really suited to New to Java. Moving to Advanced Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    javin.paul is offline Member
    Join Date
    Oct 2011
    Posts
    35
    Rep Power
    0

    Default 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.

  4. #4
    mdblack98 is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default 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.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default 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.

  6. #6
    mdblack98 is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default 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.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default 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.

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,970
    Rep Power
    19

    Default 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.

    ** This space for rent **

Similar Threads

  1. My new post: Producer Consumer solution using blocking Queue in java
    By javin.paul in forum Reviews / Advertising
    Replies: 0
    Last Post: 02-22-2012, 10:15 AM
  2. non-blocking SSL socket server
    By e_scape in forum Networking
    Replies: 0
    Last Post: 04-12-2011, 05:18 PM
  3. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM
  4. [SOLVED] Blocking Queues - how?
    By sebo in forum New To Java
    Replies: 4
    Last Post: 12-08-2008, 12:12 PM
  5. Non Blocking Network
    By mathias in forum Networking
    Replies: 1
    Last Post: 08-07-2007, 06:49 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
  •