Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (553)
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  
  Thread pools  (Read 4545 times)
0 Members and 1 Guest are viewing this topic.
Offline zammbi

JGO Coder


Medals: 4



« Posted 2011-02-05 02:07:43 »

I’m working with thread pools and wondering if anyone knows of such thread libraries or example code on the below examples is around. I don’t want to be reinventing the wheel.

A thread pool that will process 1 thread at a time and only a maximum of 1 thread waiting. So if something new comes in, it will remove the old runnable(and therefore never used) but not cancelling any runnables already running.

A thread pool that interrupts a runnable when running too long of X amount of seconds.

Current project - Rename and Sort
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #1 - Posted 2011-02-05 02:11:42 »

Your descriptions are not very clear (eg, "remove the old thread"?). You have seen this?
http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/Executors.html

Offline zammbi

JGO Coder


Medals: 4



« Reply #2 - Posted 2011-02-05 05:01:34 »

Quote
That's what I'm currently using. But they don't offer by default (or not that I know of) of what I want.

Quote
Your descriptions are not very clear (eg, "remove the old thread"?).

Sorry I mean 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 2011-02-06 03:52:21 »

Found a solution for the first one:
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  
package manager;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import util.LinkedBlockingStack;

public class ThreadPoolManager {
   private transient final static LinkedBlockingStack<Runnable> lbs = new LinkedBlockingStack<Runnable>();
   private transient final static ExecutorService threadPoolSmallTask = new ThreadPoolExecutor(3, 10, 1000, TimeUnit.MILLISECONDS,lbs );

   private ThreadPoolManager(){}

   public static void executeStackSmallTask(final Runnable r){
      ThreadPoolManager.threadPoolSmallTask.execute(r);
   }
   
   public static boolean containsRunnable(Runnable r){
      return lbs.contains(r);
   }
   public static void executeStackSmallTaskIfNotSame(final Runnable r){
      if(!containsRunnable(r)){
         executeStackSmallTask(r);
         System.out.println("Not same task");
      }else
         System.out.println("Same task");
   }
}


Current project - Rename and Sort
Offline doravan

Junior Newbie





« Reply #4 - Posted 2011-02-17 02:15:00 »

It's very very simple

Try using Singletons and getInstance() menthods.



1  
2  
3  
4  
5  
6  
7  
private static final class SingletonHolder {
        private static final ThreadPoolManager INSTANCE = new ThreadPoolManager();
}

public static ThreadPoolManager getInstance() {
        return SingletonHolder.INSTANCE;
}
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #5 - Posted 2011-02-17 09:32:52 »

It's very very simple

Try using Singletons and getInstance() menthods.



1  
2  
3  
4  
5  
6  
7  
private static final class SingletonHolder {
        private static final ThreadPoolManager INSTANCE = new ThreadPoolManager();
}

public static ThreadPoolManager getInstance() {
        return SingletonHolder.INSTANCE;
}

This has to be the worst advice I've seen on here for a long time.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline appel

JGO Wizard


Medals: 50
Projects: 4


I always win!


« Reply #6 - Posted 2011-02-23 22:33:26 »

You have to be very explicit and clear when it comes to multithreading, and it's best not to ask a question based on an vague idea you have of implementation, but rather explain your problem in words.

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2011-02-24 02:28:52 »

It's very very simple

Try using Singletons and getInstance() menthods.



1  
2  
3  
4  
5  
6  
7  
private static final class SingletonHolder {
        private static final ThreadPoolManager INSTANCE = new ThreadPoolManager();
}

public static ThreadPoolManager getInstance() {
        return SingletonHolder.INSTANCE;
}

This has to be the worst advice I've seen on here for a long time.
OUCH! Grin
This made me laugh so hard, I had to take a breather Tongue

Offline nsigma
« Reply #8 - Posted 2011-02-24 12:18:19 »

I've been toying with a very similar problem recently in my Praxis project.  Praxis has a live compiler which allows the user to run fragments of live code in the video pipeline.  Useful, and fun to play with, but I'd like to have a 'sandboxed' mode that doesn't bring the video pipeline juddering to a halt if you pass in some dodgy code.

SO ... best I've thought of so far would be to use submit() instead of execute() on the ExecutorService, and have a second monitoring thread that calls get(timeout...) on the returned Future.  If it times out, you can then call cancel(true) on the Future to interrupt the task, but it does depend on the code handling interrupts gracefully.  If you've got control of the tasks, that shouldn't be a problem.  If it's code you can't control (like me), handling things like hard loops and clearing up after a misbehaving Runnable, seem harder to implement.  Thread.stop() anyone???  Grin

Hope that maybe helps you anyway.


It's very very simple

Try using Singletons and getInstance() menthods.
This has to be the worst advice I've seen on here for a long time.

hmm ... but hidden in full view within this "advice", which yes doesn't touch the question ... the idea of making the Manager a singleton I'd recommend, particularly with a getDefault() like factory method.  Allows you to easily inject different implementations for different purposes / testing.

Best wishes, Neil

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

Senior Member


Medals: 3
Projects: 1


Java games rock!


« Reply #9 - Posted 2011-02-24 12:22:25 »

I'm still not sure I understand the initial question properly.
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.

CopyableCougar4 (23 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (42 views)
2014-08-06 19:49:38
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!