Java Scanner input in separate thread - java

I have a multi-threaded command line app. It is a web service client with a pool of 10 threads that churns away, sending requests, batch-style, to a server.
But it runs for a few days, and sometimes further down the pipeline, the queues start getting backed up. So I want to go to the client, press - or + and have that increase or decrease a Thread.sleep(waitingTime), to take pressure off the server.
I tried running a Scanner in a separate thread, but it didn't seem to work. Has anyone managed to get non-blocking I/O working in Java? I presume it's possible, but I'm giving up for now.
Edit: Added test code as per 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 ]");
}
}
}
}

Your KeyPressThread is only testing once:
This will make it watch constantly.
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 is line buffered, by default. This means that no input is actually passed to the program until you press ENTER.

Related

How do I stop and skip a command after 3.5 seconds?

I want to stop and skip a command while it's waiting for input after 3.5 seconds. I have tried to use System.currentTimeMillis() by subtracting from the start time, however the code I made does not skip the input.
food is an arrayList from the table class.
public void timer() {
startTime = System.currentTimeMillis();
while(false||(System.currentTimeMillis()-startTime)<3500)
{
correct = input(); //What I want to skip after 3.5 seconds
}
record();
}
Here is the input() method:
public boolean input()
{
Scanner console = new Scanner (System.in);
//I want to skip everything after this after 3.5 seconds.
int num = console.nextInt();
num--;
System.out.println("You selected " + table.food.get(num).toString());
table.food.remove(num);
if (num==choice)
{
return true;
}
return false;
}
One of the problems you are facing is that any of the Scanner's next methods can not be interrupted when reading from a console. Therefore you have to read the input in a different way, for example by using a InputStreamReader.
After that you can submit a specific task to a ExecutorService that handels the execution of the "input reading" seperately from the main Thread. You will get a Future on which you can define a timeout.
Note that this operation is still blocking (on both threads).
This solution is somewhat based on this article.
import java.io.*;
import java.util.concurrent.*;
public class Test {
static class ReadInput implements Callable<Integer> {
public Integer call() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
while (br.ready() == false) {
Thread.sleep(250);
}
String input = br.readLine();
return Integer.parseInt(input);
} catch (InterruptedException e) {
return null;
}
}
}
public static void main(String[] args) {
Integer input = null;
ExecutorService ex = Executors.newSingleThreadExecutor();
try {
Future<Integer> future = ex.submit(new ReadInput());
input = future.get(3500, TimeUnit.MILLISECONDS);
} catch (ExecutionException | InterruptedException | TimeoutException e) {
// handle exceptions that need to be handeled
} finally {
ex.shutdownNow();
}
System.out.println("done: " + input);
}
}
Note that timeout in the ReadInput should be lower than the timeout in the main Thread.

Strange thread behavior

I'm trying to do this: The question is displayed in the console. If during some time the user does not write the answer, then the next question is asked. If the user enters an answer, the next question is asked immediately. My code:
public class Test {
private boolean stopQuestion;
Thread scannerThread = new Thread();
public static void main(final String[] args) {
final Test test = new Test();
test.scannerThread = new Thread(new Runnable() {
#Override
public void run() {
try {
String string;
do {
string = test.requestInput(new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(3000);
} catch (final InterruptedException e) {
}
test.scannerThread.interrupt();
}
}));
} while (!test.stopQuestion);
System.out.println("Input: " + string);
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
});
test.scannerThread.start();
}
public String requestInput(final Thread timer) throws IOException {
final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
timer.start();
System.out.println("Any question");
System.out.println("Please type your answer: ");
try {
while (!br.ready()) {
Thread.sleep(100);
}
} catch (final InterruptedException e) {
System.out.println("Time is over. Next question: ");
return null;
}
System.out.println("Thank You for providing input!");
return br.readLine();
}
}
If you do not write anything to the console, everything seems to work as expected. Time ends and the next question is asked. But if something is written to the console, the timer starts to malfunction and the next question does not wait for the specified amount of time, sometimes it does not wait at all. I do not understand what's the matter.
I created instance of thread outside the method and pass instance to the method as reference but then throws IllegalThreadStateException.
I see two major problems with your code:
You are continously creating threads that are supposed to read input:
do {
string = test.requestInput(new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(3000);
} catch (final InterruptedException e) {
e.printStackTrace();
}
test.scannerThread.interrupt();
}
}));
} while (!test.stopQuestion); // <-- this is always true
You are opening as many BufferedReaders on System.in as many timer threads you are launching:
final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Also, you are not closing any of these BufferedReader instances.

Console application with two threads

