• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is need of extending Thread Class or implementing Runnable interface ?  RSS feed

 
Jackson Martin
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am new in thread. I was practicing some examples. Now my question is "We can create a Thread in main () or in any class in any method Then Why we required Extending Thread class or implementing Runnable interface"? I am attaching code below I know it is not right way but I want to know reason.


I am new in Thread. Hope you can understand my confusion here or If I am making mistakes let me know.
 
Henry Wong
author
Sheriff
Posts: 22861
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you create threads, you do so, to do stuff... Hint. What stuff does your newly created thread do?



Henry
 
Jackson Martin
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:When you create threads, you do so, to do stuff... Hint. What stuff does your newly created thread do?



Henry


Right now I have not mentioned any specific task for Thread. But each thread will perform some task simultaneously with other task. I still don't have knowledge about concurrent process or task. I am confuse with creation of threads. We already have main thread which starts with program then we can also create new thread without extending or implementing interface. I know there must be some reason and that'y we create thread by extending or implementing interface. But I am not able to understand that thing.
 
Henry Wong
author
Sheriff
Posts: 22861
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jackson Martin wrote:Right now I have not mentioned any specific task for Thread. But each thread will perform some task simultaneously with other task. I still don't have knowledge about concurrent process or task. I am confuse with creation of threads.


So, first, you don't know how to create a thread that does a specific task yet -- but you are learning, and understand that there is a way to do that...


Jackson Martin wrote:I am confuse with creation of threads. We already have main thread which starts with program then we can also create new thread without extending or implementing interface. I know there must be some reason and that'y we create thread by extending or implementing interface. But I am not able to understand that thing.


So, second, you don't know what is the purpose of extending the Thread class, or implementing the Runnable interface. You do know that it does something, but you don't know what it is yet. And of course, this is the reason that you are asking here...


Hint ... do you think that these two points may be related?

Henry
 
Jackson Martin
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry but I am not able to understand what you are saying.
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jackson Martin wrote:...We already have main thread which starts with program then we can also create new thread without extending or implementing interface. I know there must be some reason and that'y we create thread by extending or implementing interface. But I am not able to understand that thing.

There is no greater authority on Threading than Henry but here's my 2 cents.

When you run a Java application, the JVM starts a JVM process which spawns a main thread that runs the application’s main() method.

The main() thread may spawn additional threads.

The ability to spawn additional threads can make an application more responsive, for example, while typing in an MS Word document, the following features run at the same time as separate threads:
  • MS Word’s AutoCorrect feature runs at the same time as a separate thread and corrects typos and misspelled words
  • MS Word’s AutoRecovery feature runs at the same time as a separate thread and saves your document (every 10 minutes by default)

  • The main thread can spawn an additional thread as follows:
  • the main() method creates an instance of java.lang.Thread (passing in an implementation of java.lang.Runnable into the constructor)
  • the main() method calls the java.lang.Thread instance’s start() method which induces the JVM to create a thread

  • The thread then becomes runnable and eventually gets to run when it is allocated CPU time by the thread scheduler. It runs the java.lang.Runnable implementation's run() method.
     
    Henry Wong
    author
    Sheriff
    Posts: 22861
    119
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Henry Wong wrote:
    Hint ... do you think that these two points may be related?


    Jackson Martin wrote:Sorry but I am not able to understand what you are saying.


    The purpose "of extending Thread Class or implementing Runnable interface" is so that you can define a "specific task for Thread". Otherwise, as in your example, all you can do is create a thread that does nothing.

    Henry
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!