OOPM JAVA - Print table of any number using Synchronized method.

Experiment No. 15

Aim: Write a program to print table of any number using Synchronized method.

Objective: To make aware of multithreading concept and use of Synchronized method to avoid thread interference

Outcome: Students implemented multithreading by using synchronized method.

Theory: 

Synchronization in java language is the capability to manage the access of multiple threads to any shared resources.

Java Synchronization is a better option where we can allow only one thread to manage the shared resource.

Why use Synchronization

The synchronization is mainly used to

1. To prevent thread interference.

2. To prevent consistency problem.

Types of Synchronization

There are two types of synchronization

1. Process Synchronization

2. Thread Synchronization


Thread Synchronization

There are mainly two types of thread synchronization that is inter thread communication and mutual exclusive.

1. Mutual Exclusive

  • Synchronized method.
  • Synchronized block.
  • static synchronization.

2. Cooperation (Inter-thread communication in java)

Java synchronized method

If you say any method as synchronized, it is called as synchronized method.

Synchronization is used to lock an object for shared resources.

When a thread invokes a synchronized method, it automatically acquires the lock for that object and releases it when the thread completes its task.


Understanding the problem without Synchronization

 

class Table{  

void printTable(int n){

//method not synchronized  

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);  }  

}  

class TestSynchronization1{  

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();  

}  

}  


Output: 

       5

       100

       10

       200

       15

       300

       20

       400

       25

       500

class Table{  

 synchronized void printTable(int n){

//synchronized method  

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);  }  

}  

class TestSynchronization1{  

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();  

}  

}  


Output: 

       5

       10

       15

       20

       25

       100

       200

       300

       400

       500


Code : 
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);  
}  
}  
class TestSynchronization1
{  
public static void main(String args[])
{  
Table obj = new Table();
MyThread1 t1=new MyThread1(obj);  
MyThread2 t2=new MyThread2(obj);  
t1.start();  
t2.start();  
}  
}  

Output : 
program to print table of any number using Synchronized method.

Observations and learning: 
In this experiment we implemented multithreading by using synchronized method. We wrote a program to print table of any number using Synchronized method.
 
Conclusion: 
In conclusion to this experiment,we learnt multi threading by using synchronized method.
 
Question of Curiosity 
1. What is synchronization of threads?
Ans : Synchronization in java language is the capability to manage the access of multiple threads to any shared resource.
 
2. Can a static method be synchronized?
Ans : If you make any static method as synchronized, the lock will be on the class not on object.
 
3. What is the use of join method in threads?
Ans : Join method in Java allows one thread to wait until another thread completes its execution. In simpler words, it means it waits for the other thread to die. It has a void type and throws InterruptedException.
 
4. Describe a few other important methods in Threads?
Ans : 
  1. public void run(): is used to perform action for a thread.
  2. public void start(): starts the execution of the thread.JVM calls the run() method on the thread.
  3. public void sleep(long miliseconds): Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.
  4. public void join(): waits for a thread to die.
  5. public void join(long miliseconds): waits for a thread to die for the specified miliseconds.
  6. public int getPriority(): returns the priority of the thread.
  7. public int setPriority(int priority): changes the priority of the thread.
  8. public String getName(): returns the name of the thread.
 
5. What is a deadlock?
Ans : Deadlock is a situation where a set of processes are blocked because each process is holding a resource and waiting for another resource acquired by some other process.

Comments