1

I'm doing smth like:

Iterator<String> iterator = requestList.iterator()

    (1..threadCount).each {

        Thread.start {

            while(iterator.hasNext()) {
                log.info iterator.next()
                Thread.sleep(50)
            }
        }
    }

Given that threadCount = 10 and requestList is ~115, I expect all threads to output all the list, each time asking iterator to give them next.

However, I hardly even get 10 logs, usually 8.

Everything is done inside SoapUI groovy script step, instead of log.info I actually plan triggering a REST request with number N.

What am I doing wrong with these threads?

UPD

Okay, did smth stupid like this, to test (and avoid using one array):

def array1 = all[0..5]
def array2 = all[6..11]


Thread.start{

    for(String r: array1) {
        log.info r
    }
}

Thread.start{

    for(String r: array2) {
        log.info r
    }
}

And now I have no output at all or one log at most, though I expect 12. How do I create threads that will output data simultaneously?

EVEN MORE

def threadCount=10

(0..threadCount).each { n ->

 Thread.start {

  (1..10).each {
       log.info "thread"+n+" says| "+it
  }
 }
}

Output is:

thread0 says| 1
thread3 says| 1
thread8 says| 1
thread2 says| 1
thread1 says| 1
thread9 says| 1
thread7 says| 1
thread5 says| 1
thread4 says| 1
thread0 says| 2

And nothing more. Again, what's wrong with me or groovy? (hope groovy is fine)

TEH EMPRAH
  • 1,828
  • 16
  • 32
  • 1
    This will lead to race condition and a data race as well because the iterator object is shared by all threads without any synchronization. So the output is unpredictable. – MS Srikkanth Apr 19 '16 at 09:26
  • You stumbled upon a thread safety issue. A related question is this one: http://stackoverflow.com/questions/5847939/is-list-iterator-thread-safe – Danilo Apr 19 '16 at 09:56
  • About your update, define the arrays properly to see the output you are expecting: def array1 = 0..5. In the for loop you should use Integer, but String will work as well. – Danilo Apr 19 '16 at 10:08
  • 1
    Please check [this](http://stackoverflow.com/questions/10790813/groovy-concurrency) – Rao Apr 19 '16 at 11:32

1 Answers1

0

In the end, problem is that SoapUI kills main thread before all threads have an opportunity to grab their next number.

A quick way to live with this is to add sleep to main method

TEH EMPRAH
  • 1,828
  • 16
  • 32