ProgressIndicator, ThreadLocal and Synchronized

Hi,

i was trying to add a ProgressIndicator to my main window to be able to poll from the server what was done by my worker thread. I read the ProgressIndicator demo and put a synchronized block on the whole application (as stated in the code) at the end of my run method in the worker thread.
The problem is that It never enters the synchronized block.

Here is the run method, I had to modify class name and method name but hopefully I was consistent.

	
public void run() {
		
  Iterator<ActionBean> it = ActionRecorder.RECORDER.getActions().iterator();

  while (it.hasNext())
  {
     if (ModeSwitcher.SWITCHER.isShutDown()) {
       logger.info("Mode Switcher ShutDown");
       break;
     }
     it.next();
     it.remove();
  }
		
  synchronized (ClientApplication.getInstance()) {
    //disabling the progress indicator.
    ((MyMainWindow)ClientApplication.getInstance().getMainWindow()).disableProgressIndicator();
  }
}

My application implements the HttpServletRequestListener interface.


private static ThreadLocal<ClientApplication> currentApplication = new ThreadLocal<ClientApplication>();
	
	@Override
	public void init() {
		setMainWindow(new MyMainWindow());
	}

		
	public static ClientApplicationgetInstance() {
		return currentApplication.get();
	}

I don’t know why i’ve this starvation problem… Am I trying to lock the correct object ? I didn’t find any example with an application implementing the HttpServletRequestListener interface.

Hope this is clear.

I run into the same problem, it seems that the TreadLocal pattern does not work with threads. My solution was, that i passed the application (or other relevant parameters) in the constructor of the worker thread:



    ...

    Worker worker = new Worker(ClientApplication.getInstance());
    worker.start();

    ...



    public class Worker extends Thread   {
    	private ClientApplication m_Model = null;
    	
    	public Worker(ClientApplication model) {
    		m_Model = model;
    	}

        public void run() {
       
        Iterator<ActionBean> it = ActionRecorder.RECORDER.getActions().iterator();

        while (it.hasNext())
        {
             if (ModeSwitcher.SWITCHER.isShutDown()) {
             logger.info("Mode Switcher ShutDown");
            break;
        }
        it.next();
        it.remove();
  }
       
  synchronized ((getApplication()) {
    //disabling the progress indicator.
    ((MyMainWindow)m_Model.getMainWindow()).disableProgressIndicator();
  }
}

In the worker thread i only used the passed application object - never use ClientApplication.getInstance()!

Hope that helps!

Norbert

Use InheritableThreadLocal instead of ThreadLocal.

Thanks, using InheritableThreadLocal is the solution!

Accepted as an answer, It solves the problem.

Just one question though, I understand that ThreadLocal is not propagating all the right info to the child threads, but what kind of info is required ?

Whatever the background threads or other users of ThreadLocal in your application need and store in ThreadLocals. In your code above, the client application instance seems to come from one. It is always possible to develop an application without the use of ThreadLocal by passing the necessary instances to child threads etc. but that might lead to more code and parameter passing all over the application.

InheritableThreadLocal makes everything stored in the ThreadLocal available to child threads started from that thread (unless overridden in the child thread).