L
lonelyplanet999
Hi,
I'm writing a program to make one thread periodically notifying
another regularly.
I tried 2 approaches. Both caused the waiting thead t1 triggered below
runtime exception:
I would like to know what's wrong with the coding & any solution to
make the waiting happen
Approach 1
==========
public class Waiting6 {
public static void main (String [] args) {
System.out.println("Program starts");
MyThread2 t2 = new MyThread2();
MyThread1 t1 = new MyThread1(t2);
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
}
class MyThread1 extends Thread {
MyThread2 t2;
MyThread1 (MyThread2 t2) {
this.t2 = t2;
}
public void run() {
String name = Thread.currentThread().getName();
while (true) {
System.out.println(name+" is running...");
synchronized(this) {
try {
t2.wait();
} catch(InterruptedException e) {}
}
}
}
}
class MyThread2 extends Thread {
public void run() {
String name = Thread.currentThread().getName();
while (true) {
System.out.println(name+" is running***");
try {
System.out.println(name+" is sleeping...");
Thread.sleep(2000);
} catch(InterruptedException e) {}
synchronized(this) {
System.out.println(name+" will notify waiting thread...");
notify();
}
}
}
}
Runtime exception triggered
===========================
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:426)
at MyThread1.run(Waiting7.java:25)
Approach 2
==========
public class Waiting7 {
public static void main (String [] args) {
System.out.println("Program starts");
MyThread2 t2 = new MyThread2();
MyThread1 t1 = new MyThread1();
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
}
class MyThread1 extends Thread {
MyThread2 t2 = new MyThread2();
public void run() {
String name = Thread.currentThread().getName();
while (true) {
System.out.println(name+" is running...");
synchronized(this) {
try {
t2.wait();
} catch(InterruptedException e) {}
}
}
}
}
class MyThread2 extends Thread {
public void run() {
String name = Thread.currentThread().getName();
while (true) {
System.out.println(name+" is running***");
try {
System.out.println(name+" is sleeping...");
Thread.sleep(2000);
} catch(InterruptedException e) {}
synchronized(this) {
System.out.println(name+" will notify waiting thread...");
notify();
}
}
}
}
Runtime exception triggered
===========================
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:426)
at MyThread1.run(Waiting7.java:25)
I'm writing a program to make one thread periodically notifying
another regularly.
I tried 2 approaches. Both caused the waiting thead t1 triggered below
runtime exception:
I would like to know what's wrong with the coding & any solution to
make the waiting happen
Approach 1
==========
public class Waiting6 {
public static void main (String [] args) {
System.out.println("Program starts");
MyThread2 t2 = new MyThread2();
MyThread1 t1 = new MyThread1(t2);
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
}
class MyThread1 extends Thread {
MyThread2 t2;
MyThread1 (MyThread2 t2) {
this.t2 = t2;
}
public void run() {
String name = Thread.currentThread().getName();
while (true) {
System.out.println(name+" is running...");
synchronized(this) {
try {
t2.wait();
} catch(InterruptedException e) {}
}
}
}
}
class MyThread2 extends Thread {
public void run() {
String name = Thread.currentThread().getName();
while (true) {
System.out.println(name+" is running***");
try {
System.out.println(name+" is sleeping...");
Thread.sleep(2000);
} catch(InterruptedException e) {}
synchronized(this) {
System.out.println(name+" will notify waiting thread...");
notify();
}
}
}
}
Runtime exception triggered
===========================
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:426)
at MyThread1.run(Waiting7.java:25)
Approach 2
==========
public class Waiting7 {
public static void main (String [] args) {
System.out.println("Program starts");
MyThread2 t2 = new MyThread2();
MyThread1 t1 = new MyThread1();
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
}
class MyThread1 extends Thread {
MyThread2 t2 = new MyThread2();
public void run() {
String name = Thread.currentThread().getName();
while (true) {
System.out.println(name+" is running...");
synchronized(this) {
try {
t2.wait();
} catch(InterruptedException e) {}
}
}
}
}
class MyThread2 extends Thread {
public void run() {
String name = Thread.currentThread().getName();
while (true) {
System.out.println(name+" is running***");
try {
System.out.println(name+" is sleeping...");
Thread.sleep(2000);
} catch(InterruptedException e) {}
synchronized(this) {
System.out.println(name+" will notify waiting thread...");
notify();
}
}
}
}
Runtime exception triggered
===========================
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:426)
at MyThread1.run(Waiting7.java:25)