public class T00 {
volatile List<String> list = new ArrayList<>();
void op() {
System.out.printf("%s 启动计算中 %n",
Thread.currentThread().getName());
String[] o = new String[]{"", "+", "-"};
Random rand = new Random();
StringBuffer str = new StringBuffer("1");
Pattern p = Pattern.compile("(\\d+|-\\d+)");
while (list.size()!=11) {
for (int i = 2; i < 10; i++)
str.append(String.format("%s%d",
o[rand.nextInt(o.length)], i));
String s = str.toString();
Matcher m = p.matcher(s);
List<Integer> ln = new ArrayList<>();
while (m.find()) {
ln.add(Integer.parseInt(m.group()));
}
int sum = ln.stream().reduce(0, Integer::sum);
if (sum == 100 && !list.contains(s)) {
list.add(s);
System.out.printf("[%s]:%s = 100%n",
Thread.currentThread().getName(), s);
} else {
str.delete(1, str.length());
}
}
System.out.printf("%s 结束 %n",
Thread.currentThread().getName());
}
public static void main(String[] args) {
var t = new T00();
for (int i = 0; i < 15; i++)
new Thread(t::op, "T" + i).start();
}
}