I use Eclipse KEPLER [I loved Eclipse JUNO version] Warning: My English is not good at all, please correct me or notice me if senseless sentences found.
My problem is: I have a program which have a lot of loops inside of main loop and I noticed a critical failure, that will cause the whole program to get jammed for every 3 seconds and then it wake up again. [I would like to send the code, but it includes over 14 classes, so... perhaps I do not send it]
The problem is caused by loop(s), which take too long time to finish. If these "loops" are in "main loop" this "main loop" takes 3 seconds to start again [which is not acceptable]
Because mount of code, I wrote an example of my problem and it perhaps include possible solution, but I don't know how to do it:
public class EX1 {
static public String w=""; // something where program can storage stuff
static public String alp="AaÁáÂâÄäÃãÅåÀàBbCcDdEeÉéÊêËëÈèFfGgHhIiÍíÎîÏïÌìJjKkLlMmNnÑñOoÓóÔôÖöÕõÒòPpQqRrSsTtUuÚúÛûÜüÙùVvWwXxYyÝýÿZz1234567890 ";
public static void StuffThatSupposeToBeAlwaysOn(){ // As I told here is loop that suppose to be running all time
int rand=0; // in this example, this loop works as random text generator
for(int a=0;a<100;a++){
rand=(int)(Math.random()*alp.length()-1);
w=w+(alp.substring(rand,rand+1));
}
}
public static void StuffThatSupposeToBeAlwaysOn2(){ //this suppose to be another same time running loop
/*
* printed String w should always be 16 letters long
* but because program run both loops one by one, it simply can't print it right length (16)
* so it print it as max length of loop (100)
*/
for(int a=0;a<50;a++){
if(w.length()>15){
System.out.println("Randomly generated text: "+w+". length of text is: "+w.length());
w="";
}
}
}
public static void main(String[] args) {// main program
long a=System.currentTimeMillis();
while(a+2000>System.currentTimeMillis()){ //Main loop[automated kill after 2 seconds]
StuffThatSupposeToBeAlwaysOn(); // so if I get both running at same time, problem is solved.
StuffThatSupposeToBeAlwaysOn2();
}System.exit(0);//program gets killed here
}
}
如果它们滞后于您的主循环,那么在这里多线程是个好用处,这样每个方法都可以在单独的线程上运行并在完成后反馈到主线程,而不会减慢主线程的运行速度。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.