You are on page 1of 24

MULTITHREADING

IN
JAVA
CALLING RUN

What if we call run() method directly instead start() method?

• Each thread starts in a separate call stack

• Invoking the run() method from main thread, the run() method goes onto the current

call stack rather than at the beginning of a new call stack


LOGIC

class TestCallRun1 extends Thread {


public void run() {
System.out.println("running...");
}
public static void main(String args[]) {
TestCallRun1 t1 = new TestCallRun1();
t1.run(); //fine, but does not start a separate call stack
}
}
JOINS

• The join method allows one thread to wait for the completion of another

• If t is a Thread object whose thread is currently executing

• t.join(); causes the current thread to pause execution until t's thread terminate
SYNTAX

public void join()throws InterruptedException


public void join(long milliseconds)throws InterruptedException

• The join() method waits for a thread to die

• In other words, it causes the currently running threads to stop executing until the

thread it joins with completes its task


LOGIC:

class join extends Thread { public static void main(String


public void run() { args[]) {
for (int i = 1; i <= 5; i++) { TestJoinMethod1 t1 = new
try { TestJoinMethod1();
Thread.sleep(500); TestJoinMethod1 t2 = new
} catch (Exception e) { TestJoinMethod1();
System.out.println(e); TestJoinMethod1 t3 = new
} TestJoinMethod1();
System.out.println(i); t1.start();
} try {
} t1.join(); //add values
inside the method (1500 miliseconds)
} catch (Exception e) {
System.out.println(e);
}
t2.start();
t3.start();
}
}
NAMING THREAD

public String getName(): is used to return the name of a thread.


public void setName(String name): is used to change the name of a thread.

• The Thread class provides methods to change and get the name of a thread By

default, each thread has a name i.e. thread-0, thread-1 and so on

• By we can change the name of the thread by using setName() method

• The syntax of setName() and getName() methods are given below


LOGIC

Class Naming extends Thread { t1.start();


public void run() { t2.start();
System.out.println("running...");
} t1.setName(“sam andwerson”);
public static void main(String System.out.println("After
args[]) { changing name of t1:" + t1.getName());
TestMultiNaming1 t1 = new }
TestMultiNaming1(); }
TestMultiNaming1 t2 = new
TestMultiNaming1();
System.out.println("Name of t1:"
Output: Name of t1:Thread-0
+ t1.getName()); Name of t2:
System.out.println("Name of t2:" Thread-1 id of t1:8
+ t2.getName()); running...
After changeling name of t1:sam andwerson
running...
CURRENT THREAD IN JAVA

● The currentThread() method of Thread class is used to get current


thread in java.
● It is a static method.

public static Thread currentThread()

It returns a reference to the currently running thread


LOGIC

package com.codesjava;

public class Test {


public static void main(String args[]){
Thread currentThread = Thread.currentThread();
System.out.println(currentThread);
}
}
FETCHING THE NAME OF CURRENT THREAD:

class ThreadNaming extends Thread


{

public void run()


{
// getting the current thread 's name.
System.out.println("Fetching current thread name..");
System.out.println(Thread.currentThread().getName());
}
}
CONTINUED…..

class Main
{
public static void main (String[] args)
{
// creating two threads
ThreadNaming t1 = new ThreadNaming();
ThreadNaming t2 = new ThreadNaming();

t1.start();
t2.start();
}
}
Yield

● yield() basically means that the thread is not doing anything particularly
important and if any other threads or processes need to be run, they should run.
Otherwise, the current thread will continue to run.

● Whenever a thread calls java.lang.Thread.yield method, it gives hint to the thread


scheduler that it is ready to pause its execution.

● Thread scheduler is free to ignore this hint.


● If any thread executes yield method , thread scheduler checks if there is any
thread with same or high priority than this thread.

● If processor finds any thread with higher or same priority then it will move
the current thread to Ready/Runnable state and give processor to other
thread and if not – current thread will keep executing.
LOGIC

import java.lang.*;

// MyThread extending Thread


class MyThread extends Thread
{
public void run()
{
for (int i=0; i<5 ; i++)
System.out.println(Thread.currentThread().getName()
+ " in control");
}
}
CONTINUED...

public class yieldDemo


{
public static void main(String[]args)
{
MyThread t = new MyThread();
t.start();

for (int i=0; i<5; i++)


{
// Control passes to child thread
Thread.yield();

// After execution of child Thread


// main thread takes over
System.out.println(Thread.currentThread().getName()
+ " in control");
}
}
}
NOTE:

● Once a thread has executed yield method and there are many threads with
same priority is waiting for processor, then we can't specify which thread will
get execution chance first.
● The thread which executes the yield method will enter in the Runnable state
from Running state.
● Once a thread pauses its execution, we can't specify when it will get chance
again it depends on thread scheduler.
isAlive()

● It tests if this thread is alive.


● A thread is alive if it has been started and has not yet died.
● There is a transitional period from when a thread is running to when a thread
is not running.
● After the run() method returns, there is a short period of time before the
thread stops.
● . This method is used to find out if a thread has actually been started and has
yet not terminated.
LOGIC:

public class oneThread extends Thread {


public void run()
{
System.out.println("F1 ");
try {
Thread.sleep(300);
}
catch (InterruptedException ie) {
}
System.out.println("FORMULA ");
}
public static void main(String[] args)
{
oneThread c1 = new oneThread();
oneThread c2 = new oneThread();
c1.start();
c2.start();
System.out.println(c1.isAlive());
System.out.println(c2.isAlive());
EXAMPLE OF THREAD WITHOUT JOIN

public class oneThread extends Thread {


public void run()
{
System.out.println("FORMULAS.... ");
try {
Thread.sleep(300);
}
catch (InterruptedException ie) {
}
System.out.println("CHEMISTRY..... ");
}
public static void main(String[] args)
{
oneThread c1 = new oneThread();
oneThread c2 = new oneThread();
c1.start();
c2.start();
System.out.println(c1.isAlive());
System.out.println(c2.isAlive());
EXAMPLE OF THREAD WITH JOIN

public class oneThread extends Thread {


public void run()
{
System.out.println("............ ");
try {
Thread.sleep(300);
}
catch (InterruptedException ie) {
}
System.out.println("RUNNING........ ");
}
public static void main(String[] args)
{
oneThread c1 = new oneThread();
oneThread c2 = new oneThread();
c1.start();

try {
c1.join(); // Waiting for c1 to finish
THANK YOU

You might also like