0

What is Multithreaded Programming in Java

MultiThread in Java

A multithreaded program contains two or more parts that can run concurrently.Each part of such program is called a thread.
Each thread defines a separate path of execution.
Multithreading is specialized form of multitasking
Multithreading enables you to write very efficient programs that make maximum use of the CPU

Java thread Model

Thread exist in several states.A thread can be running.It can be ready to run as soon as it gets CPU times.A running thred can be temporarily suspends its activity.A suspended thread can be resumed.A thread can be blocked when waiting for a resource

Thread priorities

java assigns to each thread a priority that determines how that thread should be treated with respect to the others

Synchronization:

Multithreading introduces a asynchronous behaviour to your programs.there must be a way for you to enforce synchronicity when you need it

Messaging:

In Spread threads, you have to define how your threads communicate each other.Java messaging system allows a thread to enter a synchronized method on an objects have.

MethodMeaning
getNameObtain a thread Name
getPriorityObtain a thread’s priority
isAliveDetermine if a thread is still Running
joinWait for thread to terminate
runEntry point for thread
sleepSuspend a thread for a period of time
startStart a thread by calling its run method

When java programs starts up,one thread begins running immediately.this is usually called the main thread of our program.

Program

In this program a reference to the current thread is obtained by calling currentThread() .
This reference is stored in the local variable t.Than program calls setName() to change the internal name of thread
Next,a loop counts down from five,pausing one second between each line which is caused by sleep method
The sleep() method in Thread might throw an InterruptedException

output

thread

Pravesh Kumar

Leave a Reply

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