在给定时间后跳转到带有线程的for循环中的下一个索引

时间:2016-08-02 15:02:24

标签: java multithreading for-loop

我试图创建一个从文件.txt创建词汇表的程序。它需要一个文件,创建一个文档(一个字符串),在句子中分割该文档,最后,对于每个句子,它做一些操作(标记化,pos标记,成本树解析等)。

有时候程序停在句子上(对我来说不好),所以我要做的是避免在句子上停留一段时间(让我们假设120秒)。 如果是这样,程序应该转到列表中的下一个句子。

我想使用一个线程和一个计时器并检查是否通过了该时间,但我对如何使用该线程感到困惑。

这是我的部分代码:

public class Vocabulary {
    private Thread thread;
    private long sentenceDuration;
    private Queue<File> corpus;

    public Vocabulary(){
        this.sentenceDuration = 0;           
        corpus = loadCorpus();
    }

    public void buildVocabulary(){

        for (File f : corpus) {
                Scanner in = new Scanner(new FileReader(f.getPath()));

                // Create a string representing the document
                String document = "";
                while (in.hasNextLine()) {
                    document += in.nextLine() + " ";
                }
                in.close();

                // Split the document in sentences
                ArrayList<String> sentences = tpr.documentToSentences(document);

                for (int i = 0; i < sentences.size(); i++) {
                    Timer timer = new Timer(1000, timer());
                    timer.start();

                    while(sentenceDuration < 120){      // while it takes under 120 seconds to compute the sentence, ok.

                        List<CoreLabel> tokens = tpr.tokenize(sentences.get(i));        // tokenize the sentence
                        Tree parse = tpr.apply(tokens);                                 // create the costituents tree
                        Object[] basicDependencies = tpr.getBasicDependencies(parse);   // get the basic dependencies from the tree

                        // some operations here...             

                        Thread.sleep(1000);     // 1000 ms
                    }

                    // when it takes over 120 seconds to compute the sentence, jump to the next sentence 
                    System.out.println("Hey, I took long time to compute the sentence. I'm going to the next one!");
                }  

                // Other operations here...
        }
    }

    public void start() {
        end();
        this.thread = new Thread(this);
        this.thread.start();
    }

    public void end() {
       if (thread != null && thread.isAlive()) {
            thread.interrupt();
        }
    }

    private ActionListener timer() {

        ActionListener taskPerformer = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                sentenceDuration++;
            }
        };
        return taskPerformer
    }   
}

主要是我打电话:

 public static void main(String[] args) {
        new Vocabulary().start();     
 }

我怎么能对节目说:&#34;如果120s通过,跳过句子!&#34;。也就是说,&#34;立即退出while循环!无论你做什么操作。&#34; ?

谢谢!

2 个答案:

答案 0 :(得分:1)

好的,所以抓住Semaphore,一个简单的join()会做。类似的东西:

// Split the document in sentences
ArrayList<String> sentences = tpr.documentToSentences(document);

for (int i = 0; i < sentences.size(); i++) {
    SentenceProcessorThread sentenceProcessorThread = new SentenceProcessorThread(sentences.get(i));
    sentenceProcessorThread.start();
    try {
        sentenceProcessorThread.join(120000); // your timeout period goes here
        if (sentenceProcessorThread.isAlive()) {
            sentenceProcessorThread.interrupt();
            System.out.println("aborting thread");
        }
    } catch (InterruptedException x) {
    }
}

将句子处理逻辑放在它自己的主题中:

class SentenceProcessorThread extends Thread {
    private String sentence;

    public SentenceProcessorThread(String sentence) {
        this.sentence = sentence;
    }

    public void run() {
        try {
            // your sentence processing logic goes here
        } catch (InterruptedException x) {
        }
    }
}

答案 1 :(得分:0)

最简单且不易出错的方法是将初始时间保留在变量中,如果下次超过120秒,则定期检查代码:

for (int i = 0; i < sentences.size(); i++) {
    // initial time
    long initialTime = System.currentTimeMillis();

    List<CoreLabel> tokens = tpr.tokenize(sentences.get(i));
    // Check if more than 120 sec have elapased
    if (System.currentTimeMillis() - initialTime > 120_000L) {
        System.out.println(
            "Hey, It took long time to compute the sentence. I'm going to the next one!"
        );
        continue;
    }
    ...