Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ExecutorService queue = new ThreadPoolExecutor(2,
- 4,
- 400,
- TimeUnit.MILLISECONDS,
- new ArrayBlockingQueue<Runnable>(250, true),
- new ThreadPoolExecutor.DiscardOldestPolicy());
- Runnable r = new Runnable() {
- @Override
- public void run() {
- try {
- if(isTimeForLongRunningExecution()){
- //Is it possible to start this job asynchronous without starting to many threads
- doLongRunningTask();
- }//do job with old data until new data is available, but do not block this operation too long
- doNormalTaskWithOldOrNewData();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- private void doNormalTaskWithOldOrNewData() throws InterruptedException {
- if(checkNewDataAvailable()){
- System.out.println("Work with new Data");
- Thread.sleep(300L);
- System.out.println("Work completed!");
- }else{
- System.out.println("Work with old Data");
- Thread.sleep(300L);
- System.out.println("Work completed!");
- }
- }
- private void doLongRunningTask() throws InterruptedException {
- System.out.println("Create new Data");
- Thread.sleep(3*60*1000L);
- System.out.println("Data creation completed!");
- }
- };//end of runnable
- while(true){
- for (int i = 0; i < 60; i++) {
- queue.submit(r);
- }
- Thread.sleep(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement