2

I'm having trouble at understanding out teachers' method of multithreading. Well, before showing both our examples I must say he's teaching a bunch of newbies (including me I guess ^.^) so he might have picked a method of multithreading that's easy to understand. Also, I'm not visiting his classes, but I've got his script which says the following:

He's doing multithreading like this:

class MyThread implements Runnable {

Thread t;

MyThread() {

    t = new Thread(this, "Demo");
    t.start();
}

public void run() {

    try {

        for (int i = 5; i > 0; i--) {

            System.out.println("Child-Thread:" + i);
            Thread.sleep(1000);
        }
    } catch (InterruptedException e) {
        System.out.println("Child interrupted");
    }
    System.out.println("Child finished");
}

}

I find it cleaner doing like this:

public class Aufg1 {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    MyThread t1 = new MyThread(1);

    ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);
    executor.schedule(t1, 1, TimeUnit.SECONDS);
}

static class MyThread implements Runnable {

    int number;

    public MyThread(int number) {
        this.number = number;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        for (int i = 5; i > 0; i--) {
            System.out.println("Thread " + number + ": " + i);
        }
    }


}
}

Don't bother why I used a Thread-Pool of size 5. I needed it for another exercise. Also, I could've used a ThreadPool for a single execution is this example.

Is there a big difference? Is there an even cleaner way? I know there're some other methods to multithread as well, though I only showed one in here.

Thanks!

7
  • I'm not familiar with ScheduledExecutorService. Does it support interrupting sleeping threads? Commented Jun 10, 2013 at 7:12
  • 1
    He's doing "new Thread" inside a Runnable object ? it's pretty missing the whole point of Runnable. Commented Jun 10, 2013 at 7:13
  • @MarcoForberg I don't know. Though you can manage multiple Runnables (Threads) with this one service. Thx Orel Eraki Commented Jun 10, 2013 at 7:16
  • @OrelEraki why do you say it's missing the whole point of runnable? Commented Jun 10, 2013 at 7:26
  • 2
    @ JustBasti : could you update the title to be more in sync with discussion? Commented Jun 10, 2013 at 8:29

3 Answers 3

8

I would not name the runnable class something with thread in its name, it is a little confusing.

Other than that, you are using the java concurrency package, he's using the lower level thread class that this package is built on. Probably because one must learn how to walk before one can learn how to run. :)

Sign up to request clarification or add additional context in comments.

1 Comment

@JustBasti And starting a thread in a constructor, although fine in this example, is bad practice.
3

There are many implementations of threads, being the easiest one Thread itself, a basic implementation of runnable. I guess his point is just to understand what a Thread is; in your career you will need threads as a tool and you need to know its inner workings, and then use the best implementation that suits you.

Both executors and threads will create one or more (in the case of executors) An Executor is normally used instead of explicitly creating threads. *

Anyway as personal advice I recommend you to keep focusing on new ways of using threads, as each implementation will be better for different problems.

Comments

3

Being somebody who's taught multithreading to beginners I have to admit that I have (at least once) been guilty of writing inelegant code in order to demonstrate a point.

This example seems to be demonstrating what the sleep method does in an artificial way - which your code does not. Your teacher should probably have included some explanation with their code.

Is there a big difference? Is there an even cleaner way? I know there're some other methods to multithread as well, though I only showed one in here.

It depends on what you're trying to achieve. As others have said, there's lots of different ways to do multithreading. The code will have slightly different behaviour, so there is a difference - but that doesn't mean that once is 'cleaner'. As this is an artificial example (counting to 5) we can't really say what the 'cleanest' or 'best' method is.

Comments

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.