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等到方法结束后再执行关闭或如果它是等待模式(通知类),我想出来它并退出