Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

Help in Threading when program waits for an input

Thread view: 
Mithil - 29 May 2007 19:59 GMT
Hello everyone,

I am using the following line to get input from the user in the
command prompt.

BufferedReader dis = new BufferedReader(new
InputStreamReader(System.in));

The program does nothing until the user enter a value into it, is it
possible to use threads and do more work while waiting for the input.
If so how can I do it any code examples would be great :)

Thanks in advance,
Mithil
Richard Reynolds - 29 May 2007 20:19 GMT
> Hello everyone,
>
[quoted text clipped - 10 lines]
> Thanks in advance,
> Mithil

yep, http://java.sun.com/docs/books/tutorial/essential/concurrency/
http://www.javaworld.com/javaworld/jw-04-1996/jw-04-threads.html etc.
try searching for java thread tutorial on google, for what you want to do
it's fairly straightforward stuff.
Tom Hawtin - 29 May 2007 20:40 GMT
> I am using the following line to get input from the user in the
> command prompt.
[quoted text clipped - 5 lines]
> possible to use threads and do more work while waiting for the input.
> If so how can I do it any code examples would be great :)

Threading is difficult.

For this example, assuming you just want to read lines of input and poll
for results:

    final BlockingQueue<String> input =
       new java.util.concurrent.ArrayBlockingQueue<String>(10);
    Thread thread = new Thread(new Runnable() {
            public void run() {
                try {
                    BufferedReader in = new BufferedReader(
                        new InputStreamReader(System.in)
                    );
                    for (;;) {
                        String line = in.readLine();
                        if (line == null) {
                            break;
                        }
                        put(line);
                    }
                } catch (java.io.IOException exc) {
                    // Oops... (perhaps should quit)
                    throw new Error(exc);
                } finally {
                    put(null);
                }
            }
            private void put(String line) {
                for (;;) {
                    try {
                        input.put(line);
                        return;
                    } catch (java.lang.InterruptedException exc) {
                        // Ignore - we should keep going.
                        // IO may throw, however.
                    }
                }
            }
    });
    thread.setDaemon(true);
    thread.setPriority(6);
    thread.start();

    outerLp: for (;;) {
        while (!input.isEmpty()) {
            String line = input.remove();
            if (line == null) {
                // End of input.
                break outerLp; // say
            }
            ... do stuff with line ...
        }
        ... do a little stuff while waiting ...
    }

(Disclaimer: Not tested or even compiled.)

Tom hawtin


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.