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  
  Thread pools  (Read 3819 times)
0 Members and 2 Guests are viewing this topic.
Offline zammbi

JGO Strike Force
***

Posts: 963
Medals: 9



« on: 2011-02-04 21: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 Neuromancer
****

Posts: 1063
Medals: 30


mooooo


« Reply #1 on: 2011-02-04 21: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 Strike Force
***

Posts: 963
Medals: 9



« Reply #2 on: 2011-02-05 00: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! Go get 'em!
Offline zammbi

JGO Strike Force
***

Posts: 963
Medals: 9



« Reply #3 on: 2011-02-05 22: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

JGO n00b
*

Posts: 4



« Reply #4 on: 2011-02-16 21: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
*****

Posts: 2960
Medals: 37


Monkey for a head


« Reply #5 on: 2011-02-17 04: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
****

Posts: 1477
Medals: 23


I always win!


« Reply #6 on: 2011-02-23 17: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
*****

Posts: 3160
Medals: 196


I'm the King!


« Reply #7 on: 2011-02-23 21: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

Sr. Member
**

Posts: 342
Medals: 18



« Reply #8 on: 2011-02-24 07: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

Offline Addictman

Full Member
**

Posts: 204
Medals: 5


Java games rock!


« Reply #9 on: 2011-02-24 07:22:25 »

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