Develop a java program that implements a multi-threaded program, which has three
threads. First thread generates a random integer for every 1 second, if the generated
integer is even the second thread computes the square of the number and print it. If the
generated integer is odd the third thread will print the value of cube of the number.
RandomDemo.java
import java.util.Random;
class Square extends Thread
int x;
Square(int n)
x = n;
public void run()
int sqr = x * x;
System.out.println("Square of " + x + " = " + sqr );
class Cube extends Thread
int x;
Cube(int n)
x = n;
}
public void run()
int cub = x * x * x;
System.out.println("Cube of " + x + " = " + cub );
class Number extends Thread
public void run()
Random random = new Random();
for(int i =0; i<10; i++)
int randomInteger = random.nextInt(100);
System.out.println("Random Integer generated : " + randomInteger);
Square s = new Square(randomInteger);
s.start();
Cube c = new Cube(randomInteger);
c.start();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
System.out.println(ex);
}
public class RandomDemo {
public static void main(String args[])
Number n = new Number();
n.start();
}
Develop a java program to identify the use of synchronized blocks, synchronized methods
and static synchronized methods in threads concept.
TestSynchronizedMethod.java
class Table{
synchronized void printTable(int n){
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
class MyThread1 extends Thread{
Table t;
MyThread1(Table t){
this.t=t;
public void run(){
t.printTable(5);
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
public void run(){
t.printTable(100);
public class TestSynchronizedMethod{
public static void main(String args[]){
Table obj = new Table();
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
TestSynchronizedBlock.java
class Table
void printTable(int n){
synchronized(this){//synchronized block
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
}//end of the method
class MyThread1 extends Thread{
Table t;
MyThread1(Table t){
this.t=t;
public void run(){
t.printTable(5);
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
public void run(){
t.printTable(100);
}
}
public class TestSynchronizedBlock{
public static void main(String args[]){
Table obj = new Table();//only one object
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
StaticSynchronization.java
class Table
synchronized static void printTable(int n){
for(int i=1;i<=10;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){}
class MyThread1 extends Thread{
public void run(){
Table.printTable(1);
class MyThread2 extends Thread{
public void run(){
Table.printTable(10);
class MyThread3 extends Thread{
public void run(){
Table.printTable(100);
class MyThread4 extends Thread{
public void run(){
Table.printTable(1000);
public class StaticSynchronization{
public static void main(String t[]){
MyThread1 t1=new MyThread1();
MyThread2 t2=new MyThread2();
MyThread3 t3=new MyThread3();
MyThread4 t4=new MyThread4();
t1.start();
t2.start();
t3.start();
t4.start();
Develop a java program to illustrate the concept of inter thread communication.
TestInterComm.java
class Customer{
int amount=10000;
synchronized void withdraw(int amount){
System.out.println("going to withdraw...");
if(this.amount<amount){
System.out.println("Less balance; waiting for deposit...");
try{wait();}catch(Exception e){}
this.amount-=amount;
System.out.println("withdraw completed...");
}
synchronized void deposit(int amount){
System.out.println("going to deposit...");
this.amount+=amount;
System.out.println("deposit completed... ");
notify();
class TestInterComm{
public static void main(String args[]){
final Customer c=new Customer();
new Thread(){
public void run(){c.withdraw(15000);}
}.start();
new Thread(){
public void run(){c.deposit(10000);}
}.start();
}}