Concept:
we want access multiple thread using one method
Main Method: Several threads are coming different location than handle it using method
variable names are represented as global it as unique variable as all thread
step:-1 synchronized method used to handle multiple thread and execute one by one thread
with in the main main method variable names are represented because of step by step execution of threads at the time same variable name represented as different thread
step:-2 multiple threads are coming we want execute step by step by using notify and wait
notify means it will be report coming to execution of threads
wait(2000); means that one thread is able to access resource sharing remaining threads won't be access it will wait 2000msec
//main method:
public class Addition
{
public synchronized void add(int a,int b,boolean status)
{
Thread t=Thread.currentThread();
String name=t.getName();
System.out.println(name+"Thread started........");
System.out.println(name+"1st number.....="+a);
System.out.println(name+"2nd number.....="+b);
try
{
if(status)
notify();
else
wait(2000);
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
int c=a+b;
System.out.println(name+"result"+c);
}
}
//example :ThreadA all the method Local variable represented as a global variable by using this
public class ThreadA extends Thread
{
int a,b;
boolean status;
Addition aobj;
public ThreadA(int a,int b,boolean status,Addition aobj)
{
this.a=a;
this.b=b;
this.status=status;
this.aobj=aobj;
setName("A");
start();
}
public void run()
{
aobj.add(a,b,status);
}
}
//example :ThreadB all the method Local variable represented as a global variable by using this
public class ThreadB extends Thread
{
int a,b;
boolean status;
Addition aobj;
public ThreadB(int a,int b,boolean status,Addition aobj)
{
this.a=a;
this.b=b;
this.status=status;
this.aobj=aobj;
setName("B");
start();
}
public void run()
{
aobj.add(a,b,status);
}
}
INTERVIEW QUESTION FOR JAVA
Java program for EMPLOYEE DETAILS
Java program for MULTIPLE THREADS ACCESSING ONE METHOD
Java program for SINGLE THREADS ACCESSING ONE METHOD
Java program for Strin Reverse
java Program for Twin Prime numbers
JAVA PROGRAM FOR EQUILATERAL TRIANGLE
JAVA PROGRAM FOR RIGHT ANGLE TRIANGLE
JAVA PROGRAM FOR SORTING OF ARRAY MINIMUM TO MAXIMUM WITHOUT USING SORTING METHOD
Java Program for Sorting Of Array Maximum to Minimum WITHOUT USING SORTING METHOD
JAVA PROGRAM FOR TO PRINT PRIME NUMBERS
JAVA PROGRAM FOR FIZZBIZZ , FIZZ BIZZ TEST
we want access multiple thread using one method
Main Method: Several threads are coming different location than handle it using method
variable names are represented as global it as unique variable as all thread
step:-1 synchronized method used to handle multiple thread and execute one by one thread
with in the main main method variable names are represented because of step by step execution of threads at the time same variable name represented as different thread
step:-2 multiple threads are coming we want execute step by step by using notify and wait
notify means it will be report coming to execution of threads
wait(2000); means that one thread is able to access resource sharing remaining threads won't be access it will wait 2000msec
public class Addition
{
public synchronized void add(int a,int b,boolean status)
{
Thread t=Thread.currentThread();
String name=t.getName();
System.out.println(name+"Thread started........");
System.out.println(name+"1st number.....="+a);
System.out.println(name+"2nd number.....="+b);
try
{
if(status)
notify();
else
wait(2000);
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
int c=a+b;
System.out.println(name+"result"+c);
}
}
//example :ThreadA all the method Local variable represented as a global variable by using this
public class ThreadA extends Thread
{
int a,b;
boolean status;
Addition aobj;
public ThreadA(int a,int b,boolean status,Addition aobj)
{
this.a=a;
this.b=b;
this.status=status;
this.aobj=aobj;
setName("A");
start();
}
public void run()
{
aobj.add(a,b,status);
}
}
//example :ThreadB all the method Local variable represented as a global variable by using this
public class ThreadB extends Thread
{
int a,b;
boolean status;
Addition aobj;
public ThreadB(int a,int b,boolean status,Addition aobj)
{
this.a=a;
this.b=b;
this.status=status;
this.aobj=aobj;
setName("B");
start();
}
public void run()
{
aobj.add(a,b,status);
}
}
//Main PRogram executed..........now we call all thread's ,method and execute it
public class Program
{
public static void main(String[] args)
{
Addition aobj=new Addition();
new ThreadA(12,13,true,aobj);
new ThreadB(50,13,true,aobj);
}
}
INTERVIEW QUESTION FOR JAVA
Java program for EMPLOYEE DETAILS
Java program for MULTIPLE THREADS ACCESSING ONE METHOD
Java program for SINGLE THREADS ACCESSING ONE METHOD
Java program for Strin Reverse
java Program for Twin Prime numbers
JAVA PROGRAM FOR EQUILATERAL TRIANGLE
JAVA PROGRAM FOR RIGHT ANGLE TRIANGLE
JAVA PROGRAM FOR SORTING OF ARRAY MINIMUM TO MAXIMUM WITHOUT USING SORTING METHOD
Java Program for Sorting Of Array Maximum to Minimum WITHOUT USING SORTING METHOD
JAVA PROGRAM FOR TO PRINT PRIME NUMBERS
JAVA PROGRAM FOR FIZZBIZZ , FIZZ BIZZ TEST
Comments
Post a Comment