From f4de6c2332080e7008dfb1790f6e324b066bc7bf Mon Sep 17 00:00:00 2001 From: Martin Wiesner Date: Tue, 3 Dec 2024 07:32:08 +0100 Subject: [PATCH] OPENNLP-1662: Wrap thread-safe classes in try-with resources in Eval test (#705) --- .../tools/eval/MultiThreadedToolsEval.java | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/opennlp-tools/src/test/java/opennlp/tools/eval/MultiThreadedToolsEval.java b/opennlp-tools/src/test/java/opennlp/tools/eval/MultiThreadedToolsEval.java index fcb2bfa90..96d33f7b0 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/eval/MultiThreadedToolsEval.java +++ b/opennlp-tools/src/test/java/opennlp/tools/eval/MultiThreadedToolsEval.java @@ -22,6 +22,7 @@ import org.junit.jupiter.api.Test; +import opennlp.tools.commons.ThreadSafe; import opennlp.tools.postag.POSModel; import opennlp.tools.postag.ThreadSafePOSTaggerME; import opennlp.tools.sentdetect.SentenceModel; @@ -33,37 +34,37 @@ /** * Test the reentrant tools implementations are really thread safe by running concurrently. * Replace the thread-safe versions with the non-safe versions to see this test case fail. + * + * @see ThreadSafe */ public class MultiThreadedToolsEval extends AbstractEvalTest { @Test public void runMEToolsMultiThreaded() throws IOException, InterruptedException { - File sModelFile = new File(getOpennlpDataDir(), "models-sf/en-sent.bin"); + File dataDir = getOpennlpDataDir(); + File sModelFile = new File(dataDir, "models-sf/en-sent.bin"); + File tModelFile = new File(dataDir, "models-sf/en-token.bin"); + File pModelFile = new File(dataDir, "models-sf/en-pos-maxent.bin"); SentenceModel sModel = new SentenceModel(sModelFile); - ThreadSafeSentenceDetectorME sentencer = new ThreadSafeSentenceDetectorME(sModel); - - File tModelFile = new File(getOpennlpDataDir(), "models-sf/en-token.bin"); TokenizerModel tModel = new TokenizerModel(tModelFile); - ThreadSafeTokenizerME tokenizer = new ThreadSafeTokenizerME(tModel); - - File pModelFile = new File(getOpennlpDataDir(), "models-sf/en-pos-maxent.bin"); POSModel pModel = new POSModel(pModelFile); - ThreadSafePOSTaggerME tagger = new ThreadSafePOSTaggerME(pModel); - final String text = "All human beings are born free and equal in dignity and rights. They " + - "are endowed with reason and conscience and should act towards one another in a " + - "spirit of brotherhood."; + try (ThreadSafeSentenceDetectorME sentencer = new ThreadSafeSentenceDetectorME(sModel); + ThreadSafeTokenizerME tokenizer = new ThreadSafeTokenizerME(tModel); + ThreadSafePOSTaggerME tagger = new ThreadSafePOSTaggerME(pModel)) { - // Run numThreads threads, each processing the sample text numRunsPerThread times. - final int numThreads = 8; - final int numRunsPerThread = 1000; - Thread[] threads = new Thread[numThreads]; + final String text = "All human beings are born free and equal in dignity and rights. They " + + "are endowed with reason and conscience and should act towards one another in a " + + "spirit of brotherhood."; - for (int i = 0; i < 8; i++) { - threads[i] = new Thread(new Runnable() { - @Override - public void run() { + // Run numThreads threads, each processing the sample text numRunsPerThread times. + final int numThreads = 8; + final int numRunsPerThread = 1000; + Thread[] threads = new Thread[numThreads]; + + for (int i = 0; i < 8; i++) { + threads[i] = new Thread(() -> { for (int j = 0; j < numRunsPerThread; j++) { Span[] sentences = sentencer.sentPosDetect(text); for (Span span : sentences) { @@ -72,19 +73,18 @@ public void run() { String[] tokenStrings = new String[tokens.length]; for (int k = 0; k < tokens.length; k++) { tokenStrings[k] = sentence.substring(tokens[k].getStart(), - tokens[k].getEnd()); + tokens[k].getEnd()); } String[] tags = tagger.tag(tokenStrings); } } - } - }); - threads[i].start(); - } - for (Thread t : threads) { - t.join(); + }); + threads[i].start(); + } + for (Thread t : threads) { + t.join(); + } } - } }