I've a problem with my code. I'm trying to execute two simple GUI frames (just for test purposes) in two threads. I want to wait for a thread using wait()
and to notify using notify()
or notifyAll()
the second thread. I've also used synchronized()
and did not work.
Can anyone explain to me very clear (with an example) what I'm doing wrong ? I'm new to programming and I don't know much about it. Thanks !
Here's my snippet:
public void example5() throws InterruptedException {
Thread t = new Thread() {
public final Object obj = new Object();
public void run() {
synchronized(this) { //coordinating activities and data access among multiple threads
//The mechanism that Java uses to support synchronization is the monitor
try {
obj.wait(3000); //suspendarea thread-ului t / punerea in asteptare
} catch (InterruptedException ex) {
}
JFrame frame = new JFrame();
JButton b = new JButton("CLICK ME 1");
JPanel panel = new JPanel();
panel.add(b); frame.add(panel); frame.setBounds(700, 500, 150, 100); frame.setVisible(true);
}
}
};
}
Thread t1 = new Thread(new Runnable() {
public final Object obj = new Object();
public void run() {
obj.notify();
JFrame frame = new JFrame();
JButton b = new JButton("CLICK ME 2");
JPanel panel = new JPanel();
panel.add(b); frame.add(panel); frame.setBounds(700, 500, 150, 100); frame.setVisible(true);
}
});