0
我第一次使用wait和notify()方法,我嘗試了這種方式。等待,並通知條件Java
示例代碼
public class Tester
{
static boolean closing == false;
static int n;
DateTime dt = new DateTime();
int getMin = dt.getMinuteOfDay(); //minute of the day
int tempMin = dt.getMinuteOfDay()+5;// minute of the day + 5 minutes
public static void setClosing(boolean b)
{
closing = b;
}
public static int getN()
{
return n;
}
class notifier extends Thread
{
public void run()
{
synchronized(this)
{
while(getMin == tempMin || closing == true)
{
notify();
}
}
}
}
public void starter() throws InterruptedException
{
notifier nn = new notifier();
while(n==1)
{
notify.start();
if(closing == false)
{
synchronized(notify)
{
nn.wait();
mailreader();
getMin = getMin+5;
tempMin = tempMin+5;
}
}
else
{
n=2;
}
}
}
}
主類
public class Tester2 extends WindowAdapter
{
public Tester2()
{
frame = new JFrame();
frame.addWindowListener(this);
frame.setVisible(true);
Tester t = new Tester();
t.starter();
}
@Override
public void windowClosing(WindowEvent e)
{
Tester.setClosing(true);
while(Tester.getN() != 2)
{
//wait
}
frame.dispose();
}
public static void main(String args[])
{
Tester2 t = new Tester2();
}
}
我打電話郵件閱讀()方法爲每5分鐘執行一些任務,但
當用戶關閉的JFrame的正在關閉設置爲從主類真正我想出來的while循環中通知人類。
我想在這裏做的是,當用戶關閉JFrame時,我不想讓mailreader()在中間停止並退出,而是我希望JFrame等到方法結束後再執行關閉或如果它是等待模式(通知類),我想出來它並退出