-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadSynch.java
More file actions
51 lines (51 loc) · 958 Bytes
/
Copy pathThreadSynch.java
File metadata and controls
51 lines (51 loc) · 958 Bytes
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
package OOPJava;
import java.util.*;
class Table{
void printTable(int n) {
synchronized(this) {
for(int i=1;i<=10;i++) {
System.out.println(+n+"*"+i+"="+(n*i));
try {
Thread.sleep(400);
}
catch(Throwable e) {
System.out.println(e);
}
}
}
}
}
class Mythread1 extends Thread{
Table t;
int n;
Mythread1(Table t){
this.t=t;
}
public void run() {
t.printTable(n);
}
}
class Mythread2 extends Thread{
Table t;
int n;
Mythread2(Table t){
this.t=t;
}
public void run() {
t.printTable(n);
}
}
public class ThreadSynch {
public static void main(String[]args) {
Table t=new Table();
Scanner sc=new Scanner(System.in);
Mythread1 th1=new Mythread1(t);
Mythread2 th2=new Mythread2(t);
System.out.println("Enter the table you want to run by Thread1:");
th1.n=sc.nextInt();
System.out.println("Enter the table you want to run by Thread2:");
th2.n=sc.nextInt();
th1.start();
th2.start();
}
}