在 Spring 中使用多线程时出现 NullPointer 异常
NullPointer exception while using Multithread in Spring
我是多线程新手。我试图使用 crud 存储库在 sql 数据库中保存一个对象列表。但我在保存操作中遇到空指针异常。
下面是我的代码和异常。
线程 Class
public class PrescriptionThread implements Runnable{
private MedicineService medServ = new MedicineService();
private Presciption pres ;
public PrescriptionThread(Presciption pres) {
this.pres = pres;
}
@Override
public void run() {
medServ.savePrescription(pres);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
服务Class
@Service
public class MedicineService {
@Autowired
private PrescriptionRepository presRepo;
public void storePrescription(List<Presciption> payload) {
ExecutorService executorService = Executors.newFixedThreadPool(5);
for (Presciption presciption : payload) {
Runnable runnable = new PrescriptionThread(presciption);
executorService.execute(runnable);
}
executorService.shutdown();
while(!executorService.isTerminated()) {}
}
public synchronized void savePrescription(Presciption pres) {
presRepo.save(pres);
}
}
存储库
@Repository
public interface PrescriptionRepository extends CrudRepository<Presciption, Integer> {
}
我正在获得例外
presRepo.save(压力);
Exception in thread "pool-1-thread-3" Exception in thread "pool-1-thread-1" Exception in thread "pool-1-thread-2" java.lang.NullPointerException
at com.cerner.api.service.MedicineService.savePrescription(MedicineService.java:86)
at com.cerner.api.util.PrescriptionThread.run(PrescriptionThread.java:16)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
如果我不使用线程而只说
for (Presciption presciption : payload) {
presRepo.save(pres);
}
它工作正常,我可以将它保存到数据库中。但是我想实现线程。
提前致谢。
由于此处:
private MedicineService medServ = new MedicineService();
实例 'medServ' 是 spring IOC 管理,如果你 'new' 它,可能不会得到 'PrescriptionRepository' 的实例,因为它是 '@Autowire' , 当你调用 presRepo.save(pres)
时它会抛出 NPE。但是如果你不使用 Thread 意味着不是你自己新建的实例,所以它可以正常工作
我修改了我的线程 class 如下,我能够得到这个 bean。
public class PrescriptionThread implements Runnable{
private MedicineService medServ;// = new MedicineService();
private Presciption pres ;
public PrescriptionThread(Presciption pres, MedicineService medicineService) {
this.pres = pres;
this.medServ = medicineService;
}
@Override
public void run() {
medServ.savePrescription(pres);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
我是多线程新手。我试图使用 crud 存储库在 sql 数据库中保存一个对象列表。但我在保存操作中遇到空指针异常。 下面是我的代码和异常。 线程 Class
public class PrescriptionThread implements Runnable{
private MedicineService medServ = new MedicineService();
private Presciption pres ;
public PrescriptionThread(Presciption pres) {
this.pres = pres;
}
@Override
public void run() {
medServ.savePrescription(pres);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
服务Class
@Service
public class MedicineService {
@Autowired
private PrescriptionRepository presRepo;
public void storePrescription(List<Presciption> payload) {
ExecutorService executorService = Executors.newFixedThreadPool(5);
for (Presciption presciption : payload) {
Runnable runnable = new PrescriptionThread(presciption);
executorService.execute(runnable);
}
executorService.shutdown();
while(!executorService.isTerminated()) {}
}
public synchronized void savePrescription(Presciption pres) {
presRepo.save(pres);
}
}
存储库
@Repository
public interface PrescriptionRepository extends CrudRepository<Presciption, Integer> {
}
我正在获得例外 presRepo.save(压力);
Exception in thread "pool-1-thread-3" Exception in thread "pool-1-thread-1" Exception in thread "pool-1-thread-2" java.lang.NullPointerException
at com.cerner.api.service.MedicineService.savePrescription(MedicineService.java:86)
at com.cerner.api.util.PrescriptionThread.run(PrescriptionThread.java:16)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
如果我不使用线程而只说
for (Presciption presciption : payload) {
presRepo.save(pres);
}
它工作正常,我可以将它保存到数据库中。但是我想实现线程。 提前致谢。
由于此处:
private MedicineService medServ = new MedicineService();
实例 'medServ' 是 spring IOC 管理,如果你 'new' 它,可能不会得到 'PrescriptionRepository' 的实例,因为它是 '@Autowire' , 当你调用 presRepo.save(pres)
时它会抛出 NPE。但是如果你不使用 Thread 意味着不是你自己新建的实例,所以它可以正常工作
我修改了我的线程 class 如下,我能够得到这个 bean。
public class PrescriptionThread implements Runnable{
private MedicineService medServ;// = new MedicineService();
private Presciption pres ;
public PrescriptionThread(Presciption pres, MedicineService medicineService) {
this.pres = pres;
this.medServ = medicineService;
}
@Override
public void run() {
medServ.savePrescription(pres);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}