Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By JosAH

Thread: Java thread assigner, remover and monitor advice needed.

  1. #1
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Java thread assigner, remover and monitor advice needed.

    Hello,
    I am creating a small class for assigning threads to my program so I can have more control of what must run ant what must not. Here is the example prototype:

    Java Code:
    ObserverTesting.newThread("hello");
    			ObserverTesting.getThreads().get("hello")
    			.init(new Runnable() {
    					@Override
    					public void run() {
    						System.out.println("HELLO");
    						}
    					});
    Can you review it and point me any potential pitfalls or misstakes? The test in main does what it has to, but I still need a good reviewer. Thanks in advance.

  2. #2
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Java thread assigner, remover and monitor advice needed.

    Sorry I can`t edit my post: Here is the right code
    Java Code:
    import java.util.Map;
    import java.util.HashMap;
    
    public class ObserverTesting {
    	
    	private static class Task {
    		public static volatile boolean isRunning = false;
    		private static Thread task;
    		private static Runnable runner;
    		private Task() { }
    		public static void init(Runnable r) {
    			runner = r;
    			isRunning = true;
    			task = new Thread(runner);
    			task.start();
    			
    			}
    		public static Task getInstance() {
    			return new Task();
    			}
    	}
    	private final static HashMap<String, Task> threads = 
    					new HashMap<String, Task> ();
    	
    	public static HashMap<String, Task> getThreads() {
    			return threads;
    			}
    	
    	public static void newThread(String name) {
    		if ( threads.get(name) == null ) {
    			System.out.println("NO task "+name);
    			}
    		else {
    			threads.put(name, Task.getInstance() );
    			}
    		}
    	
    	public static void main(String[] args) {
    	
    			ObserverTesting.newThread("hello");
    			ObserverTesting.getThreads().get("hello")
    			.init(new Runnable() {
    					@Override
    					public void run() {
    						int i=0; 
    						while ( ObserverTesting.getThreads().get("hello").isRunning ) {
    						System.out.println("HELLO "+ i + " times" );
    						i++;
    						}
    						}
    					});
    			ObserverTesting.newThread("stop");
    			ObserverTesting.getThreads().get("stop")
    			.init(new Runnable() {
    					@Override
    					public void run() {
    						System.out.println("stop");
    						ObserverTesting.getThreads().get("hello").isRunning = false;
    						}
    					});
    			
    	}	
    	
    }

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Java thread assigner, remover and monitor advice needed.

    I can't even figure out what you're trying to do.
    Get in the habit of using standard Java naming conventions!

  4. #4
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Java thread assigner, remover and monitor advice needed.

    Well, I didn`t explained it properly. I am trying to create a class that initiates threads, and has access to them so it can stop them at any time, so in another program to avoid new Thread(new Runnable().... in the middle of nothing.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: Java thread assigner, remover and monitor advice needed.

    Have you read the API for the classes in the java.util.concurrent package? There are quite a bit of useful classes in there that can do what you want.

    kind regards,

    Jos
    heatblazer likes this.
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Java thread assigner, remover and monitor advice needed.

    Thanks, I`ll read them. I`ve worked out a bit and it`s doing what it had to: for example I have a thread that infinitely checks for filled fields, then if filled it enables a button and dies. Kind of dies like:
    Java Code:
    Tasker.getThreads().get("addingThread").init(new Runnable() {
    		@Override									
    		public void run() {
                            /* debug purpose only */	
    			Thread.currentThread().setName("addingThread");	
    													
    				while (sname1.getText().equals("") ||
    					fname1.getText().equals("") ||
    					Tasker.getThreads().get("addingThread").isRunning() ) 
    					{
    					                /*keep looping untul both fields are filled */
    							System.out.println("check fields thread is running");
    															
    					}
    					addButton1.setEnabled(true);
    					Tasker.getThreads().get("addingThread").requestStop();
    				}
    });
    However the while loop is not good enough I was thinking changing the logic but later :) Anyway - thanks for the advice, I`ll definitely look ffor it in the optimization code. For now this one works.
    Attached Thumbnails Attached Thumbnails Java thread assigner, remover and monitor advice needed.-screenshot-05122013-08-27-54-pm.png  

  7. #7
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Java thread assigner, remover and monitor advice needed.

    1. You should not be accessing UI components in a helper thread. There is no guarantee that your loop will exit when something is entered in the field. And if it does, there is no guarantee that the write to the button state will be visible to the UI thread. It may work perfectly on one system and completely fail on another. Worse, it may usually work but sometimes fail.

    2. Your helper thread is polling as fast as it can, which will devour your CPU. You could make it poll in a more CPU-friendly way, but really you shouldn't be polling at all. The correct way to do what you're trying to do would be to add some type of listener to your fields and have it enable the button if the content of the fields is valid. The listener will run in the UI thread, so it can safely read and write the state of UI components.
    Get in the habit of using standard Java naming conventions!

  8. #8
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    137
    Rep Power
    0

    Default Re: Java thread assigner, remover and monitor advice needed.

    I`ll take note on that and will leave the GUI thread to handle the adding frame. OK, I`ll just add some textfields listeners... So, I`ll jsut work threads in non-gui tasks, like databases or file write/reads.

Similar Threads

  1. Replies: 0
    Last Post: 09-11-2012, 01:07 AM
  2. Advice needed
    By java_beginner84 in forum New To Java
    Replies: 4
    Last Post: 01-09-2012, 04:57 PM
  3. Advice needed :)
    By kagster in forum New To Java
    Replies: 1
    Last Post: 11-16-2010, 02:04 AM
  4. Advice needed
    By dineshjf in forum Advanced Java
    Replies: 18
    Last Post: 01-09-2009, 04:17 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •