Java 为什么可以';我一次不能运行多个线程吗?

Java 为什么可以';我一次不能运行多个线程吗?,java,spring,multithreading,spring-boot,Java,Spring,Multithreading,Spring Boot,我有一个SpringBoot应用程序,我试图读取多个读卡器,然后根据读卡器的输出执行某些数据库操作。在我创建一个运行组件的类之前,它一直工作得很好。现在我得到了一个arrayindexoutofboundsexception。这是密码 有错误的方法: private void Start() throws AlienReaderException, IOException{ ThreadStop = false; service= new MessageListenerSer

我有一个SpringBoot应用程序,我试图读取多个读卡器,然后根据读卡器的输出执行某些数据库操作。在我创建一个运行组件的类之前,它一直工作得很好。现在我得到了一个arrayindexoutofboundsexception。这是密码

有错误的方法:

private void Start() throws AlienReaderException, IOException{
    ThreadStop = false;

     service= new MessageListenerService(3900);
     service.setMessageListener(this);
     service.startService();
    System.out.println("length of readers: "+Reader.ipAddress.length);

      for (lastThreadId = 0; lastThreadId < Reader.ipAddress.length; 
lastThreadId++)
      {
          **m_inventory[lastThreadId] = new 
AlienReader(Reader.ipAddress[lastThreadId], Reader.port, 
Reader.username[lastThreadId], Reader.password[lastThreadId]);**
          log.info("taginventory reader: "+ 
Reader.ipAddress[lastThreadId]+"Thread: "+lastThreadId);
          m_run_process[lastThreadId] = new Thread(new 
StartInventoryThread(Reader.ipAddress[lastThreadId], Reader.port, 
Reader.username[lastThreadId], Reader.password[lastThreadId], 
m_inventory[lastThreadId]));
          m_run_process[lastThreadId].start();
      }
      --lastThreadId;

      try
      {
          Thread.sleep(1000);
      }
      catch (Exception ex)
      {
          ex.getMessage();
      }


}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tagInventory' defined in file [C:\Users\rcollins\Workspaces\MyEclipse 2017 CI\rfidreader\target\classes\com\rfidreader\rfidbusiness\TagInventory.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.rfidreader.rfidbusiness.TagInventory]: Constructor threw exception; nested exception is java.lang.ArrayIndexOutOfBoundsException: 2
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1238) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1137) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:503) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at com.rfidreader.RfidreaderApplication.main(RfidreaderApplication.java:27) [classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.rfidreader.rfidbusiness.TagInventory]: Constructor threw exception; nested exception is java.lang.ArrayIndexOutOfBoundsException: 2
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:182) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1230) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    ... 17 common frames omitted
Caused by: java.lang.ArrayIndexOutOfBoundsException: 2
    at com.rfidreader.rfidbusiness.TagInventory.Start(TagInventory.java:139) ~[classes/:na]
    at com.rfidreader.rfidbusiness.TagInventory.<init>(TagInventory.java:96) ~[classes/:na]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_112]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_112]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_112]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_112]
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:170) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    ... 19 common frames omitted
错误:

