2016-04-15 62 views
0

迭代值我有一个for循环作为获得由ThreadPoolExecutor的并列执行多次调用类的一部分。可调用类的其余部分似乎运行正常。我认为这很重要。只有迭代器导致问题。所以这里是411:循环运行,它在分立列表中搜索报警。当它找到一个(它会在我绊倒任何一个时做它),它应该创建一个报警对象并给它在列表中的位置ID。这就是迭代器的用途。问题与并行任务

直到我使这项任务并行执行,迭代器(int i)以每次返回的数组中它的位置。现在它只返回一个值0,尽管在数组上正确迭代。

这里的可调用的相关代码。迭代器不是线程安全的还是什么?我很困惑。

 //Create a list to store discretes 
     boolean[] boolist = new boolean[1024]; 
     //Read values from the site and assign them to the boolean array 
     mbc.readInputDiscretes(1, 0, boolist); 
     for (int i = 0; i < boolist.length; i++) { 
     //Checks to see if the alarm is alarmed. Do not be alarmed if the alarm is alarmed. 
      if(boolist[i]){ 
       //For each value of true we create a new alarm tag, set the alarm to active within the system, record its site ID and its Tag ID. 
       TagInfo alarmtag = new TagInfo(true, InfoBox.getSiteID(), i); 
       //THE ITERATOR ABOVE ONLY RETURNS 0, HOWEVER IT ITERATES THROUGH THE LIST PROPERLY. SO ALL TAGS ARE NOTICED BUT RETURN WITH AN ID OF 0. AHHHHHH. 
       //Add the tag to the list to return. 
       alarmtags.add(alarmtag); 
      } 
     } 

编辑:

按要求,这里的相关件TagInfo类:

public class TagInfo { 
private int siteID;     //ID # of the site this tag belongs to 
private int tagID;     //ID # of the tag itself 
private int tagOffset;    //Integer 1-1024 related to the tags read position at its given site 
private int tagType;    //The type of tag (not yet implemented, just thought it would be useful eventually) 
private int tagEscDly;    //The amount of time to wait before we raise the priority level of an alarm referencing this specific tag 
private Timestamp lastPolled;  //The last time this site was checked 
private boolean alarmstatus;  //The alarm status of this specific tag, e.g. in alarm or not in alarm 

//Default empty constructor for TagInfo object 
public TagInfo(){} 

//Overloaded constructor for faster initialization of objects and fewer method calls 
public TagInfo(boolean alarmstatus, int siteID, int tagID){ 
    this.alarmstatus = alarmstatus; 
    this.siteID = siteID; 
    this.tagID = tagID; 
} 

下面的代码调用调用:

 //Invoke run method on each site simultaneously, store results in a list 
     List<Future<List<TagInfo>>> futures=threadmaker.invokeAll(active_sites.stream().map(site -> new TAG_SCANNER(site, loggr)).collect(Collectors.toList())); 
+0

后alarmtag'的'的声明。另外一个[SSCCE](http://sscce.org/)会很好。 – m0skit0

+0

我以为这条线是宣言? TagInfo alarmtag = new TagInfo(true,InfoBox.getSiteID(),i); – TheFunk

+1

你能提供TagInfo'(构造函数和成员delaration至少) –

回答

0

解决:问题躺在使用不正确的变量。