-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcwh_69_72_Multithreading.java
32 lines (24 loc) · 1.08 KB
/
cwh_69_72_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
package company;
public class cwh_69_72_Multithreading {
public static void main(String[] args) {
/*
Multi-processing and multi-threading both are used to achieve multitasking.
In a nut shell:
-threads are shared memory area.
-Threads --> faster context switching.
-a thread is light-weight whereas a process is heavy-weight.
Example: A word processor can have one thread running in foreground as an editor
and another in the background auto-saving the document.
Creating a thread:
There are two ways to create a thread in Java-
1> By extending Thread class.
2> By implementing Runnable interface.
Life cycle of a thread:
1. New --> Instance of thread created which is not yet started by invoking start().
2. Runnable --> After invocation of start() & before it is selected to be run by the scheduler.
3. Running --> After thread scheduler has selected it.
4. New Runnable --> Thread alive, not eligible to run.
5. Terminated --> run() method has exited.
*/
}
}