我有以下课程
@Component
public class CountyScraper implements Runnable {
volatile private String stateLink;
@Autowired
StateScrapeQueueRepository stateScrapeRepository;
public CountyScraper() {
}
public CountyScraper(String stateLink) {
this.stateLink = stateLink;
}
@Override
public void run() {
//other code
stateScrapeRepository.updateScrapeTimestamp(stateLink);
} catch (Exception e) {
e.printStackTrace();
}
}
}
我在java.lang.NullPointerException
上得到了stateScrapeRepository.updateScrapeTimestamp(stateLink);
这是我创建线程的方式
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(1);
for (int i = 0; i < stateLinks.size(); i++) {
CountyScraper countyScraper = new CountyScraper(stateLinks.get(i));
executor.execute(countyScraper);
}
executor.shutdown();
我假设在多线程类中使用@Autowired
时应该做不同的事情?