I just want to print learning... as long as I enter 1
package a;
import java.util.Scanner;
class main extends Thread {
static String n;
Scanner reader = new Scanner(System.in);
public void run() {
n = reader.nextLine();
}
public static void main(String args[]) throws InterruptedException {
(new Thread(new main())).start();
n="5";
System.out.println("1 = ON\n0 = OFF");
while (n.equals("1")) {
System.out.println("Learning..");
}
}
}
You may be interested in reading up on the Producer-Consumer pattern. You can take a look here http://javarevisited.blogspot.fr/2012/02/producer-consumer-design-pattern-with.html and try with something like
class main extends Thread {
// a thread-safe queue for decoupling reading and writing threads avoiding
// synchronization issues. The capacity of the queue is 1 to avoid reading (producing) a
// command without having handled (consumed) the previous before
private static final BlockingQueue<String> sharedQueue = new LinkedBlockingQueue<>(1);
Scanner reader = new Scanner(System.in);
public void run() {
while (true) {
String s = reader.nextLine();
try {
//if the queue is empty, adds the element,
//otherwise blocks waiting for the current element to be handled by main thread
sharedQueue.put(s);
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
}
}
public static void main(String args[]) throws InterruptedException {
(new Thread(new main())).start();
System.out.println("1 = ON\n0 = OFF");
while (true) {
//will block till an element is available, then removes and handles it
final String s = sharedQueue.take();
if ("1".equals(s)) {
System.out.println("Learning..");
}
}
}
}
If you are trying to stop start, it is always better to maintain two threads one for printing and other for taking input. Try with blow code. It is working fine for me.
public class ThreadsStop {
static String n="";
class Printer extends Thread{
#Override
public void run() {
while(!n.equals(null)){
try {
Thread.sleep(1000);
if(n.trim().equals("1"))
System.out.println("Learning..");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Starter extends Thread{
#Override
public void run() {
Scanner reader = new Scanner(System.in);
while(true){
System.out.println("1 = ON \n 0 = OFF");
n= reader.nextLine();
}
}
}
public static void main(String[] args) {
new ThreadsStop().start();
}
private void start() {
new Starter().start();
new Printer().start();
}
}
Use can use the given below code.
class main extends Thread {
static String n;
Scanner reader = new Scanner(System.in);
public void run() {
while (true) {
n = reader.nextLine();
if (Integer.parseInt(n) == 0) {
break;
}
}
}
public static void main(String args[]) throws InterruptedException {
(new Thread(new main())).start();
System.out.println("1 = ON\n0 = OFF");
while (n == null) {
}
while (n.equals("1")) {
System.out.println("Learning..");
}
System.out.println("DONE");
}
}
Try below program, it will take your input and print it.
class main extends Thread {
static String n;
Scanner reader = new Scanner(System.in);
public void run() {
System.out.println("Enter n value ");
n = reader.nextLine();
}
public static void main(String args[]) throws InterruptedException {
(new Thread(new main())).start();
n="5";
System.out.println("1 = ON\n0 = OFF");
while (n.equals("5")) {
//System.out.println("Learning..");
}
System.out.println(n);
}
}
The reason why your code is not taking input, before providing the input your main method executed, which means that program execution completed. I have done few modifications to your code. Now your code will take your input.

I'm trying to run console input in a thread in Java but my program is getting hungup on the readLine() method

I'm creating this program to test getting user input in a thread for a chat server program. This program stops on read = into.readLine();. Why is that and what's happening?
Here is the code:
import java.io.*;
import java.net.*;
public class ThreadClass implements Runnable
{
DataInputStream in;
private boolean checkLoop = false;
public void run()
{
BufferedReader into = new BufferedReader(new InputStreamReader(System.in));
String read;
System.out.println("Welcome...");
while(!checkLoop)
{
try
{
System.out.println("running1");
read = into.readLine();
System.out.println(read);
if(read.equals(".bye"))
{
checkLoop = true;
}
Thread.sleep(500);
}
catch(IOException e)
{
System.out.println(e);System.out.println("running2");
}
catch (InterruptedException ie)
{
System.out.println(ie);System.out.println("running3");
}
}
System.out.println("running4");
}
public static void main(String[]args)
{
ThreadClass main = new ThreadClass();
Thread t1 = new Thread(main);
t1.start();
}
}
When you use "read = into.readLine();" the program will stop and wait for the user to press the "Enter" key. Your program is working fine from my point of view.
Try to type something in console and you will see the program running correctly.

How to make the same program act like a server and client (using sockets in java)

How can I send and receive from the same program in java ? To make matters worse, I need to do both in the same time in parallel.
You need a well behaved queue such as a BlockingQueue between two Threads.
public class TwoThreads {
static final String FINISHED = "Finished";
public static void main(String[] args) throws InterruptedException {
// The queue
final BlockingQueue<String> q = new ArrayBlockingQueue<String>(10);
// The sending thread.
new Thread() {
#Override
public void run() {
String message = "Now is the time for all good men to come to he aid of the party.";
try {
// Send each word.
for (String word : message.split(" ")) {
q.put(word);
}
// Then the terminator.
q.put(FINISHED);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
{ start();}
};
// The receiving thread.
new Thread() {
#Override
public void run() {
try {
String word;
// Read each word until finished is detected.
while ((word = q.take()) != FINISHED) {
System.out.println(word);
}
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
{ start();}
};
}
}

Categories