3

I just today started to learn Threads in Java. So far I have seen people usually use 2 methods to create them, but I don't understand the difference between them:

1:

new Thread() {
    @Override
    public void run(){
        //mycode goes here;
    };
}.start();

2:

new Thread(new Runnable() {
    @Override
    public void run(){
        //mycode goes here;
    }
}).start();

So why does people use new Runnable(), if that's not necessary? It just forces you to have run() method, but if you create thread then its not logical to not create run method yourself? Or am I wrong?

But why to use new Runnable() when creating anonymous Threads? Like in second example above? Since I have seen that is some tuturials, which I found online. I am just asking if there is a reason of doing it or not.

I know that Thread can be created also in other ways:
(And I am not speaking about implementing vs extending!)

3:

Thread t1 = new Thread(new MyRunnable());

4:

MyThreadClass my1 = new MyThreadClass();
Binkan Salaryman
  • 3,008
  • 1
  • 17
  • 29
arccuks
  • 173
  • 2
  • 12
  • [Check this out](https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html) It explains why using runnable is preferred in a few scenarios. – Codebender Jul 03 '15 at 08:45
  • 2
    possible duplicate of ["implements Runnable" vs. "extends Thread"](http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread) – alex2410 Jul 03 '15 at 08:48
  • I am not asking what to prefer, i am asking why people do new Runnable. Exactly like on my examples, i wanted to know if there is a difference between using a Thread with constructor new Runnable(i am not speaking about using MyRunnables). – arccuks Jul 03 '15 at 09:11
  • 2
    Since Java 8 you can do this with a lambda expression: ``new Thread(() -> { /*code*/ }).start();`` – Binkan Salaryman Jul 03 '15 at 09:43
  • Ty for info. Havent studied Lamba expression of Java8 yet, but have seen that kinda of examples too. – arccuks Jul 03 '15 at 12:02

3 Answers3

4

It might be useful in some situations when you already have an instance of Runnable to just run in in a different Thread. For example Runnables can be used for excapsulating the Command Design Pattern.

In your case there is no reason to create Runnable as it does not add anything.

Crazyjavahacking
  • 9,343
  • 2
  • 31
  • 40
  • Thanks, as i am still in learning process of Java and programming it self, words Command Design Pattern means nothing to me. But if you say that it might be useful in some situations, gona keep that in mind :) – arccuks Jul 03 '15 at 08:57
1

Basically you need to understand that, if you have to modify the behavior of Thread then you need to extend Thread class otherwise if you just have to run some separate threads then you need to implement the interface Runnable.

Please see here for more: https://stackoverflow.com/a/16489176/1129313

Community
  • 1
  • 1
Garry
  • 4,493
  • 3
  • 28
  • 48
  • @Crazyjavahacking ... I read somewhere here, please see this http://stackoverflow.com/a/16489176/1129313 – Garry Jul 03 '15 at 08:59
  • You are not answering the question. He was asking something else. – Crazyjavahacking Jul 03 '15 at 09:24
  • Oh really, please dont be like that and remove your down vote. – Garry Jul 03 '15 at 09:28
  • Basically you're right: ``extend`` when possibly changing existing code - ``implement`` when creating something new. Nevertheless, this is not that useful when talking about threadsm, as Crazyjavahacking mentioned. – Binkan Salaryman Jul 03 '15 at 09:51
0

You may have heard of thread pools;Where we reuse the threads to process different jobs (read runnables). Look at threadpoolexecutor API submit method. That will give you some idea.

YesR
  • 100
  • 1
  • 6