1.如何将弹簧豆注入螺纹中
2.如何在spring bean中启动一个线程.
这是我的代码.
MyThread.java
@Component
public class MyThread implements Runnable {
@Autowired
ApplicationContext applicationContext;
@Autowired
SessionFactory sessionFactory;
public void run() {
while (true) {
System.out.println("Inside run()");
try {
System.out.println("SessionFactory : " + sessionFactory);
} catch (Exception e) {
e.printStackTrace();
}
try {
Thread.sleep(10000);
System.out.println(Arrays.asList(applicationContext.getBeanDefinitionNames()));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在run从下面的类调用方法(请建议我是否遵循错误的appraoch调用spring bean中的线程)
@Component
public class MyServiceCreationListener implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if (event.getApplicationContext().getParent() == null) {
System.out.println("\nThread Started");
Thread …Run Code Online (Sandbox Code Playgroud) 在java中我们创建一个线程对象
Thread t1 = new Thread(Runnable object);
t1.start();
Run Code Online (Sandbox Code Playgroud)
线程生命周期的不同阶段t1和执行后run()的状态是t1什么?