Skip to content

Commit 9106fa3

Browse files
committed
Synchronization Demo
1 parent 46f77ef commit 9106fa3

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

SynchronisationDemo.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Table{
2+
public synchronized void printTable(int n){
3+
for(int i=1;i<11;i++){
4+
System.out.println(n*i);
5+
try{Thread.sleep(200);}catch(Exception e){}
6+
}
7+
}
8+
}
9+
class MyThread extends Thread{
10+
Table t;
11+
int n;
12+
MyThread(Table t,int n){
13+
this.t = t;
14+
this.n = n;
15+
}
16+
public void run(){
17+
t.printTable(n);
18+
}
19+
}
20+
class SynchronisationDemo{
21+
public static void main(String[] args){
22+
Table tb = new Table();
23+
MyThread t = new MyThread(tb,5);
24+
t.start();
25+
MyThread t2 = new MyThread(tb,50);
26+
t2.start();
27+
}
28+
}

SynchronizationDemo2.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Table{
2+
public static synchronized void printTable(int n){
3+
for(int i=1;i<11;i++){
4+
System.out.println(n*i);
5+
try{Thread.sleep(200);}catch(Exception e){}
6+
}
7+
}
8+
}
9+
class MyThread extends Thread{
10+
Table t;
11+
int n;
12+
MyThread(Table t,int n){
13+
this.t = t;
14+
this.n = n;
15+
}
16+
public void run(){
17+
t.printTable(n);
18+
}
19+
}
20+
class SynchronizationDemo2{
21+
public static void main(String[] args){
22+
Table tb = new Table();
23+
MyThread t = new MyThread(tb,5);
24+
t.start();
25+
//created another table object so now both thread have different resources so we
26+
//need static synchronization
27+
Table tb2 = new Table();
28+
MyThread t2 = new MyThread(tb2,50);
29+
t2.start();
30+
}
31+
}

SynchronizationDemo3.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Table{
2+
public void printTable(int n){
3+
synchronized(this){
4+
for(int i=1;i<11;i++){
5+
System.out.println(n*i);
6+
try{Thread.sleep(200);}catch(Exception e){}
7+
}
8+
}
9+
}
10+
}
11+
class MyThread extends Thread{
12+
Table t;
13+
int n;
14+
MyThread(Table t,int n){
15+
this.t = t;
16+
this.n = n;
17+
}
18+
public void run(){
19+
t.printTable(n);
20+
}
21+
}
22+
class SynchronizationDemo3{
23+
public static void main(String[] args){
24+
Table tb = new Table();
25+
MyThread t = new MyThread(tb,5);
26+
t.start();
27+
28+
MyThread t2 = new MyThread(tb,50);
29+
t2.start();
30+
}
31+
}

SynchronizationDemo4DeadLock.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class SynchronizationDemo4DeadLock{
2+
3+
public static void main(String[] args){
4+
final String res1 = "Icecream";
5+
final String res2 = "Chocolate";
6+
Thread t1 = new Thread(){
7+
public void run(){
8+
synchronized(res1){
9+
System.out.println(res1+" is locked by "+getName());
10+
try{Thread.sleep(200);}catch(Exception e){}
11+
synchronized(res2){
12+
System.out.println(res2+" is locked by "+getName());
13+
try{Thread.sleep(200);}catch(Exception e){}
14+
System.out.println(res2+" is released by "+getName());
15+
}
16+
System.out.println(res1+" is released by "+getName());
17+
}
18+
}
19+
};
20+
t1.start();
21+
Thread t2 = new Thread(){
22+
public void run(){
23+
synchronized(res2){
24+
System.out.println(res2+" is locked by "+getName());
25+
try{Thread.sleep(200);}catch(Exception e){}
26+
synchronized(res1){
27+
System.out.println(res1+" is locked by "+getName());
28+
try{Thread.sleep(200);}catch(Exception e){}
29+
System.out.println(res1+" is released by "+getName());
30+
}
31+
System.out.println(res2+" is released by "+getName());
32+
}
33+
}
34+
};
35+
t2.start();
36+
}
37+
}

0 commit comments

Comments
 (0)