Entering a Java scanner in a separate thread

I have a multithreaded command line application. This is a web service client with a pool of 10 threads that selects, sends requests, batch style, to the server.

But it works for several days, and sometimes further down the assembly line, queues begin to recover. So I want to go to the client, press - or + and increase or decrease Thread.sleep (waitingTime) to relieve pressure from the server.

I tried to run the scanner in a separate thread, but it did not seem to work. Has anyone been able to get non-blocking Java I / O? I suppose it's possible, but now I'm giving up.

Edit: Added test code on request

package test;

import java.io.*;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by djb on 2015/06/03.
 */
public class ThreadTest {
    public ThreadTest() {
    }

    static long rand = 10000;

    public static void main(String args[])
    {

        ExecutorService executor = Executors.newFixedThreadPool(5);

        File f = new File("C:\\code\\ThreadTest\\text.csv");
        try {

            Runnable keyPressThread = new ThreadTest.KeyPressThread();
            Thread t = new Thread(keyPressThread);
            t.start();

            BufferedReader br = new BufferedReader(new FileReader(f));

            String line;

            while ((line = br.readLine()) != null)
            {

                try {
                    final String copy = line;

                    executor.execute(new Runnable() {
                        @Override
                        public void run() {
                            try {

                                System.out.println(rand);
                                Thread.sleep(rand);
                                System.out.println(copy);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    });


                } catch (Exception e)
                {
                    e.printStackTrace();
                }

            }

        } catch (Exception e)
        {
            e.printStackTrace();
        }




    }


    public static class KeyPressThread implements Runnable {

        Scanner inputReader = new Scanner(System.in);

        //Method that gets called when the object is instantiated
        public KeyPressThread() {
        }

        public void run() {
            String input = inputReader.next();
            if (input.equals("["))
            {
                rand+=100;
                System.out.println("Pressed [");
            }
            if (input.equals("]"))
            {
                rand-=100;
                System.out.println("Pressed ]");
            }
        }

    }



}
+4
source share
1 answer

KeyPressThread :

.

public void run() 
{
    while(true) 
    {
        if (inputReader.hasNext())
        {
            String input = inputReader.next();
            if (input.equals("["))
            {
                rand+=100;
                System.out.println("Pressed [");
            }
            if (input.equals("]"))
            {
               rand-=100;
               System.out.println("Pressed ]");
            }
            if (input.equalsIgnoreCase("Q"))
            {
                break; // stop KeyPressThread
            }
        }
    }
}

System.in . , , ENTER.

+1

All Articles