I need some help to understand where my program is going wrong, I have a very simple program to learn about multithreading but every time I run the following code it gives me an IllegalStateMonitorException. I don't know what's causing this although I suspect it may be my synchronized block, Thanks.
Main Method class:
public class Lab8 {
public static void main(String [] args) {
Thread1 thread1 = new Thread1();
thread1.start();
}
}
Thread 1:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Thread1 extends Thread {
public DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
public Thread1() {
super("Thread1");
}
public void run() {
Thread2 thread2 = new Thread2();
System.out.println("==Start: " + dateFormat.format(new Date()) + "==\n");
synchronized(thread2) {
try {
this.wait();
} catch (InterruptedException e) {
System.err.println(e.toString());
}
(new Thread(thread2)).start();
}
System.out.println("==End: " + dateFormat.format(new Date()) + "==\n");
}
}
Thread 2:
public class Thread2 implements Runnable {
@Override
public void run() {
synchronized(this) {
for(int i = 1; i <= 100; i++) {
System.out.print(i + " ");
if(i % 10 == 0) {
System.out.print("\n");
}
}
notify();
}
}
}