Skip to main content

Thread

THREAD
  • Thread is execution instance which have its own execution environent(system resource).
  • Thread is a unit of procees and it is a light weight.
  •  Thread class is define in java.lang package
  •  Jvm can run only runnable type object.
  • Runnable is a interface available in java.lang package.
  •  Runnable have only one abstract method run()
  • Thread class implement runnable interface where run method does not have any implementation
  • Thread implementation started by calling start() method on thread type object.
There are two ways to create thread.
1. By extending thread class and overridng run()
2. By implementing runnable interface and
provide implementation to run()

Commonly used constructors of thread class:

  • Thread()
  • Thread(string name)
  • Thread(runnable r)
  •  Thread(runnable r,string name)

Each thread in jvm has following properties.
1. threadid
2. threadname
3. threadpriority

Thread class provide gettter and setter method to access the properties like

  • getid()
  • getname()
  • setname()
  • getpriority()
  • setpriority()
Method in thread
1.public void start

  • Start the thread execution in seperate stack and calls the run method of thread object
  • You can not run thread twice.

2.public void run()

  • Start the execution of task define in run() current execution stack.(no concurrency)