0% found this document useful (0 votes)
11 views3 pages

Synchronized Method Explanation

Uploaded by

snikith7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Synchronized Method Explanation

Uploaded by

snikith7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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);
}}
public class TestSynchronization2{
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();
} }
----------------------------------------------
The provided code demonstrates a simple example of thread synchronization in Java. It works
alongside the previously shared classes Table, MyThread1, and MyThread2.
Here's the detailed explanation of this code:

Code Breakdown
1. Class TestSynchronization2
This is the main class where the program's execution begins. It demonstrates the creation and
interaction of threads with a shared object.
2. The main Method
 Step 1: Create a shared Table object
java

Table obj = new Table();


A single instance of the Table class is created. This object is shared between two threads
(t1 and t2).
 Step 2: Create two threads (MyThread1 and MyThread2)

MyThread1 t1 = new MyThread1(obj);


MyThread2 t2 = new MyThread2(obj);
o MyThread1 is initialized with the shared Table object and calls printTable(5).
o MyThread2 is initialized with the same shared Table object and calls printTable(100).
 Step 3: Start the threads

t1.start();
t2.start();
The start() method begins the execution of run() methods in both MyThread1 and
MyThread2.

Synchronization in Action
 Both threads (t1 and t2) are operating on the same shared Table object (obj).
 The synchronized keyword in the printTable method ensures that only one thread at a time
can execute the method for the shared object.
 If t1 is printing the multiplication table of 5, t2 must wait until t1 finishes. Only after t1 is
done, t2 can execute printTable(100).

Expected Output
When the program is executed, the output will be:

5
10
15
20
25
100
200
300
400
500
This output is not interleaved because the printTable method is synchronized, ensuring serialized
access to the method.

What Happens Without Synchronization?


If the synchronized keyword is removed from the printTable method, the threads may interleave,
and the output would be unpredictable, such as:

5
100
10
200
15
300
20
400
25
500
This happens because both threads are accessing the shared object (obj) simultaneously without
any synchronization mechanism.

Key Concepts Illustrated


1. Shared Resource
o A single Table object (obj) is shared between two threads.
2. Thread Synchronization
o The synchronized keyword in the printTable method ensures thread-safe access to
the method, preventing race conditions.
3. Thread Lifecycle
o The start() method begins the execution of each thread, and the threads execute the
run() method concurrently.

Summary
This program demonstrates how multiple threads can safely operate on a shared object by using
synchronization. The synchronized keyword ensures that only one thread can execute the critical
section (printTable method) at a time, thereby maintaining consistent behavior.

You might also like