为了避免主线程退出导致其它还未执行完的线程退出,可以使用ExecutorService管理多线程的生命周期
关于类的静态和非静态方法同步的区别(synchronized)
要想同一个类的不同实例的某个方法同步,使用static
ExecutorService exec = Executors.newCachedThreadPool();
for (int i = 0; i < 100; i++) {
exec.execute(new Parser(files));
}
exec.shutdown();
关于类的静态和非静态方法同步的区别(synchronized)
要想同一个类的不同实例的某个方法同步,使用static
public static synchronized File getFile(List<File> files) {
File file = null;
if (files.size() > 0) {
file = files.get(0);
System.out.println(files.size());
((ArrayList<File>)files).remove(0);
}
return file;
}