Categories
Core JAVA

Multithreading in Java

Thread is a light-weight process. The processing unit is known as a thread. When a process is divided into multiple parts and each part can be executed independently, then it is known as multi-threading or multi-tasking. Multithreading in java is very suitable for game and network applications.

Multithreading

Multithreading is a Java feature that allows concurrent execution of two or more parts of a program for maximum utilization of CPU. Each part of such a program is called a thread. So, threads are light-weight processes within a process

Thread is a predefined class prsent in java.lang package.

This class is responsible to create and manupulate the thread.

class Test
{
static void fact() throws Exception{
System.out.println(Thread.currentThread().getname());
System.out.println("fact()….");
Thread.sleep(5000);
System.out.println("………..");
public static void main(String[] args) throws Exception
{
System.out.println(Thread.currentThread().getname());
System.out.println("hello");
fact();
System.out.println("hi");
reverse();
System.out.println("now i am in main");
}
static void reverse()throws Exception
{
System.out.println(Thread.currentThread().getname());
System.out.println("reverse()…");
Thread.sleep(3000);
System.out.println("………….");
}
}

Following  are the predefined members which are generally used to create and manupulate the thread.

CONSTRUCTOR:-

Thread( String Thread_name)

Thread(Runable object)

Thread(Runable object, String Thread_name)

Methods :-

Static Thread() – It returns the object of current running thread.

String getName()- It returns the name of Thread.

Void setName (string Thread_name) – Assign a name of thread.

Int get priorit()- Returns the priority of thread.

Void setPriority():-Assign a priority to thread.

Void start():- To active the thread. It is responsible to devide process into multiple part.

Void run():- This method contaions the logic of thread .

Void sleep (long millisec):- This method block the thread for a specifi period of time.

Void suspend():-Block the thread until resume () called.

Void resume():- Reactive the suspended thread.

Void join():- Block the Thread until notify() or notify all() called.

Void stop():- move the thread to dead seat.

Boolean isalive():- check the thread exstance live or dead.

Leave a Reply

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