-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadSynchronisation.java
More file actions
67 lines (63 loc) · 1.45 KB
/
Copy pathThreadSynchronisation.java
File metadata and controls
67 lines (63 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package newproj;
import java. util.Scanner;
public class ThreadSynchronisation {
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
multiTable tab=new multiTable();
System.out.println("Enter the number 1");
int number1=sc.nextInt();
System.out.println("Enter the number 2");
int number2=sc.nextInt();
System.out.println("Enter the number 3");
int number3=sc.nextInt();
thread1 t1= new thread1(tab,number1);
thread2 t2= new thread2(tab,number2);
thread3 t3=new thread3(tab,number3);
t1.start();
t2.start();
t3.start();
}
}
class multiTable{
synchronized void printTable(int num) { //synchronized used as a function
/*synchronized (this) {*/ //synchronized is used as a block
for(int i=1;i<=10;i++) {
System.out.println(num+"*"+i+"="+num*i);
}
/*}*/ //synchronized can be used in block as well as in function
System.out.println();
}
}
class thread1 extends Thread{
int number;
multiTable m;
thread1(multiTable in,int num){
this.m=in;
this.number=num;
}
public void run() {
m.printTable(number);
}
}
class thread2 extends Thread{
int number;
multiTable m;
thread2(multiTable in,int num){
this.m=in;
this.number=num;
}
public void run() {
m.printTable(number);
}
}
class thread3 extends Thread{
int number;
multiTable m;
thread3(multiTable in,int num){
this.m=in;
this.number=num;
}
public void run() {
m.printTable(number);
}
}