Added 5th and 6th program
This commit is contained in:
43
Sync6.java
Normal file
43
Sync6.java
Normal file
@@ -0,0 +1,43 @@
|
||||
// 6. write a java program for demonstrating Thread Synchronization
|
||||
class Table {
|
||||
synchronized void printTable(int n){
|
||||
for(int i = 1; i<= 5; i++){
|
||||
System.out.println(n * i);
|
||||
try {
|
||||
Thread.sleep(200);
|
||||
} catch (Exception e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Thread1 extends Thread {
|
||||
Table t;
|
||||
Thread1 (Table t){
|
||||
this.t = t;
|
||||
}
|
||||
public void run(){
|
||||
t.printTable(5);
|
||||
}
|
||||
}
|
||||
|
||||
class Thread2 extends Thread {
|
||||
Table t;
|
||||
Thread2(Table t){
|
||||
this.t = t;
|
||||
}
|
||||
public void run(){
|
||||
t.printTable(100);
|
||||
}
|
||||
}
|
||||
|
||||
public class Sync6 {
|
||||
public static void main(String args[]){
|
||||
Table obj = new Table();
|
||||
Thread1 t1 = new Thread1(obj);
|
||||
Thread2 t2 = new Thread2(obj);
|
||||
t1.start();
|
||||
t2.start();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user