从run()方法调用另一个类的方法

huangapple go评论77阅读模式
英文:

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>



huangapple
  • 本文由 发表于 2020年8月10日 05:47:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63331644.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定