0

I have a for loop which needs to execute 36000 times

for(int i=0;i<36000;i++)
{
}

Whether its possible to use Multiple threads inorder to execute the loop faster at the same time

Please suggest how to use it.

Yuvaraj
  • 3
  • 3
  • 6
    what are you trying to do in this loop? – SMA Feb 25 '16 at 07:00
  • I am trying to get data from a dynamically webpage from a website and write in a file.Here 1 to 36000 is page number. – Yuvaraj Feb 25 '16 at 07:04
  • multi thread execute in order from 0 to 36000? this will be more faster? – xxxzhi Feb 25 '16 at 07:08
  • @What i want is Thread1 is executing 1 ,Where Thread2 is executing 2 and Thread 3 is executing 3 again Thread1 is executing 4 ...and so.As its done in parallel i am expecting to increase the speed of the task – Yuvaraj Feb 25 '16 at 07:30
  • Check how many cores/processors you have available, split work evenly between them by creating number of threads = number of cores/processors. For example with 4 cores you get 4 threads: first will process pages from 0-8999, second 9000-17999 and so on... – zubergu Feb 25 '16 at 07:39

1 Answers1

0

If you want a more explicit method, you can use thread pools with Thread, Callable or Runnable. See my answere here for examples: Java : a method to do multiple calculations on arrays quickly Thread won't naturally exit at end of run()

I do not recommend using Java's Fork/Join as they are not that great as they were hyped to be. Performance is pretty bad. Instead, I would use Java 8's map and parallel streams if you want to make it easy. You have several options using this method.

IntStream.range(1, 4)
    .mapToObj(i -> "testing " + i)
    .forEach(System.out::println);

You would want to call map( lambda ). Java 8 finally brings lambda functions. It is possible to feed the stream one huge list, but there will be a performance impact. IntStream.range will do what you want. Then you need to figure out which of the new functions you want to use like filter, map, count, sum, reduce, etc. You may have to tell it that you want it to be a parallel stream. See these links. https://docs.oracle.com/javase/tutorial/collections/streams/parallelism.html http://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/

Classic method and still has the best performance is to do it yourself using a thread pool: Basically, you would create a Runnable (does not return something) or Callable (returns a result) object that will do some work on one of the treads in the pool. The pool with handle scheduling, which is great for us. Java has several options on the pool you use. You can create a Runnable/Callable in a loop, then submit that into the pool. The pool immediately returns a Future object that represents the task. You can add that Future to an ArrayList if you have many of these. After adding all the futures to the list, loop through them and call future.get(), which will wait for the end of execution. See the linked example above, which does not use a list, but does everything else I said.

Community
  • 1
  • 1
ldmtwo
  • 419
  • 5
  • 14