-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPractice14.java
More file actions
90 lines (79 loc) · 1.75 KB
/
Practice14.java
File metadata and controls
90 lines (79 loc) · 1.75 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
Multithreading: When multiple thread(processes) execute(run) simultaneously.
A thread is a lightweight sub-process, the small unit of processing.
- There are two to achieve it:
1) using runnable interface
2) using thread class
class Thread
{
Thread(Runnable r1)
{
}
}
*/
// Using "Runnable interface"
class Process1 implements Runnable
{
public void run() // Runnable interface has run method but it is public and abstract by default. So we havr to override it.
{
int i;
for(i = 0; i <= 10; i++)
{
System.out.println("Process1: " + i);
}
}
}
class Process2 implements Runnable
{
public void run()
{
int i;
for(i = 0; i <= 10; i++)
{
System.out.println("Process2: " + i);
}
}
}
// Using "Thread class"
class Process3 extends Thread
{
public void run() // Runnable interface has run method but it is public and abstract by default. So we havr to override it.
{
int i;
for(i = 0; i <= 10; i++)
{
System.out.println("Process3: " + i);
}
}
}
class Process4 extends Thread
{
public void run()
{
int i;
for(i = 0; i <= 10; i++)
{
System.out.println("Process4: " + i);
}
}
}
class Practice14
{
public static void main(String
[] args)
{
/* Runnable interface
Process1 p1 = new Process1();
Process2 p2 = new Process2();
Thread t1 = new Thread(p1);
Thread t2 = new Thread(p2);
t1.start(); // Thread has start() method
t2.start();
*/
// Thread class
Process3 p3 = new Process3();
Process4 p4 = new Process4();
p3.start();
p4.start();
}
}