Threads in Java Tutorial for Beginners is part of previous tutorial on Java core programming . As we know threads is the important feature of Java programming So , it’s necessary to learn concepts of threads in java .
Threads in Java Course Contents are Given below in Sequence
- Introduction to Multitasking
- Definition of Thread
- Thread Methods
- Synchronization
- InterThread Communication
- Questions
Starting with the Terms related to threads in Java …
What is Process ?
A process is a program in execution on system or computer . Two or more processes running concurrently in a computer is called multitasking. So , multiple process leads to multitasking .
Java also supports for multithreading. A Process can contain multiple threads to execute its different sections. So , Process having multiple threads is multithreading in Java .
These terms are important from many point of views , So I suggest you to keep these terms in mind .
What is Thread?
A thread is a line of execution of code . It is the smallest unit of code that is dispatched by the scheduler.
A process can contain multiple threads to execute its different sections. This is called multithreading.
Advantages of Threads –
- Can be created faster.
- Requires less overhead.
- Interprocess communication is faster.
- Context switching is faster.
- Maximum use of CPU time.
Some Common Thread Methods which are generally used for programming threads in Java codes are given below:
start();
Start the execution of thread.
stop();
Stop the execution of Thread.
suspend();
Suspend the execution of Thread.
resume();
Resume the execution of suspended Thread.
Common Thread Methods
sleep (long);
Suspend the execution of thread for a certain period of time . Time is in milliseconds.
How to Create a Thread?
A thread in Java is created as an Object of Class Thread.
Thread t = new Thread();
A thread in Java can also be created by implementing your class by Runnable Interface.
Example :
By default every Java class contains a main thread where the execution of the program starts.
class sample
{
public static void main (String args)
{
— Print even numbers from 0 to 100
— Print odd numbers from 0 to 100
}
}
Example : (Multithreading)
class sample
{
— New Thread Print even Numbers from 0 to 100.
public static void main (String args)
{
— Create a New Thread t1
— Starts a New Thread t1
— Print odd numbers from 0 to 100
}
}
Example : (Multithreading)
class sample extends Thread
{
— New Thread Print even Numbers from 0 to 100.
public static void main (String args)
{
sample s = new sample();
— Starts a New Thread t1
— Print odd numbers from 0 to 100
}
}
Example : (Multithreading)
class sample extends Thread
{
— New Thread Print even Numbers from 0 to 100.
public static void main (String args)
{
sample s = new sample();
s.start();
— Print odd numbers from 0 to 100
}
}
Example : (Multithreading)
class sample extends Thread
{
public void run() { — even Numbers from 0 to 100 }
public static void main (String args)
{
sample s = new sample();
s.start();
— Print odd numbers from 0 to 100
}
}
Thread Methods
User can create multiple threads in a Java program and set the priority of each.
int getPriority()
setPriority(int);
User can check whether the Thread is alive or dead.
boolean isAlive();
Waits forever for this Thread to die.
join()
Thread Methods
Returns a reference to the currently executing Thread object and is a static method.
currentThread();
Thread Names can be set by using :
String getName();
setName(String);
Example :
Class sample extends Thread
{
public static void main (String args[]) {
Thread t= Thread.currentThread();
System.out.println (t.getName());
System.out.println (t.getPriority());
t.setName (“myThread”);
t.setPriority(2);
System.out.println (t.getName());
System.out.println (t.getPriority());
} }
Synchronization
Two or more Thread accessing the same data simultaneously may lead to loss of data integrity.
For example, when two people access a saving account, it is possible that one person may overdraw and the cheque may bounce.
Monitor
Java uses the concept of monitor or a semaphore to enable this. A monitor is an object, used as a mutually exclusive lock.
At a time, only one thread can access the monitor. A second thread cannot enter the monitor until the first comes out. Till such time, the other thread is said to be waiting.
Monitor
The keyword synchronized is used in the code to enable synchronization.
The word synchronized can be used with a method or within a block.
public void synchronized request();
Inter Thread Communication
Java offers interprocess communication through the usage of wait(), notify() and notifyall() methods of Object class and all are synchronized methods.
Wait() – waits indefinitely on another thread of execution until it receives a notify() or notifyall() message.
Inter Thread Communication
notify() – This method wakes up a single thread waiting on the Object’s monitor.
notifyAll()- This methods wakes up all threads waiting on the Object’s monitor.
Questions ?
Define Thread. In Java what are different ways to create a Thread?
What are different Thread Methods present in class Thread?
Which is the default method executed by Created new Thread.?
What are the advantages of Threads over processes?
Leave a Reply