运行 两个线程并发
Run two thread concurrently
我正在尝试同时打印两个线程的名称。任何人都可以建议,我做错了什么?我收到“IllegalMonitorStateException
//奇数线程可运行
public class OddThread 实现 Runnable {
MainClass obj;
public OddThread(MainClass obj) {
// TODO Auto-generated constructor stub
this.obj= obj;
}
public void run() {
int number=1;
while(number <10)
{
obj.PrintNumbers();
notifyAll();
try {
wait();
} catch (InterruptedException e) {System.out.println("Exception in wait of OddThread");
}
number = number+2;
}
}
}
// 偶线程可运行
public class EvenThread implements Runnable{
MainClass obj;
public EvenThread(MainClass obj)
{
this.obj=obj;
}
public void run() {
int number=0;
while(number<=10)
{
obj.PrintNumbers();
notifyAll();
try {
wait();
} catch (InterruptedException e) {
System.out.println("Exception in wait of EvenThread");
}
number = number+2;
}
}
}
// 主要 CLASS
public class MainClass {
public static void main(String[] args)
{
MainClass obj = new MainClass();
// Boolean flag= true;
EvenThread evenThread = new EvenThread(obj);
OddThread oddThread = new OddThread(obj);
Thread Even = new Thread(evenThread);
Thread Odd = new Thread(oddThread);
Even.setName("Even Thread");
Odd.setName("Odd Thread");
Even.start();
Odd.start();
}
public synchronized void PrintNumbers()
{
System.out.println(Thread.currentThread().getName());
}
}
我得到的输出是
偶数线程
奇线程
线程异常 "Even Thread" 线程异常 "Odd Thread" java.lang.IllegalMonitorStateException
您应该将 notifyAll()
和 wait()
包围在 synchronized (this) {}
内
喜欢:
synchronized (this) {
notifyAll();
}
和
synchronized (this) {
wait();
}
这些方法notify()
,notifyAll()
,wait()
只能被这个对象的监视器的所有者线程调用,所以你必须把它们包围在一个同步块中,正如所说Here the Object Java doc
正确代码如下:
public class MainClass {
public static int number=0;
public static void main(String[] args)
{
MainClass obj = new MainClass();
// Boolean flag= true;
EvenThread evenThread = new EvenThread(obj);
OddThread oddThread = new OddThread(obj);
Thread Even = new Thread(evenThread);
Thread Odd = new Thread(oddThread);
Even.setName("Even Thread");
Odd.setName("Odd Thread");
Even.start();
Odd.start();
}
public synchronized void PrintNumbers()
{
while(number<20)
{
System.out.println(number+"---"+Thread.currentThread().getName());
number++;
notifyAll();
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class OddThread implements Runnable {
MainClass obj;
public OddThread(MainClass obj) {
// TODO Auto-generated constructor stub
this.obj= obj;
}
public void run() {
obj.PrintNumbers();
}
}
public class EvenThread implements Runnable{
MainClass obj;
public EvenThread(MainClass obj)
{
this.obj=obj;
}
public void run() {
int number=0;
obj.PrintNumbers();
}
}
我正在尝试同时打印两个线程的名称。任何人都可以建议,我做错了什么?我收到“IllegalMonitorStateException
//奇数线程可运行 public class OddThread 实现 Runnable {
MainClass obj;
public OddThread(MainClass obj) {
// TODO Auto-generated constructor stub
this.obj= obj;
}
public void run() {
int number=1;
while(number <10)
{
obj.PrintNumbers();
notifyAll();
try {
wait();
} catch (InterruptedException e) {System.out.println("Exception in wait of OddThread");
}
number = number+2;
}
}
}
// 偶线程可运行
public class EvenThread implements Runnable{
MainClass obj;
public EvenThread(MainClass obj)
{
this.obj=obj;
}
public void run() {
int number=0;
while(number<=10)
{
obj.PrintNumbers();
notifyAll();
try {
wait();
} catch (InterruptedException e) {
System.out.println("Exception in wait of EvenThread");
}
number = number+2;
}
}
}
// 主要 CLASS
public class MainClass {
public static void main(String[] args)
{
MainClass obj = new MainClass();
// Boolean flag= true;
EvenThread evenThread = new EvenThread(obj);
OddThread oddThread = new OddThread(obj);
Thread Even = new Thread(evenThread);
Thread Odd = new Thread(oddThread);
Even.setName("Even Thread");
Odd.setName("Odd Thread");
Even.start();
Odd.start();
}
public synchronized void PrintNumbers()
{
System.out.println(Thread.currentThread().getName());
}
}
我得到的输出是 偶数线程 奇线程 线程异常 "Even Thread" 线程异常 "Odd Thread" java.lang.IllegalMonitorStateException
您应该将 notifyAll()
和 wait()
包围在 synchronized (this) {}
内
喜欢:
synchronized (this) {
notifyAll();
}
和
synchronized (this) {
wait();
}
这些方法notify()
,notifyAll()
,wait()
只能被这个对象的监视器的所有者线程调用,所以你必须把它们包围在一个同步块中,正如所说Here the Object Java doc
正确代码如下:
public class MainClass {
public static int number=0;
public static void main(String[] args)
{
MainClass obj = new MainClass();
// Boolean flag= true;
EvenThread evenThread = new EvenThread(obj);
OddThread oddThread = new OddThread(obj);
Thread Even = new Thread(evenThread);
Thread Odd = new Thread(oddThread);
Even.setName("Even Thread");
Odd.setName("Odd Thread");
Even.start();
Odd.start();
}
public synchronized void PrintNumbers()
{
while(number<20)
{
System.out.println(number+"---"+Thread.currentThread().getName());
number++;
notifyAll();
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class OddThread implements Runnable {
MainClass obj;
public OddThread(MainClass obj) {
// TODO Auto-generated constructor stub
this.obj= obj;
}
public void run() {
obj.PrintNumbers();
}
}
public class EvenThread implements Runnable{
MainClass obj;
public EvenThread(MainClass obj)
{
this.obj=obj;
}
public void run() {
int number=0;
obj.PrintNumbers();
}
}