关于多线程
下面程序运行会抛出异常java.lang.IllegalMonitorStateException原因为:某一线程已经试图等待对象的监视器,或者试图通知其他正在等待对象的监视器而本身没有指定监视器的线程。
可是我明明把两个线程都指定了监视器了。请指教。谢谢
import java.util.*;
import java.io.*;
public class Test
{
public String name;
public Test()
{
name = "";
}
public static void main(String args[])
{
Thread thread_1=new Thread(new InThread());
Thread thread_2=new Thread(new OutThread());
thread_1.start();
thread_2.start();
}
}
class InThread extends Test implements Runnable
{
public InThread()
{
}
public void run()
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
synchronized (name)
{
try
{
System.out.println("请输入:");
name=br.readLine();
notify();
}
catch (Exception e)
{
}
}
}
}
class OutThread extends TestPub implements Runnable
{
public OutThread()
{
}
public void run()
{
synchronized (name)
{
System.out.println("输出结果为:" + name);
notify();
}
}
}