Multithreading in JAVA

Multithreading in JAVA

Java provides multithreading while programming this mean we can create multiple threads in java programming. The program that will be a multithreaded program consist of two or more parts and all these parts run at a time parallel and every part have its own functionality that it will perform and the main thing as in operating system multithreading all the threads use all the available resources from CPU.

Multithreading is same as multitasking but it’s in computer terms because we divide our code in a different part and run them currently.

Threading in java
Threading in java

Implementing threads:

We implement Threads in the form of objects in java that contains a method that is called run ();

The run method is the heart of every part or thread.

The run () function is must be called by an object of the thread running parallel to it. A new thread can be created in two ways:

  • Using thread class
  • Converting class into thread

A thread goes from different stages while executing that are as follow:

  • New
  • Runnable
  • Waiting
  • Time waiting
  • Terminated

Every thread has its priority that helps us to find which process is executing faster and which thread have higher priority.

Threads functions:

  • Void start()
  • Void run()
  • Void setPriority()
  • Void interrupt()
  • Void sleep()

Creating thread by extending thread class:

Implementing using runnable interface:

You can also implement methods,

setpriority(), interrupt(), sleep() by using instance.

 

Leave a Reply

Your email address will not be published. Required fields are marked *