Block Level Synchronization

BlockLevelSyncpackage synchronizationusingmethod;

public class BlockLevelSyncronization {

public static void main(String[] args) {
Course c = new Course();
courseDetail d1 = new courseDetail(1,c);
courseDetail d2 = new courseDetail(2,c);
d1.start();
d2.start();
}

}

class Course{
private String name;
private double cost;
public int duration;

public void Display(int id,String name,double cost, int duration){
this.name = name;
this.cost = cost;
this.duration = duration;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;//thousands of code
synchronized(this){
System.out.println(“Thread :” +id + “Running………”);
for(int i=0; i<5; i++){
System.out.println("Course Name :"+name+" "+"Course Cost :"+cost+"Rs."+" "+"Course Duration :"+duration+" months");
}
try{
Thread.sleep(2000);
}catch(InterruptedException ex){
ex.printStackTrace();
}
System.out.println("ID :"+id+"Finished");
}
}
}

class courseDetail extends Thread{
private int id;
private Course c;
courseDetail(int id,Course c){
this.id = id;
this.c = c;
}
public void run(){
c.Display(id,"Core Java",5000,2);
}

}

2 thoughts on “Block Level Synchronization”

Leave a Reply