简体   繁体   English

一个Java可执行文件可以同时运行多个循环吗

[英]Can one java executable run more than one loop at the same time

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. 我使用Eclipse KEPLER [我喜欢Eclipse JUNO版本]警告:我的英语根本不好,请纠正我,或者如果发现无意义的句子,请注意我。

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. 我的问题是:我的一个程序的主循环中有很多循环,并且我注意到一个严重故障,这将导致整个程序每3秒被卡住,然后再次唤醒。 [I would like to send the code, but it includes over 14 classes, so... perhaps I do not send it] [我想发送代码,但是它包含14个以上的类,所以...也许我不发送它]

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] 如果这些“循环”位于“主循环”中,则此“主循环”需要3秒钟才能再次开始[不可接受]

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
    }
}

如果它们滞后于您的主循环,那么在这里多线程是个好用处,这样每个方法都可以在单独的线程上运行并在完成后反馈到主线程,而不会减慢主线程的运行速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 下面的while循环可以运行多次吗? - Is the following while loop can be run more than one time? 如何使多个Android AsyncTask同时运行? - How to make more than one Android AsyncTask run at the same time? JVM是否可以同时运行多个程序? - Is it possible for a JVM to run more than one program at the same time? 如何避免在同一java项目上同时运行多个实例? - how can i avoid running of more than one instance on same java project at the same time? 如何使用Spring + Hibernate开发可同时在多个数据库上运行的应用程序? - How can I develop an application which will run on more than one database at a same time using Spring + Hibernate? 如何同时滚动多个对象? - How can I scroll more than one object at the same time? Java中有多个具有相同ResultSet的jTable - More than one jTable with the same ResultSet in Java 同一文件中有多个Java类 - More than one java class in the same file 我可以同时运行 2 个线程并在 java 上设置一个结果吗? - Can I run 2 threads same time and set one Result on java? Java同步:如果将Java中的多个方法设为原子/同步,那么是否同时将锁定应用于所有这些方法? - Java synchronization: If more than one methods in java are made atomic/synchronized, is the lock applied to all of them at the same time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM