Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. ExecutorService queue = new ThreadPoolExecutor(2,
  2. 4,
  3. 400,
  4. TimeUnit.MILLISECONDS,
  5. new ArrayBlockingQueue<Runnable>(250, true),
  6. new ThreadPoolExecutor.DiscardOldestPolicy());
  7. Runnable r = new Runnable() {
  8. @Override
  9. public void run() {
  10. try {
  11. if(isTimeForLongRunningExecution()){
  12. //Is it possible to start this job asynchronous without starting to many threads
  13. doLongRunningTask();
  14. }//do job with old data until new data is available, but do not block this operation too long
  15. doNormalTaskWithOldOrNewData();
  16. } catch (InterruptedException e) {
  17. e.printStackTrace();
  18. }
  19. }
  20.  
  21. private void doNormalTaskWithOldOrNewData() throws InterruptedException {
  22. if(checkNewDataAvailable()){
  23. System.out.println("Work with new Data");
  24. Thread.sleep(300L);
  25. System.out.println("Work completed!");
  26. }else{
  27. System.out.println("Work with old Data");
  28. Thread.sleep(300L);
  29. System.out.println("Work completed!");
  30. }
  31. }
  32.  
  33. private void doLongRunningTask() throws InterruptedException {
  34. System.out.println("Create new Data");
  35. Thread.sleep(3*60*1000L);
  36. System.out.println("Data creation completed!");
  37. }
  38. };//end of runnable
  39.  
  40. while(true){
  41. for (int i = 0; i < 60; i++) {
  42. queue.submit(r);
  43. }
  44. Thread.sleep(1000);
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement