Access ThreadLocal-Instance in new Thread

Hi all,

In my application I would like to put the creation of a report into a separate Thread as it is quite a longrunner.
I used the example from Vaadin-Sampler for ProgressIndicator (
link
) to create my Thread.
It looks as follows:


public void buttonClick(ClickEvent event) {

             MyReportWorker worker = new MyReportWorker(event.getButton, someData);
	     worker.start();
	     progressIndicator.setEnabled(true);
	     progressIndicator.setVisible(true);
}

Works fine so far.
But in my worker I create an instance of my ReportHelper-Class:


@Override
public void run() {
     parentButton.setEnabled(false);
     ReportHelper h = new ReportHelper (someData);
     h.createMyReport();
}

In the constructor of this helper I try to gain access to MyApplication (ThreadLocal-Pattern):


public  ReportHelper(SomeData data){
   doSomething();
   SomeMoreData smd = MyApplication.getInstance().getSomeMoreData();
   doSomethingElse();
}

Here I always run into a NullPointerException (2nd line).
I tried to pass the application-instance as a parameter to the constructor. But this didn’t work either.

Is there a way to access my Application-instance in a separate Thread or do I have to pass all data that I need to my Helper before working with it?

Hi,

try using an InheritableThreadLocal. That might just solve your problem.

Thanks!!!

Just changed my MainApplication to


private static InheritableThreadLocal<MyApplication> threadLocal = new InheritableThreadLocal<MyApplication>();

and everything works like a charm.