我正在编写以下代码来从文件中读取条目,并在多线程环境中检查它们是否是回文。但不知何故,代码工作到特定限制。我真的无法弄清楚为什么它没有读取ArrayList中的所有值。任何帮助将不胜感激!
Palindromeworker.java
package Palindrome;
import javax.xml.crypto.Data;
import java.util.ArrayList;
/**
* Created by aliabbasjaffri on 05/10/15.
*/
public class PalindromeWorker implements Runnable
{
int key;
static int ID = 0;
ArrayList<String> words = new ArrayList<String>();
public PalindromeWorker( int keyWord )
{
ID++;
key = keyWord + 1;
words = DataClass.mapBagOfTasks.get(key);
}
public boolean isPalindrome(String str)
{
return str.equals(new StringBuilder(str).reverse().toString());
}
@Override
public void run()
{
System.out.println("Hello From Thread: " + ID);
if(words != null)
{
for (String word : words)
{
if (isPalindrome(word))
{
if (DataClass.Palindromes.containsKey(key))
DataClass.Palindromes.get(key).add(word);
else
{
ArrayList<String> arr = new ArrayList<String>();
arr.add(word);
DataClass.Palindromes.put(key, arr);
}
}
}
System.out.println("Done from Thread: " + ID);
}
}
}
PalindromeWriter.java
package Palindrome;
import java.io.*;
import java.util.ArrayList;
/**
* Created by aliabbasjaffri on 05/10/15.
*/
public class PalindromeWriter implements Runnable
{
int key;
static int ID = 0;
ArrayList<String> words = new ArrayList<String>();
public PalindromeWriter( int keyWord )
{
ID++;
key = keyWord + 1;
words = DataClass.Palindromes.get( key );
}
@Override
public void run()
{
System.out.println("Hello From Output Thread: " + ID);
try
{
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter( DataClass.outputFileName , true)));
if(words != null)
{
for (String word : words)
{
out.println( word );
}
}
out.println("Done from Thread: " + ID);
out.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
主类
package Main;
import Palindrome.*;
/**
* Created by aliabbasjaffri on 05/10/15.
*/
public class Main
{
public static void main(String[] args)
{
Palindrome obj = new Palindrome();
for (int i = 0; i < 22; i++)
{
try
{
Thread t = new Thread(new PalindromeWorker( i ));
t.start();
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i = 0; i < 22; i++)
{
try
{
new Thread(new PalindromeWriter(i)).start();
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
结果:
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Done from Thread: 1
ii
Done from Thread: 2
AAA
ABA
ala
AMA
ana
bib
bob
bub
CDC
dad
did
DOD
dud
eke
ere
eve
ewe
eye
gag
gig
gog
huh
iii
mum
non
nun
pap
PDP
pep
pip
pop
pup
sis
s's
tat
tit
TNT
tot
wow
Done from Thread: 3
deed
noon
peep
poop
teet
toot
Done from Thread: 4
civic
level
madam
minim
radar
refer
rever
rotor
tenet
Done from Thread: 5
Done from Thread: 6
Done from Thread: 7
Done from Thread: 8
Done from Thread: 9
Done from Thread: 10
Done from Thread: 11
Done from Thread: 12
Done from Thread: 13
Done from Thread: 14
Done from Thread: 15
Done from Thread: 16
Done from Thread: 17
Done from Thread: 18
Done from Thread: 19
Done from Thread: 20
Done from Thread: 21
Done from Thread: 22