我想实现一个功能, 第一个线程持续监测键盘输入,如果输入的是一个特定字母,比如“a",那么这个线程wait,第二个线程执行输出一行信息,输出之后,notify,第一个线程继续监测键盘输入。可是我的代码即使第一个线程监测到输入的是字母a,第二个线程也无法执行。debug的时候显示InputWatcher函数里面的if语句那出现了短点,想请教代码有什么问题,应该怎么改。下面是代码:import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class InputMonitor implements Runnable{ private volatile boolean input=false;
synchronized void InputWatcher() throws IOException
{
while(input==false)
{
System.out.println("checking");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String cmd=br.readLine();
if(cmd=="a")
{
System.out.println("the input is a");
input=true;
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public synchronized void getMessage()
{
while(input==true)
{
System.out.println("i know it is a");
input=false;
notify();
}
}
public void run()
{
try {
InputWatcher();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class getMonitor implements Runnable {
private InputMonitor monitor;
public getMonitor(InputMonitor mymonitor)
{
this.monitor=mymonitor;
}
@Override
public void run() {
// TODO Auto-generated method stub
monitor.getMessage();
}}
public class test { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
InputMonitor monitor=new InputMonitor();
getMonitor gm=new getMonitor(monitor);
new Thread(monitor).start();
new Thread(gm).start();
}}多谢各位大神了,刚接触多线程编程,有点迷茫
import java.io.IOException;
import java.io.InputStreamReader;
public class InputMonitor implements Runnable{ private volatile boolean input=false;
synchronized void InputWatcher() throws IOException
{
while(input==false)
{
System.out.println("checking");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String cmd=br.readLine();
if(cmd=="a")
{
System.out.println("the input is a");
input=true;
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public synchronized void getMessage()
{
while(input==true)
{
System.out.println("i know it is a");
input=false;
notify();
}
}
public void run()
{
try {
InputWatcher();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class getMonitor implements Runnable {
private InputMonitor monitor;
public getMonitor(InputMonitor mymonitor)
{
this.monitor=mymonitor;
}
@Override
public void run() {
// TODO Auto-generated method stub
monitor.getMessage();
}}
public class test { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
InputMonitor monitor=new InputMonitor();
getMonitor gm=new getMonitor(monitor);
new Thread(monitor).start();
new Thread(gm).start();
}}多谢各位大神了,刚接触多线程编程,有点迷茫
import java.io.IOException;
import java.io.InputStreamReader;
public class InputMonitor implements Runnable{ private volatile boolean input=false;
synchronized void InputWatcher() throws IOException
{
while(input==false)
{
System.out.println("checking");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String cmd=br.readLine();
System.out.println(cmd);
if("a".equals(cmd))
{
System.out.println("the input is a");
input=true;
notify();
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public synchronized void getMessage()
{
//还有这个线程可能不启动就会死亡了,如果是这个线程先启动了,这时候Input等于false,那么这个线程不执行就死亡了,以后注意啊
while(input==true)
{
System.out.println("do input");
input=false;
notify();
System.out.println("得到");//注意这里notify后并不会立即停下该线程会继续执行,等到执行完你写的while循环后,条件满足这个线程就执行完了
//希望这样解释你能明白
try
{
wait();
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void run()
{
try {
InputWatcher();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Input down");
}
}
看一下注释就可以了,程序没有该对
楼主去我博客看看 有根这个一样的http://blog.csdn.net/mengxiangyue/article/details/6872509