killing an infinite loop in java

后端 未结 3 1956

I am using a third-party library to process a large number of data sets. The process very occasionally goes into an infinite loop (or is blocked - don\'t know why and can\'t

3条回答
  •  时光说笑
    2021-01-03 02:51

    Put the call to the library in another thread and kill this thread after a timeout. That way you could also proces multiple objects at the same time if they are not dependant to each other.


    EDIT: Democode request

    This is pseudo code so you have to improve and extend it. Also error checking weather a call was succesful or not will be of help.

    for (Object data : dataList) {
        Thread t = new LibThread(data);
        // store the thread somewhere with an id
        // tid and starting time tstart 
        // threads
        t.start();
        }
    
    while(!all threads finished)
    {
        for (Thread t : threads)
        {
            // get start time of thread
            // and check the timeout
            if (runtime > timeout)
            {
                t.stop();
            }
        }
    }
    
    class LibThread extends Thread {
        Object data;
    
        public TextThread(Object data) 
        {
            this.data = data;
        }
    
        public void processData() 
        {
            Object result = TheirLibrary.processData(data);
            store(result);
        }
    }
    

提交回复
热议问题