문제

I have a single threaded executor service for fetching some data over the network.

As the user is typing in a search box I am enqueuing possible network tasks. What I want is to cancel all previous requests and only enqueue and immediately run the latest one.

My current approach is to override execute() and submit() methods and clear the queue before calling super.

Any thoughts on this?

도움이 되었습니까?

해결책

Don't get it, why don't you save the Future returned on posting a callable to the service, and then cancel() the future if you don't want it to be executed.

e.g.

Future f1 = service.submit(some_task);

// later

f1.cancel(true); // will interrupt if running...

Cleaner IMO...

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top