-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmthreadsm.java
More file actions
53 lines (46 loc) · 1.2 KB
/
mthreadsm.java
File metadata and controls
53 lines (46 loc) · 1.2 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
class Oddnum{
defining method which is not synchronized
void printoddnum(int n){
//loop
for(int i=1;i<=n;i+=2){
System.out.println(i);
}
try{
//sleep method from thread class
Thread.sleep(500);
}catch(Exception e){System.out.println(e);}
}
}
//creating class which extends thread
class Thread1 extends Thread{
Oddnum o;
Thread1(Oddnum o){
//this keyword to refer current object
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{
//main method
public static void main(String args[]){
//Creating object to access Oddnum class
Oddnum object = new Oddnum();
//creating thread
Thread1 t1 = new Thread1(object);
Thread2 t2 = new Thread2(object);
//starting thread by invoking run()
t1.start();
t2.start();
}
}