Threads in java

Threads in java

What are threads:For understanding threads we must firstly know,what is a process?
A process is a background activity running besides doing some works at a piece of time.A machine operating system runs a lot of processes to do a task such as opening a program,booting up,loading resources etc.So next question is what is a thread?
A thread is a light weight process,i.e. threads do small piece of work in a particular duration.It has a start start,running state,waiting and terminated state.There is a default thread running in a java program,if you have noticed:its the main thread.


Why do we need threads?
Suppose in a situation where we are creating a project where a program does two or more piece of work at the same time,for example processing a mouse click on a button and opening the other frame of project at the same time.HOW CAN WE DO THAT??The power comes with multithreading.

How threads works?

  • The threads are started using start() method,that gives call to the run() method of the particular thread.The code inside the run() method gets executed.
  • The main method has equal priority as compared to the started thread,thus the code is executed by JVM equally to the main method consecutively.
  • There can be more than one threads running in a program(the main goal of multithreading),and these threads shares the time while running.
  • Thread cannot be restarted once terminated.
  • Threads have a particular life cycle that include states:new,Runnable,waiting and terminated.

Java Thread
How to get threads working?

  1. implement Runnable abstract class
  2. extending a class to Thread class
  3. Anonymous implementation
1.Implementing Runnable class:
Code:class A implements Runnable
{
@Override
void run()
{
//code to run in thread
}
public static void main(String args[])
{
Thread t1=new Thread();//creating a new Thread object
t1.start();
}
}

2.extending thread class:
Extend  the to Thread class(which is a part of java.lang package)
Code:class Thread_class extends Thread
{
void run() 
{
//code to run in thread


}

public static void main(String args[])

{
Thread t1=new Thread();//creating a new Thread object
t1.start();
}
}
}

3.Anonymous implementation
Create a thread using the anonymous class
Code:Thread t1=new Thread(new Runnable()  //here we have implemented method run of runnable Abstract class
{
@Override 
void run()
{
//code to be run in threads
}

});
//starting the thread
t1.start();


Methods of thread:

  1. sleep(time in milliseconds),sleep(time in milliseconds,nanoseconds):sleep method is a static method that forces thread to pause or sleep for sometime(value in the arguments).For example:Thread.sleep(2000);pause thread for 2 seconds.
  2. join():This method waits for another thread to complete.
  3. setpriority/getpriority():Set or get priority of a particular thread.
  4. run():Method that starts a thread
  5. yield()


Comments

Popular posts from this blog

Four building blocks of Object oriented programming language

Exceptions and exceptional handling

Lists in java