I have the following code, which creates two threads, which are injected with a @ThreadScoped bean, which in turn also has @ThreadScoped bean.
But the nested @ThreadScoped bean is not threadscoped - both threads share the same bean:
Thread thread B / MyBean #9996039 in MyService #14222419 Thread thread A / MyBean #9996039 in MyService #29945686
Is this expected or a bug?
Thanks, Ondra
@Singleton
public class App
{
static WeldContainer wc;
public static void main( String[] args )
{
System.out.println( "Hello World!" );
wc = new Weld().initialize();
wc.instance().select(App.class).get().main2(args);
}
@Inject MyThread mt1;
@Inject MyThread mt2;
private void main2(String[] args) {
new Thread( mt1, "thread A" ).start();
new Thread( mt2, "thread B" ).start();
}
}
class MyThread implements Runnable {
@Inject Instance<MyService> myService;
@Override
public void run() {
System.out.println(" Thread " + Thread.currentThread().getName() + " / " + myService.get().quack() );
}
}
@ThreadScoped
class MyService {
@Inject Instance<MyBean> myBean;
public String quack(){
return "MyBean #" + myBean.get().hashCode() + " in MyService #" + this.hashCode();
}
}
@ThreadScoped
class MyBean {
}