Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantage of Thread over Runnable [duplicate]

Possible Duplicate:
Java: “implements Runnable” vs. “extends Thread”

  1. Why does Java language provide both Thread and Runnable?
  2. What are the advantages of thread over runnable ( why couldnt Java just provide a runnable)
  3. Can we make a runnable sleep, give it an id etc?
like image 314
user892871 Avatar asked Dec 07 '22 12:12

user892871


1 Answers

  1. Thread is a class, and when you say start() you create a thread of execution which is attached to an instance of Thread class. Therun() method of Runnable is called making it execute the task on to the thread of execution, and the start() method returns quickly.

  2. Runnable is the task that is assigned to the newly created thread of execution.

  3. So, without the Thread class, you cannot run your Runnable.

like image 72
Kumar Vivek Mitra Avatar answered Jan 29 '23 18:01

Kumar Vivek Mitra