यहां मैंने अपनी कंपनी के ऐप के लिए क्या किया है, यह कानूनी कारणों से कुछ छद्म कोड है, लेकिन इसका जस्ट यह है कि यदि स्क्रीन अनुत्तरदायी है, तो यह जीयूआई को रीबूट करेगा। जब भी आप ईडीटी को बंद करने के लिए स्विंग यूटिलिटीज का उपयोग करते हैं, उसी उसी ब्लॉक में, दो वॉचर थ्रेड बनाएं। एक धागा स्विंग यूटिलिटीज का उपयोग कर ईडीटी थ्रेड पर बस एक क्रिया करेगा। एक और थ्रेड पहले धागे की निगरानी करेगा यह देखने के लिए कि क्या पहला धागा उत्तरदायी है। पहला धागा केवल प्रतिक्रिया को स्वीकार करेगा यदि यह एक बहुत ही सरल कमांड कर सकता है।
सेट isEDTCheck सच करने के लिए जब सामान्य फैशन में, झूठी डिबग मोड में
if (isEDTCheck) {
new Thread("EDTHeartbeat") {
@Override
public void run() {
Runnable thisThingYouDo = new Runnable() {
public void run() {
int x = 0;
}
};
while (true) {
// first thread says we are waiting, aka bad state
edtwait=true;
try {
javax.swing.SwingUtilities.invokeAndWait(thisThingYouDo);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// first thread says we are not waiting, good state
edtwait=false;
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
new Thread("EDTValidator") {
@Override
public void run() {
while (true) {
// is first thread in bad state?
if (edtwait) {
try {
Thread.sleep(3000);
// after 3 seconds are we still in bad state? if so, get rid of initial frame, pop up a dialog box in AWT that does no commands
if (edtwait) {
mainFrame.setVisible(false);
new Dialog();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
}
public class Dialog extends Frame {
private static final int WIDTH = 400;
private static final int HEIGHT = 300;
Frame f = null;
public Dialog() {
f = this;
hasSomethingBeenEntered=false;
this.setTitle("APP PROBLEM DETECTED");
this.setSize(WIDTH, HEIGHT);
this.setLocation((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth() - myapp.width, 0);
Panel p1 = new Panel() {
@Override
public void paint(final Graphics g) {
int left = Dialog.WIDTH/2 - 45; // don't use WIDTH shadowed by Panel class
int top = Dialog.HEIGHT/2 - 20; // same as above
g.drawString("APP HAS DETECTED A PROBLEM", left, top);
}
};
this.add("Center", p1);
this.setAlwaysOnTop(true);
TextArea tb = new TextArea("APP HAS DETECTED A MAJOR PROBLEM\nIT WILL NOW RESTART IN 5 SECONDS");
this.add(tb);
this.setVisible(true);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
restartApp();
}
private void restartApp() {
Runtime.getRuntime().exec("cmd /c start cmd.exe /K \"cd C:\\Progra~1\\Common~1 && C:\\Progra~1\\Common~1\\MyAppDir\\myjavaapp.jar\"");
System.exit(0);
}
स्रोत
2012-08-02 19:49:21
चल (अन्यथा आप लगातार रिबूट कर देंगे। इसलिए मैं एक वर्ग है कि फैली में भूखंड() कोड को चलाने के लिए करना चाहते हैं SwingWorker, और doInBackground() फ़ंक्शन में उस संगणकीय रूप से भारी कोड चलाएं और फिर मेरे प्रदर्शन को रीफ्रेश करने के लिए किए गए() फ़ंक्शन का उपयोग करें? क्या गणना के दौरान मेरा जीयूआई अभी भी सक्रिय होगा? – smuggledPancakes
@ user464095: हाँ, बिल्कुल। – ColinD
बिंगो। स्विंगवर्कर ऐसा करता है और इसके लिए है। –