Thread In JAVA:

Thread In JAVA:

Share It With Your Firends:

A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently. Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority.

A thread in Java is the direction or path that is taken while a program is being executed. Generally, all the programs have at least one thread, known as the main thread, that is provided by the JVM or Java Virtual Machine at the starting of the program’s execution.

why do we need Thread?

Thread is a light weight process which helps in running the tasks in parallel. The threads works independently and provides the maximum utilization of the CPU, thus enhancing the CPU performance. Threads to make Java application faster by doing multiple things at same time.

class Process1 extends Thread{
    public void run(){
        while (true){
            System.out.println("Process 1####");
        }
    }
}

class Process2 extends Thread{
    public void run(){
        while (true){
            System.out.println("Process 2----");
        }
    }
}
public class tut14 {
    public static void main(String[] args) {
        Process1 p1 = new Process1();
        Process2 p2 = new Process2();
        p1.start();
        p2.start();
    }
}

Runnable Thread In JAVA:

The runnable interface is used to write applications which can run in a separate thread. Any class that implements the runnable interface is called a thread. The code of the thread is executed by the interpreter after the thread is started.

why do we need runnable thread in java?

The reason we need to pass the runnable object to the thread object’s constructor is that the thread must have some way to get to the run() method we want the thread to execute. Hence, ot is the runnable object we passed to the thread’s constructor.

package com.company;

class MyRunnable1 implements Runnable{
    public void run(){
        system.out.println("Running thread 1")
    }
}


class MyRunnable2 implements Runnable{
    public void run(){
        system.out.println("Running thread 2")
    }
}


public class cwh_71_runnable {
    public static void main(String[] args) {

        Runnable1 bullet1 = new Runnable1();
        Thread gun1 = new Thread(bullet1);

        Runnable2 bullet2 = new Runnable2();
        Thread gun2 = new Thread(bullet2);

        gun1.start();
        gun2.start();
    }
}

Thread Life Cycle:

A thread goes through various stages in its lifecycle. For example, a thread is born, started, runs, and then dies. The following diagram shows the complete life cycle of a thread. Following are the stages of the life cycle − New − A new thread begins its life cycle in the new state.

Life Cycle of Thread in Java is basically state transitions of a thread that starts from its birth and ends on its death. When an instance of a thread is created and is executed by calling start() method of Thread class, the thread goes into runnable state.

Thread Methods:

  1. start() – Starts the thread.
  2. getState() – It returns the state of the thread.
  3. getName() – It returns the name of the thread.
  4. getPriority() – It returns the priority of the thread.
  5. sleep() – Stop the thread for the specified time.
  6. Join() – Stop the current thread until the called thread gets terminated.
import java.io.*;
import java.lang.Thread;
public class ThreadMethod {
    public static void main(String[] args)
    {
        try {
            for (int i = 1; i <=5; i++) {
                Thread.sleep(2000);
                System.out.println(i);
            }
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

Share It With Your Friends

Leave a Reply

Recent Posts

  • All Post
  • A.I.
  • AI
  • c
  • c++
  • computer
  • cryptocurrency
  • database
  • digital marketing
  • finance
  • hacking
  • HTML
  • java
  • Marketing
  • network
  • other
  • programming
  • python
  • react
  • social
  • Tools
  • Uncategorized
  • web devlopment
    •   Back
    • drawing In Python
DIY AI Voice Assistant In Python

September 8, 2023

Build your own AI assistant with Python! Learn to recognize voice commands, generate responses, and more. Create your virtual companion…

share it

Recent Posts

  • All Post
  • A.I.
  • AI
  • c
  • c++
  • computer
  • cryptocurrency
  • database
  • digital marketing
  • finance
  • hacking
  • HTML
  • java
  • Marketing
  • network
  • other
  • programming
  • python
  • react
  • social
  • Tools
  • Uncategorized
  • web devlopment
    •   Back
    • drawing In Python

Additional Content

CodeWithAM

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus luctus.

Products

Automated Chatbot

Data Security

Virtual Reality

Communication

Support

Services

FAQ's

Privacy Policy

Terms & Condition

Team

Contact Us

Company

About Us

Services

Features

Our Pricing

Latest News

© 2023 Codewitham