-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh13_72_CreatingThreadExtension.java
More file actions
58 lines (48 loc) · 2.23 KB
/
Ch13_72_CreatingThreadExtension.java
File metadata and controls
58 lines (48 loc) · 2.23 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
class InfiniteLoop1 extends Thread{
public void run(){
while (true) {
System.out.println("Name is: Vedant");
}
}
}
class InfiniteLoop2 extends Thread{
public void run(){
while (true){
System.out.println("Birth Year: 2007");
}
}
}
public class Ch13_72_CreatingThreadExtension {
public static void main(String[] args) {
System.out.println();
/* Concurrency vs Parallelism -
1. Concurrency is handling many task at a same time but keeping focus at only one of the task.
Ex. Making food and using phone at once but can only focus at one thing at a time.
2. Parallelism is handling many task at a same time with each task running side by side.
Ex. Two chefs making different dishes, here two dishes are made without removing focus from one.
*/
/* 1. Creating a thread using extension or inheritance -
Syntax -
class Class_Name extends Thread {
// Piece of code you want to write
// * And to create method use name run() - should be public
}
To initiate the code -
* rather than writing object.methodName(); we here use -
object.start(); // It executes the run() method.
*/
// Note - Now the code will run in concurrency - (creating an infinite loop to show that, the system give allotted time to two loops one by one i.e. concurrency)
// Executing -
InfiniteLoop1 obj1 = new InfiniteLoop1();
InfiniteLoop2 obj2 = new InfiniteLoop2();
obj1.start();
obj2.start();
// Notice that sometimes obj1 method is given time then obj2 is used. And if threading would have not been used first obj1 would have been executed then obj2.
// Not only .start() there are many other - methods related to Threads. https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html (Check method summary)
}
}
/* In a nutshell -
1. There are two ways to use threads , here the discussion is on Extension based threads
2. These follow concurrency method that is time is given separately to different threads
3. There are fixed syntax to access these threads refer Line 29 - 38
*/