Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (407)
games submitted by our members
Games in WIP (293)
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 4167 times)
0 Members and 1 Guest are viewing this topic.
Offline zammbi

JGO Coder


Medals: 4



« Posted 2011-02-05 03: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 Wizard


Medals: 81
Projects: 3


Esoteric Software


« Reply #1 - Posted 2011-02-05 03: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 06: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 04: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 03: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: 48
Projects: 11


Monkey for a head


« Reply #5 - Posted 2011-02-17 10: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 Ninja


Medals: 35
Projects: 5


I always win!


« Reply #6 - Posted 2011-02-23 23: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: 264
Projects: 2


I'm the King!


« Reply #7 - Posted 2011-02-24 03: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 13: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 graphical environment for developing intermedia performance tools, projections and interactive spaces.
Praxis LIVE on Twitter
Offline Addictman

Senior Member


Medals: 3
Projects: 1


Java games rock!


« Reply #9 - Posted 2011-02-24 13: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.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (99 views)
2013-05-17 21:29:12

alaslipknot (107 views)
2013-05-16 21:24:48

gouessej (137 views)
2013-05-16 00:53:38

gouessej (132 views)
2013-05-16 00:17:58

theagentd (144 views)
2013-05-15 15:01:13

theagentd (130 views)
2013-05-15 15:00:54

StreetDoggy (173 views)
2013-05-14 15:56:26

kutucuk (195 views)
2013-05-12 17:10:36

kutucuk (197 views)
2013-05-12 15:36:09

UnluckyDevil (203 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.21 seconds with 21 queries.