4 Replies Latest reply: Sep 19, 2007 2:52 PM by 807600 RSS

    Thread,reading data Asynchronous

    807600
      Hello!

      I want to know how to read data ( as a file or a simple variable) Asynchronous in JAVA?

      Say we have two threads want to access to this data at same time,May someone give me an example for that?

      I search myself,But I found about socket programming,I do n't know socket programming in JAVA.

      Please give me a simple example about that
        • 1. Re: Thread,reading data Asynchronous
          EJP
          There is no asynchronous I/O in Java.

          There is no such thing as asynchronous I/O from a variable in any language I am aware of. Memory access is synchronized to the CPU clock.

          There is concurrent I/O, by using java.io.* and threads. See the Java I/O and Threads tutorials.

          There is non-blocking and multiplexed I/O from TCP sockets, in the java.nio.channels package - see the NIO tutorial.
          • 2. Re: Thread,reading data Asynchronous
            807600
            Hello


            This is from:JAVA threads by orielly:
            3.2 Reading Data Asynchronously
            Let's look at a complete example. One of the primary uses for threads within a Java program is to read
            data asynchronously. In this section, we'll develop a class to read a network socket asynchronously.
            Why is threading important for I/O? Whether you are reading from or writing to a file or network
            socket, a common problem exists, namely, that the action of reading or writing depends on other
            resources. These resources may be other programs; they may be hardware, like the disk or the
            network; they may be the operating system or browser. These resources may become temporarily
            unavailable for a variety of reasons: reading from a network socket may involve waiting until the data
            is available, writing large amounts of data to a file may take a long period of time to complete if the
            disk is busy with other requests, and so on. Unfortunately, the mechanism to check whether these
            resources are available does not exist in the Java API. This is particularly a problem for network
            sockets, where data is likely to take a long time to be transmitted over the network; it is possible for a
            read from a network socket to wait forever.
            Java Threads, 2nd edition
            pag e 34
            Why Asynchronous I/O?
            The driving force behind asynchronous I/O is to allow the program to continue to do
            something useful while it is waiting for data to arrive. If I/O is not asynchronous and not
            running in a thread separate from the applet thread, we run into the problems we discussed
            in the previous chapter: mouse and keyboard events will be delayed, and the program will
            appear to be unresponsive to the user while the I/O completes.
            The InputStream class does contain the available() method. However, not all input streams support
            that method, and on a slow network, writing data to a socket is also likely to take a long time. In
            general, checking for data via the available() method is much less efficient (and much harder to
            program) than creating a new thread to read the data.
            The solution to this problem is to use another thread. Say that we use this new thread in an applet:
            since this new thread is independent of the applet thread, it can block without hanging the applet. Of
            course, this causes a new problem: when this thread finally is able to read the data, this data must be
            returned to the applet thread. Let's take a look at a possible implementation of a generic socket reader
            class that will read the socket from another thread:
            import java.io.*;
            import java.net.*;
            public class
            AsyncReadSocket extends Thread {
            private Socket s;
            private StringBuffer result;
            public AsyncReadSocket(Socket s) {
            this.s = s;
            result = new StringBuffer();
            }
            public void run() {
            DataInputStream is = null;
            try {
            is = new DataInputStream(s.getInputStream());
            } catch (Exception e) {}
            while (true) {
            try {
            char c = is.readChar();
            appendResult(c);
            } catch (Exception e) {}
            }
            }
            // Get the string already read from the socket so far.
            // This method is used by the Applet thread to obtain the data
            // in a synchronous manner.
            public synchronized String getResult() {
            String retval = result.toString();
            result = new StringBuffer();
            return retval;
            }
            // Put new data into the buffer to be returned
            // by the getResult method.
            public synchronized void appendResult(char c) {
            result.append(c);
            }
            }
            • 3. Re: Thread,reading data Asynchronous
              807600
              Hello

              Why Asynchronous I/O?
              The driving force behind asynchronous I/O is to allow the program to continue to do
              something useful while it is waiting for data to arrive. If I/O is not asynchronous and not
              running in a thread separate from the applet thread, we run into the problems we discussed
              in the previous chapter: mouse and keyboard events will be delayed, and the program will
              appear to be unresponsive to the user while the I/O completes.

              Do we have Asynchronous I/O? in JAVA?
              How do you use "Lock" in JAVA?Say we have a variable in class that shares between two threads,How threads lock it?

              Edited by: Simorgh on Sep 19, 2007 11:43 AM
              • 4. Re: Thread,reading data Asynchronous
                807600
                Simorgh wrote:
                The driving force behind asynchronous I/O is to allow the program to continue to do....
                Do we have Asynchronous I/O? in JAVA?
                What you want, no matter what you call it, is what ejp suggested: concurrent IO with threads. I believe that if you do your IO in its own thread, it shouldn't interfere with keyboard or mouse.
                How do you use "Lock" in JAVA?Say we have a variable in class that shares between two threads,How threads lock it?
                Have a look at the Thread class and especially at the keyword "synchronized". Paraphrased from "Just Java 2": In Java, thread mutual exclusion is built on objects. Every object in the system has its own semaphore, so any object in the system can be used as the "turnstile" or "thread serializer" for threads. You use the synchronized[i] keyword and explicitly or implicitly provide an object, any object, to synchronize on.