I`ve just came up with a solution for a program I am working on - it involves databasse querying, string checking, sortinge and other stuff, but everything is supposed to be on separate threads. So what I`ve invented is that code:

Function.jaava:
Java Code:
public interface Function() {

   public void fun();
}
Tasker.java:
Java Code:
import java.util.Map;
import java.util.HashMap;

public class Tasker {
    public static int item;
    
    public static class Task {
        private static volatile boolean isRunning = false;
        private static volatile boolean runner = true;
        private Task() { 
        	
        	
        }
        
        //disable for now
        public static synchronized void init(final Function f) {
            try {
            	new Thread(new Runnable() {
            		@Override
            		public void run() {
            		while ( runner ) {
            		System.out.println("Inside run()");
            			while ( isRunning() ) {
            				f.fun(); 
            				}
            			} 
            		} 
            	}).start();
            	
            } catch (Exception ex) { }
         }
        
        public static  synchronized boolean isRunning() {
        		return isRunning;
        		}
        public static synchronized void requestStop() {
        	runner = true;
        	isRunning = false;
        }
        public static synchronized void kill() {
        	isRunning = false;
        	runner = false;
        	}
        public static  synchronized void requestStart() {
        	isRunning = true;
        	}
        
        public static Task getInstance() {
            return new Task();
            }
    } //~ Close Task class 
    
    // Tasker methods
    private final static HashMap<String, Task> threads = 
                    new HashMap<String, Task> ();
     
    public static HashMap<String, Task> getThreads() {
            return threads;
            }
    //improved lazy method
    /* yet to be tested */
    public static Task getThreads(String name) {
    	if ( threads.get(name) == null ) return null;
    	else return threads.get(name);
    	}
    
    public static void newThread(String name) {
        if ( threads.get(name) == null ) {
            threads.put(name, Task.getInstance() );
            }
        else {
            System.out.println("There is a task with name: "+name);
            return;
            }
        }
     
    public static void main(String[] args) {
 		Tasker.newThread("countTo100");
 		item = 20;
 		System.out.println("Starting initial");
 		Tasker.getThreads("countTo100").init(new Function() {
 				@Override
 				public void fun() {
					if ( item > 200 ) Tasker.getThreads("countTo100").kill();
					for (int i=0; i < 10; i++ ) System.out.print(i);
					System.out.println();
					item++;
					
 				}
			});
		
		//Tasker.getThreads("countTo100").requestStop();
		Tasker.getThreads("countTo100").requestStart();
		
	} // END OF MAIN  
                 
}
The core idea is in init() method, there is one runnable that infinitely checks if the method in the run will be requested to start, also checks if the method will be requested to stop. To stop the whole run() you calll the kill method. You can compile and review my class and if you can tell me if it`s ready to go? I`ll test it tomorrow, replacing that code with the previous not that versatile one.