Hi guys,

i have been having some problems parsing a list of strings.
lets say i have a list that has the following strings

List<String> list = new ArrayList<String>();

list.add("Process A Started");
list.add("Process B Started");
list.add("Process B Finished");
list.add("Process A Started");
list.add("Process C Started");
list.add("Process D Started");
list.add("Process C Finished");
list.add("Process A Finished");
list.add("Process B Started");
list.add("Process D Finished");
list.add("Process A Started");
...

what i need is to come up with the logic that would check whether the process A/B/C/D.. has started and has it finished. if it started and not finished that means it failed. The following would be the logic required:

  • If process started it has to finish

  • Process can not start again if it already started.

  • if a process started other process can run and finish before the specific process finished

please help me on sorting this out, suggestions, pseudo codes actual code :) or any other help would be appreciated.

Thanks You.

well, first you would need to have actual processes, I suppose. could you just try and explain what exactly it is that you are trying to achieve?

Hi guys,

i have been having some problems parsing a list of strings.
lets say i have a list that has the following strings

List<String> list = new ArrayList<String>();

list.add("Process A Started");
list.add("Process B Started");
list.add("Process B Finished");
list.add("Process A Started");
list.add("Process C Started");
list.add("Process D Started");
list.add("Process C Finished");
list.add("Process A Finished");
list.add("Process B Started");
list.add("Process D Finished");
list.add("Process A Started");
...

what i need is to come up with the logic that would check whether the process A/B/C/D.. has started and has it finished. if it started and not finished that means it failed. The following would be the logic required:

  • If process started it has to finish

  • Process can not start again if it already started.

  • if a process started other process can run and finish before the specific process finished

please help me on sorting this out, suggestions, pseudo codes actual code :) or any other help would be appreciated.

Thanks You.

well adding to what stultuske said, if you create threads you could use the isAlive() method on the threads instance to see if its still alive. to make the process/thread end, you could look into the join(int time) method, or an instance variable which when the value is changed will make the thread quit.

What does a list of Strings have to do with monitoring Threads?

commented: thanks for pointing this out +3

You could create a Map with process name as key and its status as value. The first time you process a record for a given process name you will add it to the Map with a status of "started". When you find its "finished" record you update the status. When all the data is processed you can check all the entries for status not "finished".
If you find a "started" record for a process that's already in the map then you know it's been started twice.

thanks a lot James, i see where you are going with this, however i am not comfortable working with hash maps( a while ago i was advised that they are not thread safe, therefore never learnt them).

However if you could expand on the second part of your comment:

When all the data is processed you can check all the entries for status not "finished".
If you find a "started" record for a process that's already in the map then you know it's been started twice.

i might be able to implement this ,but currently i am not very sure i understand how this could be achieved.

many thanks to everyone

HashMaps are a generally very useful tool - no need to avoid them! If you need thread-safe map then you can wrap the class - but there's nothing in your original post about thread-safe.
You wanted to know about processes that have started but not finished - these will be all the entries in the map that still have a status of "started" - you can find them with a simple loop thru the map.
You wanted to check that a process can not start again if it already started. That means when you try to add it to the Map it's already there. There's a map method for that.

@NormR1: nothing at all. we were just pointing out that printing a String is not the same as starting a process

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.