Total members 11895 |It is currently Sun Dec 22, 2024 2:08 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





A thread, by definition is a light weight process. They are used to increase functionality and performance by performing multiple tasks at the same time, i.e. concurrently. There are two methods for implementing threads in Java,

* Implementing an interface
* Extending a class


I'd assume that the reader is familiar with the basic Object Oriented Paradigm concepts and understands terms like 'extending', 'interface' and 'class'. Now you may start wondering why are there two ways to create threads. This is because if a class is already an inherited class of some class other than 'Thread', then it cannot extend 'Thread' because multiple inheritance is not allowed in the Java programming language. So, in such cases we use 'Runnable' interface instead.

Now, let's jump onto the coding part on how to actually create threads. The first method is to extend or inherit the 'Thread' class. The 'Thread' class is defined in the package java.lang, which needs to be imported. Take a look at the code below to get a better idea,
Code:
import java.lang.*;
 
public class myExample extends Thread
  {
      public void run()
      {
      ....
      }
  }
   

The other way to do this is to implement 'Runnable', as shown below,
Code:
import java.lang.*;
  public class myExample implements Runnable
  {
      Thread T;
      public void run()
      {
      ....
      }
  }
   

Notice that both the methods make use of the 'run()' function, which is responsible for the actions of the thread. The 'Runnable' interface actually is nothing but a class containing only one abstract method, 'public abstract void run();'. Remember an interface only provides a design framework upon which classes can be implemented. It is also interesting to note that actually the 'Thread' class also implements the 'Runnable' interface.



_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Introduction to threads in Java
 Introduction to Java IO.pdf     -  
 Java Remote Method Invocation (RM) Introduction     -  
 threads block on I/O     -  
 visual c++ MFC background threads     -  
 Implementing synchronization in C++ with threads     -  
 Flashing Graphics animations with threads     -  
 Draw Tank with animation and using threads too     -  
 Introduction     -  
 Introduction     -  
 New Member Introduction     -  



Topic Tags

Java Threads
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com