확장 성이 높아야하는 너무 많은 쿼리를 시작하지 않으려면 쿼리를 시작하기 전에 주어진 시간 동안 대기하는 메커니즘을 구현하는 것이 좋습니다. 사용자가이 기간 동안 필드의 내용을 수정할 때마다 이전 쿼리를 중단하고 새 쿼리를 예약합니다. 그런
뭔가 :
코드 타이머를 생성하고 작업을 예약 : 이전 예약 된 작업 취소
Timer timer = new Timer();
// Schedule my task to be executed in 200 milliseconds
timer.schedule(new TimerTask() {
@Override
public void run() {
// Launch my query here
}
}, 200L);
코드 :은 (언제든지 발사 할 사용자 수정 뭔가)
// Cancel the previous timer which will also abort the scheduled task
timer.cancel();
// Create a new timer
timer = new Timer();
// Re-schedule the task
timer.schedule(new TimerTask() {
@Override
public void run() {
// Launch my query here
}
}, 200L);
이전 예약 된 작업 취소
// Create the ScheduledExecutorService
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
// Submit the task to be executed in 200 milliseconds
ScheduledFuture<?> future = executor.schedule(new Runnable() {
@Override
public void run() {
// Launch my query here
}
}, 200, TimeUnit.MILLISECONDS);
코드 :로 다음 : ScheduledExecutorService
를 생성하고 작업을 예약
코드는
(사용자가 수정 언제든지 뭔가를 시작한다) // Cancel the task which will interrupt the thread that was executing the
// task if any
future.cancel(true);
// Re-submit the task
future = executor.schedule(new Callable<Void>() {
@Override
public Void call() throws InterruptedException {
...
// Check regularly in your code if the thread has been
// interrupted and if so throws an exception to stop
// the task immediately
if (Thread.currentThread().isInterrupted()) {
throw new InterruptedException("Thread interrupted");
}
...
}
}, 200, TimeUnit.MILLISECONDS);
NB : 이러한 코드 스 니펫은 아이디어를 나타 내기위한 것일 뿐이며, 개미가 완벽해야합니다.
UI (모바일 또는 웹) 란 무엇입니까? –
당신은 본질적으로 이미 필터링 된 결과의 큐를 업데이트하는 태스크를 사용할 수 있습니다. 그런 다음 "기본 검색 자"가 최신 쿼리를 기반으로 대기열에 새 서버를 배치하는 동안 해당 검색 결과를 기반으로 정제하면됩니다. – Rogue
@ShlomiHaver 모바일입니다. 안드로이드 –