2222
2323import org .junit .jupiter .api .Test ;
2424
25+ import opennlp .tools .commons .ThreadSafe ;
2526import opennlp .tools .postag .POSModel ;
2627import opennlp .tools .postag .ThreadSafePOSTaggerME ;
2728import opennlp .tools .sentdetect .SentenceModel ;
3334/**
3435 * Test the reentrant tools implementations are really thread safe by running concurrently.
3536 * Replace the thread-safe versions with the non-safe versions to see this test case fail.
37+ *
38+ * @see ThreadSafe
3639 */
3740public class MultiThreadedToolsEval extends AbstractEvalTest {
3841
3942 @ Test
4043 public void runMEToolsMultiThreaded () throws IOException , InterruptedException {
4144
42- File sModelFile = new File (getOpennlpDataDir (), "models-sf/en-sent.bin" );
45+ File dataDir = getOpennlpDataDir ();
46+ File sModelFile = new File (dataDir , "models-sf/en-sent.bin" );
47+ File tModelFile = new File (dataDir , "models-sf/en-token.bin" );
48+ File pModelFile = new File (dataDir , "models-sf/en-pos-maxent.bin" );
4349 SentenceModel sModel = new SentenceModel (sModelFile );
44- ThreadSafeSentenceDetectorME sentencer = new ThreadSafeSentenceDetectorME (sModel );
45-
46- File tModelFile = new File (getOpennlpDataDir (), "models-sf/en-token.bin" );
4750 TokenizerModel tModel = new TokenizerModel (tModelFile );
48- ThreadSafeTokenizerME tokenizer = new ThreadSafeTokenizerME (tModel );
49-
50- File pModelFile = new File (getOpennlpDataDir (), "models-sf/en-pos-maxent.bin" );
5151 POSModel pModel = new POSModel (pModelFile );
52- ThreadSafePOSTaggerME tagger = new ThreadSafePOSTaggerME (pModel );
5352
54- final String text = "All human beings are born free and equal in dignity and rights. They " +
55- "are endowed with reason and conscience and should act towards one another in a " +
56- "spirit of brotherhood." ;
53+ try ( ThreadSafeSentenceDetectorME sentencer = new ThreadSafeSentenceDetectorME ( sModel );
54+ ThreadSafeTokenizerME tokenizer = new ThreadSafeTokenizerME ( tModel );
55+ ThreadSafePOSTaggerME tagger = new ThreadSafePOSTaggerME ( pModel )) {
5756
58- // Run numThreads threads, each processing the sample text numRunsPerThread times.
59- final int numThreads = 8 ;
60- final int numRunsPerThread = 1000 ;
61- Thread [] threads = new Thread [numThreads ];
57+ final String text = "All human beings are born free and equal in dignity and rights. They " +
58+ "are endowed with reason and conscience and should act towards one another in a " +
59+ "spirit of brotherhood." ;
6260
63- for (int i = 0 ; i < 8 ; i ++) {
64- threads [i ] = new Thread (new Runnable () {
65- @ Override
66- public void run () {
61+ // Run numThreads threads, each processing the sample text numRunsPerThread times.
62+ final int numThreads = 8 ;
63+ final int numRunsPerThread = 1000 ;
64+ Thread [] threads = new Thread [numThreads ];
65+
66+ for (int i = 0 ; i < 8 ; i ++) {
67+ threads [i ] = new Thread (() -> {
6768 for (int j = 0 ; j < numRunsPerThread ; j ++) {
6869 Span [] sentences = sentencer .sentPosDetect (text );
6970 for (Span span : sentences ) {
@@ -72,19 +73,18 @@ public void run() {
7273 String [] tokenStrings = new String [tokens .length ];
7374 for (int k = 0 ; k < tokens .length ; k ++) {
7475 tokenStrings [k ] = sentence .substring (tokens [k ].getStart (),
75- tokens [k ].getEnd ());
76+ tokens [k ].getEnd ());
7677 }
7778 String [] tags = tagger .tag (tokenStrings );
7879 }
7980 }
80- }
81- } );
82- threads [ i ]. start ();
83- }
84- for ( Thread t : threads ) {
85- t . join ();
81+ });
82+ threads [ i ]. start ( );
83+ }
84+ for ( Thread t : threads ) {
85+ t . join ();
86+ }
8687 }
87-
8888 }
8989
9090}
0 commit comments