private void Start() throws AlienReaderException, IOException{
    ThreadStop = false;

     service= new MessageListenerService(3900);
     service.setMessageListener(this);
     service.startService();
    System.out.println("length of readers: "+Reader.ipAddress.length);

      for (lastThreadId = 0; lastThreadId < Reader.ipAddress.length; 
lastThreadId++)
      {
          **m_inventory[lastThreadId] = new 
AlienReader(Reader.ipAddress[lastThreadId], Reader.port, 
Reader.username[lastThreadId], Reader.password[lastThreadId]);**
          log.info("taginventory reader: "+ 
Reader.ipAddress[lastThreadId]+"Thread: "+lastThreadId);
          m_run_process[lastThreadId] = new Thread(new 
StartInventoryThread(Reader.ipAddress[lastThreadId], Reader.port, 
Reader.username[lastThreadId], Reader.password[lastThreadId], 
m_inventory[lastThreadId]));
          m_run_process[lastThreadId].start();
      }
      --lastThreadId;

      try
      {
          Thread.sleep(1000);
      }
      catch (Exception ex)
      {
          ex.getMessage();
      }


}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tagInventory' defined in file [C:\Users\rcollins\Workspaces\MyEclipse 2017 CI\rfidreader\target\classes\com\rfidreader\rfidbusiness\TagInventory.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.rfidreader.rfidbusiness.TagInventory]: Constructor threw exception; nested exception is java.lang.ArrayIndexOutOfBoundsException: 2
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1238) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1137) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:503) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
    at com.rfidreader.RfidreaderApplication.main(RfidreaderApplication.java:27) [classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.rfidreader.rfidbusiness.TagInventory]: Constructor threw exception; nested exception is java.lang.ArrayIndexOutOfBoundsException: 2
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:182) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1230) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    ... 17 common frames omitted
Caused by: java.lang.ArrayIndexOutOfBoundsException: 2
    at com.rfidreader.rfidbusiness.TagInventory.Start(TagInventory.java:139) ~[classes/:na]
    at com.rfidreader.rfidbusiness.TagInventory.<init>(TagInventory.java:96) ~[classes/:na]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_112]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_112]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_112]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_112]
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:170) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    ... 19 common frames omitted
org.springframework.beans.factory.BeanCreationException:创建名为“tagInventory”的bean时出错[C:\Users\rcollins\Workspaces\MyEclipse 2017 CI\rfidreader\target\classes\com\rfidreader\rfidbusiness\tagInventory.class]:bean实例化失败;嵌套异常为org.springframework.beans.beanstantiationException:未能实例化[com.rfidreader.rfidbusiness.TagInventory]:构造函数引发异常;嵌套异常为java.lang.ArrayIndexOutOfBoundsException:2
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.InstanceBean(AbstractAutowireCapableBeanFactory.java:1238)~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1137)~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:503)~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
在org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
在org.springframework.beans.factory.support.DefaultListableBeanFactory.PreInstanceSingleton(DefaultListableBeanFactory.java:760)~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869)~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
在org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
位于org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395)[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
在org.springframework.boot.SpringApplication.run(SpringApplication.java:327)[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
在org.springframework.boot.SpringApplication.run(SpringApplication.java:1255)[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
在org.springframework.boot.SpringApplication.run(SpringApplication.java:1243)[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
在com.rfidreader.RfidreaderApplication.main(RfidreaderApplication.java:27)[classes/:na]
原因:org.springframework.beans.beanstantiationException:未能实例化[com.rfidreader.rfidbusiness.TagInventory]:构造函数引发异常;嵌套异常为java.lang.ArrayIndexOutOfBoundsException:2
在org.springframework.beans.BeanUtils.instanceClass(BeanUtils.java:182)~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
在org.springframework.beans.factory.support.SimpleInstallationStrategy.instantiate(SimpleInstallationStrategy.java:87)~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.InstanceBean(AbstractAutowireCapableBeanFactory.java:1230)~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
... 省略17个公共框架
原因:java.lang.ArrayIndexOutOfBoundsException:2
在com.rfidreader.rfidbusiness.TagInventory.Start(TagInventory.java:139)~[classes/:na]
在com.rfidreader.rfidbusiness.TagInventory.(TagInventory.java:96)~[classes/:na]
在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)~[na:1.8.0_112]
在sun.reflect.nativeconstructoraccessormpl.newInstance(nativeconstructoraccessormpl.java:62)~[na:1.8.0\u 112]
在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)~[na:1.8.0112]
在java.lang.reflect.Constructor.newInstance(Constructor.java:423)~[na:1.8.0_112]
在org.springframework.beans.BeanUtils.instanceClass(BeanUtils.java:170)~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
... 省略19个公共框架

期望有人在没有其他信息的情况下通读380行的类来解决您的问题是不合理的。请您提供有关错误的更多信息,包括带有行号的stacktrace。如果可能的话,试着将您要求人们阅读和理解的代码限制为仅与您试图解决的错误相关的代码。您自己尝试管理线程有什么原因吗?除非您有限制,否则我强烈建议您不要尝试自己管理它,而是使用一些高级并发抽象