-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiplicationMultiThreading.java
63 lines (53 loc) · 1.33 KB
/
MultiplicationMultiThreading.java
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
public class MultiplicationMultiThreading {
public static void main(String[] args) {
multiplicationTable table=new multiplicationTable();
MyThread1 thread1=new MyThread1(table);
thread1.start();
MyThread2 thread2=new MyThread2(table);
thread2.start();
}
}
class MyThread1 extends Thread{
multiplicationTable table;
MyThread1(multiplicationTable table){
this.table=table;
}
public void run() {
table.getThreadDetails();
table.printTable(5);
}
}
class MyThread2 extends Thread{
multiplicationTable table;
MyThread2(multiplicationTable table){
this.table=table;
}
public void run() {
table.getThreadDetails();
table.printTable(7);
}
}
class multiplicationTable{
synchronized void printTable(int number) {
/* synchronized(this) { */
for(int i=1;i<=10;i++) {
if(i<10) {
System.out.print(" ");
}
System.out.println(i+" x " +number+" = "+i*number);
}
/* } */
}
void getThreadDetails() {
synchronized(this){
System.out.println(Thread.currentThread().getName());
System.out.println("The priority of Thread is " +Thread.currentThread().getPriority());
if(Thread.currentThread().isAlive()) {
System.out.println("The thread is running");
}
else {
System.out.println("The thread is not running");
}
}
}
}