現在、SSLクローラーで作業していますが、スポーンされたスレッドを削除するには、どの方法が最も安全な方法かと思っています。実際、私はDBからフェッチされた利用可能なURLに基づいてスレッドをフォークします。私は、すべての生成されたスレッドを監視し、URLの可用性に基づいてさらにフォークするかどうかを決定するクラスのスレッドコントローラを持っていますか?スレッドを安全に殺すことに関するアドバイスが必要ですか?ここ Java環境でスレッドを強制終了するには?
は、スレッドコントローラです:public void startThreadsV4() throws InstantiationException, IllegalAccessException {
//creating a pool of maxThreads threads
//ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(maxThreads);
int m = maxThreads - nThreads;
if (availableURLs < m) {
m = availableURLs;
}
System.out.println("Starting "+ m + " threads");
for (int n = 0; n < m; n++) {
ControllableThread thread = (ControllableThread) threadClass.newInstance();
thread.setThreadController(this);
thread.setMessageReceiver(receiver);
thread.setId(nThreads++);
thread.setDbhandler(dbhandler);
thread.setIOhandler(IOhandler);
thread.setIgnoresundomains(ignoresubdomain);
thread.start();
allThreads.add(thread);
}
}
、ここでスレッド・ロジックです:
public void run() {
///first thing is to check if the The thread Controller requested
IOhandler io = new IOhandler();
try {
//Object newTask = dbhandler.getNCURLandMarkTransaction();
Object newTask = dbhandler.getNCURL();
System.out.println("new URL -->"+ newTask);
while (newTask != null) {
if (tc.getStop() == 1) {
return;
} else {
//treatment in case of ignore sub domains
if(ignoresundomains == 1){
String[] temp = new String[2];
temp = io.getHostnamefromURL(newTask.toString());
if(dbhandler.HostExist(temp[0],temp[1])){
return;
}
}
dbhandler.markURLinprogress(newTask.toString());
//notify controler of what we do
mr.receiveMessage(newTask, id);
//process the new task (URL)
System.out.println("Processing" + newTask);
process(newTask, tc.getLastrecord(), tc, dbhandler,IOhandler);
//dec availabe urls
tc.decAvailabeURLs();
dbhandler.markURLcrawled(newTask.toString());
if (tc.getMaxThreads() > tc.getRunningThreads()) {
try {
tc.startThreadsV4();
} catch (InstantiationException | IllegalAccessException ex) {
Logger.getLogger(ControllableThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
//System.out.println("DONE");
//send finished message to controller
tc.finished(id);
newTask = dbhandler.getNCURL();
}
}
} catch (SQLException ex) {
Logger.getLogger(ControllableThread.class.getName()).log(Level.SEVERE, null, ex);
}
}