Java 单独线程中的扫描仪输入
Java Scanner input in separate thread
我有一个多线程命令行应用程序。它是一个 Web 服务客户端,有一个包含 10 个线程的池,这些线程以批处理方式向服务器发送请求。
但它运行了几天,有时在管道的更深处,队列开始备份。所以我想去客户端,按 - 或 + 并增加或减少 Thread.sleep(waitingTime),以减轻服务器的压力。
我在单独的线程中尝试 运行 扫描仪,但它似乎没有用。有没有人设法让非阻塞 I/O 在 Java 中工作?我认为这是可能的,但我现在放弃了。
编辑:根据要求添加了测试代码
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 ]");
}
}
}
}
您的 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.
之前,实际上没有任何输入传递给程序
我有一个多线程命令行应用程序。它是一个 Web 服务客户端,有一个包含 10 个线程的池,这些线程以批处理方式向服务器发送请求。
但它运行了几天,有时在管道的更深处,队列开始备份。所以我想去客户端,按 - 或 + 并增加或减少 Thread.sleep(waitingTime),以减轻服务器的压力。
我在单独的线程中尝试 运行 扫描仪,但它似乎没有用。有没有人设法让非阻塞 I/O 在 Java 中工作?我认为这是可能的,但我现在放弃了。
编辑:根据要求添加了测试代码
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 ]");
}
}
}
}
您的 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.