-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadSynchronization.java
More file actions
65 lines (65 loc) · 1002 Bytes
/
Copy pathThreadSynchronization.java
File metadata and controls
65 lines (65 loc) · 1002 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package javalab;
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(Exception 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 ThreadSynchronization
{
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();
}
}