-
Notifications
You must be signed in to change notification settings - Fork 0
/
Multithreading.java
51 lines (44 loc) · 1.3 KB
/
Multithreading.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
class Multithread1 extends Thread{
private int threadNumber;
public Multithread1(int threadNumber){
this.threadNumber = threadNumber;
}
@Override
public void run(){
for(int i = 0 ; i < 5 ; i++){
System.out.println(i + " From Thread threadNumber: " + threadNumber);
try{
Thread.sleep(1000);
} catch(Exception e){
System.out.print(e);
}
}
}
}
class Multithread2 implements Runnable{
private int threadNumber;
public Multithread2(int threadNumber){
this.threadNumber = threadNumber;
}
@Override
public void run(){
for(int i = 0 ; i < 5 ; i ++){
System.out.println(i + " From thread threadNumber: " + threadNumber);
try{
Thread.sleep(1000);
}catch(Exception e){
System.out.print(e);
}
}
}
}
public class Multithreading{
public static void main(String []args){
System.out.println("\nExample of Extending thread\n");
for(int i = 0 ; i < 5 ; i ++)
(new Multithread1(i)).start();
System.out.println("\nExample of Implemanting Runnnable\n");
for(int i = 0 ; i < 5 ; i ++)
(new Thread(new Multithread2(i))).start();
}
}