Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Order of execution in ExecutorService  (Read 4327 times)
0 Members and 1 Guest are viewing this topic.
Offline zammbi

JGO Coder


Medals: 4



« Posted 2010-11-21 21: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 Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2010-11-21 21: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 Coder


Medals: 4



« Reply #2 - Posted 2010-11-21 22: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! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zammbi

JGO Coder


Medals: 4



« Reply #3 - Posted 2010-11-21 23: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 Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2010-11-21 23: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 Knight


Medals: 32



« Reply #5 - Posted 2010-11-22 05: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 Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2010-11-22 05: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 Coder


Medals: 4



« Reply #7 - Posted 2010-11-22 05: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
Online nsigma
« Reply #8 - Posted 2010-11-22 12: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()?

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2010-11-22 21: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! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zammbi

JGO Coder


Medals: 4



« Reply #10 - Posted 2010-11-22 21: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
Online nsigma
« Reply #11 - Posted 2010-11-24 09: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

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline zammbi

JGO Coder


Medals: 4



« Reply #12 - Posted 2010-11-24 10: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

Junior Member





« Reply #13 - Posted 2010-11-30 12: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]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (30 views)
2014-09-21 02:42:18

BurntPizza (20 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (28 views)
2014-09-20 20:14:06

BurntPizza (32 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!