Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (580)
Games in Android Showcase (162)
games submitted by our members
Games in WIP (632)
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  
  threadpool  (Read 978 times)
0 Members and 1 Guest are viewing this topic.
Offline darkjava65

Senior Newbie

« Posted 2011-06-17 16:54:21 »

I can't figure out how to fix the errors.
import java.util.LinkedList;

    A thread pool is a group of a limited number of threads that
    are used to execute tasks.
public class ThreadPool extends ThreadGroup {

    private boolean isAlive;
    private LinkedList taskQueue;
    private int threadID;
    private static int threadPoolID;

        Creates a new ThreadPool.
        @param numThreads The number of threads in the pool.
    public ThreadPool(int numThreads) {
        super("ThreadPool-" + (threadPoolID++));

        isAlive = true;

        taskQueue = new LinkedList();
        for (int i=0; i<numThreads; i++) {
            new PooledThread().start();

        Requests a new task to run. This method returns
        immediately, and the task executes on the next available
        idle thread in this ThreadPool.
        <p>Tasks start execution in the order they are received.
        @param task The task to run. If null, no action is taken.
        @throws IllegalStateException if this ThreadPool is
        already closed.
    public synchronized void runTask(Runnable task) {
        if (!isAlive) {
            throw new IllegalStateException();
        if (task != null) {


    protected synchronized Runnable getTask()
        throws InterruptedException
        while (taskQueue.size() == 0) {
            if (!isAlive) {
                return null;
        return (Runnable)taskQueue.removeFirst();

        Closes this ThreadPool and returns immediately. All
        threads are stopped, and any waiting tasks are not
        executed. Once a ThreadPool is closed, no more tasks can
        be run on this ThreadPool.
    public synchronized void close() {
        if (isAlive) {
            isAlive = false;

        Closes this ThreadPool and waits for all running threads
        to finish. Any waiting tasks are executed.
    public void join() {
        // notify all waiting threads that this ThreadPool is no
        // longer alive
        synchronized (this) {
            isAlive = false;

        // wait for all threads to finish
        Thread[] threads = new Thread[activeCount()];
        int count = enumerate(threads);
        for (int i=0; i<count; i++) {
            try {
            catch (InterruptedException ex) { }

        A PooledThread is a Thread in a ThreadPool group, designed
        to run tasks (Runnables).
    private class PooledThread extends Thread {

        public PooledThread() {
                "PooledThread-" + (threadID++));

        public void run() {
            while (!isInterrupted()) {

                // get a task to run
                Runnable task = null;
                try {
                    task = getTask();
                catch (InterruptedException ex) { }

                // if getTask() returned null or was interrupted,
                // close this thread by returning.
                if (task == null) {

                // run the task, and eat any exceptions it throws
                try {
                catch (Throwable t) {
                    uncaughtException(this, t);

I have some java experience but no game developing experience with java. I purchased a book and the first chapter is about threads. so I copied the sample program into my eclipse window and got the following error.

launch error
selection does not contain a main type.

please explain what is wrong with the program.
any advise is welcome.


Senior Newbie

« Reply #1 - Posted 2011-06-17 18:08:19 »

There is no main method in ur code.
Thats what the error says.
Offline darkjava65

Senior Newbie

« Reply #2 - Posted 2011-06-17 19:30:42 »

thanks, I got the program to work now
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel

Medals: 386
Projects: 3
Exp: 5 years

I'm the King!

« Reply #3 - Posted 2011-06-18 00:14:25 »

Please use the [code] enter code here [/code] tags when pasting any code here.

Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Waterwolf (20 views)
2015-05-20 15:01:45

chrislo27 (24 views)
2015-05-20 03:42:21

BurntPizza (58 views)
2015-05-10 15:53:18

FrozenShade (44 views)
2015-05-07 09:11:21

TheLopais (207 views)
2015-05-06 13:36:48

TheLopais (191 views)
2015-05-06 13:35:14

TheLopais (198 views)
2015-05-06 13:33:39

TheLopais (214 views)
2015-05-06 13:32:48

TheLopais (214 views)
2015-05-06 13:31:28

ClaasJG (236 views)
2015-04-30 20:33:25
List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00 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‑
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!