1

I'm currently learning about thread in Java and I saw these examples on the net.

Thread t1 = new Thread(new Runnable() {
    @Override
    public void run() {
        //code....
    }
});

and

Thread t2 = new Thread() {
    @Override
    public void run() {
        //code....
    }
};

What's the differences between creating thread with and without creating anonymous runnable? Which one is correct way to use a thread?

keroc
  • 11
  • 1
  • Both are "correct" but I like the first better. Creating sub-classes of Thread is usually the wrong approach. Creating an object like a Runnable than can be executed by a Thread, an ExecutorService or some other means is considered superior in terms of separation of concerns and flexibility. – markspace Nov 27 '14 at 02:57

0 Answers0