-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmthreadwsm.java
More file actions
45 lines (38 loc) · 960 Bytes
/
mthreadwsm.java
File metadata and controls
45 lines (38 loc) · 960 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
class Oddnum{
//defining synchronized method
synchronized void printoddnum(int n){
for(int i=1;i<=n;i+=2){
System.out.println(i);
}
try{
Thread.sleep(500);
}catch(Exception e){System.out.println(e);}
}
}
class Thread1 extends Thread{
Oddnum o;
Thread1(Oddnum o){
this.o=o;
}
public void run(){
o.printoddnum(10);
}
}
class Thread2 extends Thread{
Oddnum o;
Thread2(Oddnum o){
this.o=o;
}
public void run(){
o.printoddnum(20);
}
}
class ThreadTesting{
public static void main(String args[]){
Oddnum object = new Oddnum();
Thread1 t1 = new Thread1(object);
Thread2 t2 = new Thread2(object);
t1.start();
t2.start();
}
}