Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Order of execution in ExecutorService  (Read 2059 times)
0 Members and 1 Guest are viewing this topic.
Offline zammbi

JGO Strike Force
***

Posts: 963
Medals: 9



« on: 2010-11-21 16:22:08 »

Does anyone know the order of execution in the ExecutorService class?
I'm trying to do some thread pooling but I want it stack(last in first out) based and ExecutorService seems to be queue(first in first out) based in my application.
Does anyone know how to change it without rewriting it? I'm not really finding anything on Google.

Current project - Rename and Sort
Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #1 on: 2010-11-21 16:38:29 »

Pass your own BlockingQueue to the ThreadPoolExecutor constructor

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline zammbi

JGO Strike Force
***

Posts: 963
Medals: 9



« Reply #2 on: 2010-11-21 17:49:53 »

Awesome found what I want:

1  
private transient final ExecutorService threadPool= new ThreadPoolExecutor(3, 10,10, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<Runnable>());

Current project - Rename and Sort
Games published by our own members! Go get 'em!
Offline zammbi

JGO Strike Force
***

Posts: 963
Medals: 9



« Reply #3 on: 2010-11-21 18:20:34 »

Under my tests that doesn't seem to be working either. Ideas on why?

Current project - Rename and Sort
Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #4 on: 2010-11-21 18:32:46 »

I left my crystall ball at the office last friday, so could you please forgive me to ask you what exactly doesn't work? Smiley

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline lhkbob

JGO Neuromancer
****

Posts: 1174
Medals: 35



« Reply #5 on: 2010-11-22 00:13:13 »

Forgive me, but isn't a LinkedBlockingDeque or other BlockingQueue just another queue structure, so it will still use FIFO behavior?

Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #6 on: 2010-11-22 00:34:44 »

Sure, but you can invoke addFirst() instead of add().

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline zammbi

JGO Strike Force
***

Posts: 963
Medals: 9



« Reply #7 on: 2010-11-22 00:51:57 »

Quote
I left my crystall ball at the office last friday, so could you please forgive me to ask you what exactly doesn't work?
Wink Sorry I mean't it wasn't LIFO.

Quote
Sure, but you can invoke addFirst() instead of add().
How do I get the ExecutorService to do this?

Current project - Rename and Sort
Offline nsigma

Sr. Member
**

Posts: 342
Medals: 18



« Reply #8 on: 2010-11-22 07:03:20 »

I guess you could create a LIFO wrapper to the Deque.  There's Collections.asLifoQueue(), but that returns a Queue not a BlockingQueue. 

Or extend ThreadPoolExecutor and override execute() or submit()?

Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #9 on: 2010-11-22 16:37:39 »

How do I get the ExecutorService to do this?
AFAIK, you can simple push your tasks (to the other side) in the queue yourself, ExecutorService will pop the queue automatically.

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Games published by our own members! Go get 'em!
Offline zammbi

JGO Strike Force
***

Posts: 963
Medals: 9



« Reply #10 on: 2010-11-22 16:48:28 »

Quote
AFAIK, you can simple push your tasks (to the other side) in the queue yourself, ExecutorService will pop the queue automatically.
Tried that but that didn't seem to work.

Quote
Or extend ThreadPoolExecutor and override execute() or submit()?
That looked quite hard to get right by looking at the ThreadPoolExecutor source code.

Quote
I guess you could create a LIFO wrapper to the Deque.
This was the easiest option. Thanks.

If anyone else want's to use the hacked up wrapper code :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
package util;

import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;

/**
 * LIFO BlockingQueue to be used with the ExecutorService.
 * @author Daniel
 * @param <T>
 */

public class LinkedBlockingStack<T> implements BlockingQueue<T>{
   private final LinkedBlockingDeque<T> stack = new LinkedBlockingDeque<T>();

   @Override
   public T remove() {
      return stack.remove();
   }

   @Override
   public T poll() {
      return stack.poll();
   }

   @Override
   public T element() {
      return stack.element();
   }

   @Override
   public T peek() {
      return stack.peek();
   }

   @Override
   public int size() {
      return stack.size();
   }

   @Override
   public boolean isEmpty() {
      return stack.isEmpty();
   }

   @Override
   public Iterator<T> iterator() {
      return stack.iterator();
   }

   @Override
   public Object[] toArray() {
      return stack.toArray();
   }

   @Override
   public <S> S[] toArray(final S[] a) {
      return stack.toArray(a);
   }

   @Override
   public boolean containsAll(final Collection<?> c) {
      return stack.containsAll(c);
   }

   @Override
   public boolean addAll(final Collection<? extends T> c) {
      return stack.addAll(c);
   }

   @Override
   public boolean removeAll(final Collection<?> c) {
      return stack.removeAll(c);
   }

   @Override
   public boolean retainAll(final Collection<?> c) {
      return stack.removeAll(c);
   }

   @Override
   public void clear() {
      stack.clear();
   }

   @Override
   public boolean add(final T e) {
      return stack.offerFirst(e); //Used offerFirst instead of add.
  }

   @Override
   public boolean offer(final T e) {
      return stack.offerFirst(e); //Used offerFirst instead of offer.
  }

   @Override
   public void put(final T e) throws InterruptedException {
      stack.put(e);
   }

   @Override
   public boolean offer(final T e, final long timeout, final TimeUnit unit)
   throws InterruptedException {
      return stack.offerLast(e, timeout, unit);
   }

   @Override
   public T take() throws InterruptedException {
      return stack.take();
   }

   @Override
   public T poll(final long timeout, final TimeUnit unit)
   throws InterruptedException {
      return stack.poll();
   }

   @Override
   public int remainingCapacity() {
      return stack.remainingCapacity();
   }

   @Override
   public boolean remove(final Object o) {
      return stack.remove(o);
   }

   @Override
   public boolean contains(final Object o) {
      return stack.contains(o);
   }

   @Override
   public int drainTo(final Collection<? super T> c) {
      return stack.drainTo(c);
   }

   @Override
   public int drainTo(final Collection<? super T> c, final int maxElements) {
      return stack.drainTo(c, maxElements);
   }
}



Current project - Rename and Sort
Offline nsigma

Sr. Member
**

Posts: 342
Medals: 18



« Reply #11 on: 2010-11-24 04:59:55 »

This was the easiest option. Thanks.

If anyone else want's to use the hacked up wrapper code : ...


I've just copy and pasted that to a file in case it's of use in the future.  I think I've found my new approach to coding the boring stuff - I'm just going to suggest them on here and wait a few days for them to appear!  Grin

Offline zammbi

JGO Strike Force
***

Posts: 963
Medals: 9



« Reply #12 on: 2010-11-24 05:23:05 »

Quote
I've just copy and pasted that to a file in case it's of use in the future.  I think I've found my new approach to coding the boring stuff - I'm just going to suggest them on here and wait a few days for them to appear! 
Hehe. It helped my application a lot. I would of thought a stacked based thread pool would be used more but couldn't find anything on Google.

Current project - Rename and Sort
Offline i30817

Full Member
**

Posts: 212



« Reply #13 on: 2010-11-30 07:27:19 »

I have a LIFO executor service hacked up in my private libs. I'm not certain it works as expected (no unit tests  Grin, but i use in my stupid projects).

It is somewhere inside this:
http://code.google.com/p/bookjar-utils/source/browse/BookJar-utils/src/util/threads/Threads.java
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.137 seconds with 20 queries.