Java – Threading

Java – Multi Threading

Multi-threading is a java process of executing two or more threads (programs) simultaneously to utilize the processor maximum. Multi-tasking: Single Threaded application: class Code{            public static void main(String args[]) {                        int x=10/0;            }} Exception in thread “main” java.lang.ArithmeticException: / by zero

Java – start() method in Thread

start(): Program to define 2 Custom thread and start from main thread: class Default{            public static void main(String[] args)                        {                        First f = new First();                        Second s = new Second();                        f.start();                        s.start();            }}class First extends Thread{            public void run()            {                        for (int i=1 ; i<=100 ; i++)                        {                                    System.out.println(“First : ” + i);                        }            }}class …

Java – start() method in Thread Read More »

Java – sleep() method in Thread

sleep(): class Default{            public static void main(String[] args)            {                        First f = new First();                        Second s = new Second();                        f.start();                        s.start();            }}class First extends Thread{            public void run()            {                        for (int i=1 ; i<=10 ; i++)                        {                                    System.out.println(“First : ” + i);                                    try{                                                Thread.sleep(1000);                                    }catch(Exception e){ }                        }            }}class Second extends Thread{            public void run()            …

Java – sleep() method in Thread Read More »

Java – join() method in Thread

join(): For Example, Consider First thread is calculating sum of first N numbers and second thread has to display the result. The Second thread has to wait until First thread completes calculation. import java.util.Scanner;class Main{            static int n;            public static void main(String[] args)            {                        System.out.println(“***Sum of First N numbers***”);                        Scanner sc = new Scanner(System.in);                        System.out.print(“Enter …

Java – join() method in Thread Read More »

Java – Thread Synchronization

Thread Synchronization: Program: Consider a method increase the value of x by 1 and when multiple threads are trying to invoke the method concurrently, we get odd results. Hence we need to synchronize the value() method. class Modify{            static int x;            synchronized static void value()            {                        x=x+1;            }}class First extends Thread{            public void run()            {                        …

Java – Thread Synchronization Read More »

Java – wait(), notify(), notifyAll() in Thread

wait(), notify() and notifyAll(): Syntax of Calling: wait() notify() notifyAll() synchronized( lockObject ){            while(!condition )            {                        lockObject.wait();            }            //take the action here;} synchronized(lockObject){            //establish_the_condition;            lockObject.notify();            //any additional code} synchronized(lockObject){            establish_the_condition;            lockObject.notifyAll();}

Scroll to Top