0

First of all, thanks for taking time to read this. The code is written and executed as a JUnit test, so I dont know whether that affects the answer.

@Test
public void generate___() {
    long startTime = System.nanoTime();
    for (File file : getResultsFromFolder("C:\\temp\\....")) {
        class runnableClass implements Runnable{
            public void run() {
                // do something with file

            }
        }
        new runnableClass().run();
    }

    long endTime = System.nanoTime();
    System.out.println("total took: " + (endTime - startTime) / 1000000); //divide by 1000000 to get milliseconds.
}
Allen H.
  • 318
  • 6
  • 19
  • Depends if `getResultsFromFolder` is multithreaded. Otherwise, I don't see the creation of any threads. – Jacob G. Apr 19 '18 at 14:10
  • I don't agree with Jacob; Since you are creating and running a new thread (Runnable) for each file you get from `getResultsFromFolder("C:\\temp\\....")`, it is multithreaded – Arnauld Alex Apr 19 '18 at 14:14
  • 1
    @ArnauldAlex A `Runnable` is not a `Thread`. – Jacob G. Apr 19 '18 at 14:15
  • http://javarevisited.blogspot.fr/2012/01/difference-thread-vs-runnable-interface.html Ok it's hard to see the difference but ok. – Arnauld Alex Apr 19 '18 at 14:18
  • 1
    @ArnauldAlex, the article you linked has a misleading title. It does not explain the difference between what a `Thread` _is_, and what a `Runnable` is. A `Thread` is an object with a magic method `start()` that creates a new thread. `Runnable` is just an interface: It doesn't _do_ anything at all. If you create a class that _implements_ runnable, then you will be allowed to store it in `Runnable` variables and pass it to functions that take a `Runnable` argument. But the compiler will require your class to have a `run()` method. – Solomon Slow Apr 19 '18 at 17:48

4 Answers4

2

No it is not.

new runnableClass().run();

This calls the run method directly as defined above it.

If you want this code to be multithreaded you will need to use:

new Thread(new runnableClass()).start();
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
2

No, the runnable will be executed by the main caller thread, just like this:

for (File file : getResultsFromFolder("C:\\temp\\....")) {
     // do something with file
}

To make it multi-threaded, you can create new threads and call start():

for (final File file : getResultsFromFolder("C:\\temp\\....")) {
    class runnableClass implements Runnable{
        public void run() {
            // do something with file
        }
    }
    new Thread(new runnableClass()).start();
}
xingbin
  • 27,410
  • 9
  • 53
  • 103
2

This method is not multi threaded due to the creation of Runnable instance in your method.

Example to showcase answer:

Code

 public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            Runnable myRunnable = new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName());
                }
            };
            myRunnable.run();
        }
    }

Output

main
main
main
main
main
main
main
main
main
main

To make this method multi threaded you could use an ExecutorService.

Code

 public static void main(String[] args) {

    ExecutorService executorService = Executors.newFixedThreadPool(2);

    for (int i = 0; i < 10; i++) {
        Runnable myRunnable = new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
            }
        };

        executorService.execute(myRunnable);
    }

    executorService.shutdown();
}

Output

pool-1-thread-1
pool-1-thread-2
pool-1-thread-1
pool-1-thread-2
pool-1-thread-1
pool-1-thread-2
pool-1-thread-1
pool-1-thread-2
pool-1-thread-1
pool-1-thread-2
Garreth Golding
  • 985
  • 2
  • 11
  • 19
  • Thanks for demonstrating this. Is there a quick tweak to change it to be multithreaded? – Allen H. Apr 19 '18 at 14:20
  • I've updated my answer to showcase the use of an ExecutorService to make this multi threaded. Please accept the answer if it helps. – Garreth Golding Apr 19 '18 at 14:26
  • Thanks! This exactly matches the implementation of some colleagues. ExecutorService.execute is the trick! – Allen H. Apr 19 '18 at 14:40
  • I am able to get this to multithread, but the code block in the run() method does not behave as expected. Any clues on where I can start to look? – Allen H. Apr 19 '18 at 16:32
  • Can you update your question with the code in the run method and I can try and help. – Garreth Golding Apr 19 '18 at 16:54
  • Thanks!! New question posted here https://stackoverflow.com/questions/49926689/java-multithreaded-method-not-behaving-as-expected – Allen H. Apr 19 '18 at 17:15
1

As a complement see this tutorial, it explains well what you want to know. And it has example

https://www.tutorialspoint.com/java/java_multithreading.htm

xingbin
  • 27,410
  • 9
  • 53
  • 103
Arnauld Alex
  • 339
  • 1
  • 3
  • 13