Understanding and implementation on thread in java

How to implement thread in a programe, defination, example, code in java, explaination


Category: Java J2SE Tags: Java J2SE


Introduction

  • A thread of execution is the smallest sequence of programmed instructions that can be
    managed independently by an operating system scheduler.
  • A thread is a light-weight process.
  • Threading concept tends to sharing a address space via multiple threads(small piece of
    program).
  • Multiple threads can exist within the same process and share resources such as memory.
  • Mutli-threading is way to execute multi-threads.
  • The all threads execute independently on each other.
  • Threading needs synchronization between them.

Real Example:-a golf course, which many golfers use at the same time.


Difference between thread and process:-

  • processes are typically independent, while threads exist as subsets of a process.
  • processes have separate address spaces, whereas threads share their address space.
  • context switching between threads in the same process is typically faster than context
    switching between processes.

Learn with code:

First demo class:

class demo extends Thread
{
public void run()
{
for(int i=0;i<10;i++)
System.out.println(getName()+" i="+i);
}
demo()
{
start();
}
}

Explanation:

                  Creating a thread using this method simply involves creating a new class which extends the thread class and overriding run method. to make run method to execute you will be calling start method to start method to start the execution of thread.

Now second class:

class temp
{
public static void main(String[] args)
{
demo d1 = new demo();
demo d2 = new demo();
}
}

Explanation:

        Now in this class we make two objects of demo class, when we make objects the default constructor will called, because constructor have start method, the start method execute and it will invoke the run method.so thread will start to execute.here we are executing for loop in run from 0-9 and in printf getName method gives the

name of thread.


The output:


Thread-0 i=0

Thread-0 i=1

Thread-0 i=2

Thread-0 i=3

Thread-0 i=4

Thread-0 i=5

Thread-0 i=6

Thread-0 i=7

Thread-1 i=0

Thread-1 i=1

Thread-1 i=2

Thread-1 i=3

Thread-1 i=4

Thread-1 i=5

Thread-1 i=6

Thread-1 i=7

Thread-1 i=8

Thread-1 i=9

Thread-0 i=8

Thread-0 i=9


Understanding with image:


 
  


                      So here we showing life cycle of a thread, new thread comes then start running,

may be blocked by I/O and go to  sleep state where it stopped executing, then it notify to I/O

completed then it start execution again and completed the execution, and exists called dead stage.


Thread Methods in java:

  1. Thread.sleep()

This static method asks the system to put the current thread to sleep for (approximately) the specified amount of time, effectively allowing us to implement a "pause". A thread can be interrupted from its sleep.


        2. interrupt()


As mentioned, you can call a Thread object's interrupt() method to interrupt the corresponding thread if it is sleeping or waiting. The corresponding thread will "wake up" with an IOException at some point in the future.


        3. setPriority() / getPriority()


Sets and queries some platform-specific priority assignment of the given thread. When calculating a priority value, it's good practice to always do so in relation to the constants Thread.MIN_PRIORITY, Thread.NORM_PRIORITY and Thread.MAX_PRIORITY. In practice, values go from 1 to 10, and map on to some machine-specific range of values: nice values in the case of Linux, and local thread priorities in the case of Windows. These are generally the range of values of "normal" user threads, and the OS will actually still run other threads beyond these values (so, for example, you can't preempt the mouse pointer thread by setting a thread to MAX_PRIORITY!).


        4. join()


The join() method is called on the Thread object representing enother thread. It tells the current thread to wait for the other thread to complete. To wait for multiple threads at a time, you can use a CountDownLatch.


        5. Thread.yield()


This method effectively tells the system that the current thread is "willing to relinquish the CPU". What it actually does is quite system-dependent.


        6. setName() / getName()


Threads have a name attached to them. By default, Java will attach a fairly dull name such as Thread-12. But for debugging purposes you might want to attach a more meaningful name such as Animation Thread, WorkerThread-10 etc. (Some of the variants of the Thread constructor actually allow you to pass in a name from the start, but you can always change it later.


Like 0 People
Last modified on 11 October 2018
Nikhil Joshi

Nikhil Joshi
Ceo & Founder at Dotnetlovers
Atricles: 132
Questions: 9
Given Best Solutions: 9 *

Comments:

No Comments Yet

You are not loggedin, please login or signup to add comments:

Existing User

Login via:

New User



x