Sample Oop Programs Using Java
Sample Oop Programs Using Java
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;
int x;
Square(int n)
x = n;
int sqr = x * x;
int x;
Cube(int n)
x = n;
}
int cub = x * x * x;
s.start();
c.start();
try {
Thread.sleep(1000);
System.out.println(ex);
}
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{
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
Table t;
MyThread1(Table t){
this.t=t;
t.printTable(5);
Table t;
MyThread2(Table t){
this.t=t;
t.printTable(100);
t1.start();
t2.start();
TestSynchronizedBlock.java
class Table
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);}
Table t;
MyThread1(Table t){
this.t=t;
t.printTable(5);
Table t;
MyThread2(Table t){
this.t=t;
t.printTable(100);
}
}
t1.start();
t2.start();
StaticSynchronization.java
class Table
for(int i=1;i<=10;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){}
Table.printTable(1);
Table.printTable(10);
Table.printTable(100);
Table.printTable(1000);
t2.start();
t3.start();
t4.start();
TestInterComm.java
class Customer{
int amount=10000;
System.out.println("going to withdraw...");
if(this.amount<amount){
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;
notify();
class TestInterComm{
new Thread(){
}.start();
new Thread(){
}.start();
}}