wait / notify virker ikke?
Jeg har forsøgt at benytted wait() og notify() i forbindelse med en gui, men uden held. Jeg har derefter forsøgt at lave et simpelt eksempel, men stadig uden held. Mine testklasser:public class Thread1 extends Thread {
@Override
public void run()
{
while(true)
{
System.out.println("Thread1 before wait.");
synchronized(this)
{
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Thread1 after wait.");
}
}
}
public class Thread2 extends Thread {
@Override
public void run()
{
while(true)
{
System.out.println("Thread2 before notify.");
System.out.println("Tick tack...");
synchronized(this)
{
notifyAll();
notify();
}
System.out.println("Thread2 after notify.");
}
}
}
public class ThreadTest {
public static void main(String[] args) {
Thread1 t1 = new Thread1();
Thread2 t2 = new Thread2();
t1.start();
t2.start();
}
}
Hvad gør jeg galt?
