英文:
Calling method of Another class from run() method
问题
我已创建一个调度程序,调用run方法。
@WebListener
public class BaclkgroundJobManager implements ServletContextListener {
private ScheduledExecutorService scheduler;
public void contextInitialized(ServletContextEvent sce) {
scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new SomeMinuteJob(), 0, 5, TimeUnit.MINUTES);
}
}
这调用了:
public class SomeMinuteJob implements Runnable {
@Override
public void run() {
System.out.print("Inside run method");
ReadData obj = new ReadData();
obj.readData();
System.out.println("After reading data");
}
}
我已创建ReadData
类如下:
public class ReadData {
public void readData() {
System.out.println("I am inside readdata");
}
}
所以我在run()方法内创建了ReadData
对象,并调用了它的方法。但控制权没有传递到ReadData
类。它没有打印出readData()方法内的内容。即使我创建了构造函数并放入一些内容,但仍然没有打印出来。我该如何解决这个问题?
控制权进入了run并打印出Inside run method
。
英文:
I have created a scheduler which calls run method.
@WebListener
public class BaclkgroundJobManager implements ServletContextListener {
private ScheduledExecutorService scheduler;
public void contextInitialized(ServletContextEvent sce) {
scheduler=Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new SomeMinuteJob(), 0, 5, TimeUnit.MINUTES);
}
}
this calls to
public class SomeMinuteJob implements Runnable {
@Override
public void run() {
System.out.print("Inside run method");
ReadData obj= new ReadData();
obj.readData();
System.out.println("After reading data");
}
}
I have created ReadData
class below.
public class ReadData
{
public void readData()
{
System.out.println("I am inside readdata");
}
}
So I have created object of ReadData
inside the run() mehthod and I have called its method. But control is not coming to ReadData class. it is not printing the content inside readData() method. Even I created constructor and put some content but even though that is not printing. How can I resolve that?
Control is going inside run and it is printing Inside run method
答案1
得分: 0
ScheduledExecutorService
的一个常见问题是,如果计划的命令抛出异常,异常会被静默地吞噬。问题中的代码似乎不会抛出异常,但在更复杂的代码中很难避免(例如意外的空指针异常)。
一个快速的修复方法是将所有的Runnable.run
方法放入try-catch块中:
public class SomeMinuteJob implements Runnable {
@Override
public void run() {
try {
System.out.print("Inside run method");
ReadData obj = new ReadData();
obj.readData();
System.out.println("After reading data");
} catch (Exception e) {
// 记录异常
e.printStackTrace();
}
}
}
英文:
A common problem with ScheduledExecutorService
is that if the scheduled command throws an exception, the exception is silently swallowed. The code in the question does not seem to throw exceptions, but in more complex code it is difficult to avoid (for example unexpected NullPointerExceptions)
A quick fix is to put all of the Runnable.run method in a try-catch block:
public class SomeMinuteJob implements Runnable {
@Override
public void run() {
try {
System.out.print("Inside run method");
ReadData obj = new ReadData();
obj.readData();
System.out.println("After reading data");
} catch (Exception e) {
// Log exception
e.printStackTrace();
}
}
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论