3

I have to execute multiple instances of my class concurrently. i have written the following code below. I have done it in two ways. But I don't see the difference. What is the right way to have parallel running threads?

Thanks.

Here the snippet:

public class MyClass {

        public MyClass() {
            runnable = true;
        }

        public boolean isRunnable() {
            return runnable;
        }

        public static void main(String[] args) throws InterruptedException {

            /* METHOD 1
             MyClass myclass = new MyClass();

             if (myclass.isRunnable()) {
                 for (int i = 0; i < loop; i++) {
                     myclass.execTask();      
                     Thread.sleep(sleep);
                 }
             }
             */

                  //METHOD 2
            final MyClass myclass = new MyClass();


            ExecutorService threadPool = Executors.newFixedThreadPool(threadNo);

            for (int i = 0; i < threadNo; i++) {
               threadPool.submit(new Runnable() {
                    public void run() {
                        for (int i = 0; i < loop; i++) {
                        myclass.execTask();
                            try {
                                Thread.sleep(sleep);
                            } catch (InterruptedException ex) {
                                Logger.getLogger(MyClass.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    }
                });
            }
            threadPool.shutdown();

            public void execTask(){
            .........
            }
        }
    }
2
  • in parallel, or concurrently? Commented Nov 4, 2013 at 15:12
  • BTW: myclass.execTask; does nothing - you should at least be using myclass.execTask();. Commented Nov 4, 2013 at 15:15

1 Answer 1

6

The difference between your two methods is that in "Method 2," you may actually have multiple threads of execution (assuming that threadNo is greater than 1). Method 2 allows for multiple work threads to grab a Runnable and call execTask (by calling run) in parallel (concurrently, to be precise). The pause in "Method 2" does pretty much nothing.

"Method 1" executes entirely on the "Main" thread. It calls execTask, waits, and then calls it again. This will call the execution some number of times serially.

(I assume this is pseudo-code because parts of it won't compile.)

Here is a cleaned-up "Method 2". Note that I have threadNo worker threads (it helps if this is larger than 1) and I create loop Runnables.

ExecutorService threadPool = Executors.newFixedThreadPool(threadNo);

for (int i = 0; i < loop; i++) {
    threadPool.submit(new Runnable() {
        public void run() { myclass.execTask(); });
}
Sign up to request clarification or add additional context in comments.

6 Comments

so I can remove the sleep? if I Loop only once, I see only one Task being executed. I still thought the threadpool submit would start all 'threadNo' threads concurrently..
I edited the answer to make the proper use of Method 2 a little clearer.
Thanks.Just to be clear, when does one want to have several "runnable" Tasks? Is the case when Runnable is 1, all the threads compete to execute the one Runnable and only one gets to?
You want to use this kind of multithreading when each call to execTask does a single, independent piece of work. By placing these pieces of work on a queue and allowing them to be worked concurrently, you should be able to get them done faster than if you do them one at a time.
that execTask actually does some file handling. Can I be sure that Access to the file would be mutually exclusive?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.