You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _moreReadMe/multithreading/README.md
+60-53Lines changed: 60 additions & 53 deletions
Original file line number
Diff line number
Diff line change
@@ -72,9 +72,9 @@ Thread class provide constructors and methods to create and perform operations o
72
72
<li><strong>public Thread.State getState(): </strong>returns the state of the thread.</li>
73
73
<li><strong>public boolean isAlive(): </strong>tests if the thread is alive.</li>
74
74
<li><strong>public void yield(): </strong>causes the currently executing thread object to temporarily pause and allow other threads to execute.</li>
75
-
<li><strong>public void suspend(): </strong>is used to suspend the thread(depricated).</li>
76
-
<li><strong>public void resume(): </strong>is used to resume the suspended thread(depricated).</li>
77
-
<li><strong>public void stop(): </strong>is used to stop the thread(depricated).</li>
75
+
<li><strong>public void suspend(): </strong>is used to suspend the thread(deprecated).</li>
76
+
<li><strong>public void resume(): </strong>is used to resume the suspended thread(deprecated).</li>
77
+
<li><strong>public void stop(): </strong>is used to stop the thread(deprecated).</li>
78
78
<li><strong>public boolean isDaemon(): </strong>tests if the thread is a daemon thread.</li>
79
79
<li><strong>public void setDaemon(boolean b): </strong>marks the thread as daemon or user thread.</li>
80
80
<li><strong>public void interrupt(): </strong>interrupts the thread.</li>
@@ -95,36 +95,37 @@ The Runnable interface should be implemented by any class whose instances are in
95
95
96
96
1. Thread Example by extending Thread class
97
97
```java
98
-
classMultiextendsThread{
99
-
publicvoidrun(){
100
-
System.out.println("thread is running...");
101
-
}
102
-
publicstaticvoidmain(Stringargs[]){
103
-
Multi t1=newMulti();
104
-
t1.start();
105
-
}
106
-
}
98
+
classMultiextendsThread {
99
+
publicvoidrun() {
100
+
System.out.println("thread is running...");
101
+
}
102
+
publicstaticvoidmain(Stringargs[]) {
103
+
Multi t1=newMulti();
104
+
t1.start();
105
+
}
106
+
}
107
107
```
108
108
109
109
2. Thread Example by implementing Runnable interface
110
110
```java
111
-
classMulti3implementsRunnable{
112
-
publicvoidrun(){
113
-
System.out.println("thread is running...");
114
-
}
115
-
116
-
publicstaticvoidmain(Stringargs[]){
117
-
Multi3 m1=newMulti3();
118
-
Thread t1 =newThread(m1);
119
-
t1.start();
120
-
}
121
-
}
111
+
classMulti3implementsRunnable {
112
+
publicvoidrun() {
113
+
System.out.println("thread is running...");
114
+
}
115
+
116
+
publicstaticvoidmain(Stringargs[]) {
117
+
Multi3 m1=newMulti3();
118
+
Thread t1 =newThread(m1);
119
+
t1.start();
120
+
}
121
+
}
122
122
```
123
123
124
124
## Thread Scheduler in Java
125
125
Thread scheduler in java is the part of the JVM that decides which thread should run. There is no guarantee that which runnable thread will be chosen to run by the thread scheduler. Only one thread at a time can run in a single process. The thread scheduler mainly uses preemptive or time slicing scheduling to schedule the threads.
126
126
127
127
**Difference between preemptive scheduling and time slicing**
128
+
128
129
Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors.
129
130
130
131
**NOTE :**
@@ -139,30 +140,35 @@ The sleep() method of Thread class is used to sleep a thread for the specified a
139
140
```java
140
141
Thread.sleep(500)
141
142
```
142
-
At a time only one thread is executed. If you sleep a thread for the specified time,the thread shedular picks up another thread and so on.
143
+
At a time only one thread is executed. If you sleep a thread for the specified time,the thread scheduler picks up another thread and so on.
143
144
144
145
## run() method in Java
145
146
In Java, Each thread starts in a separate call stack. Invoking the run() method from main thread, the run() method goes onto the current call stack rather than at the beginning of a new call stack.
146
147
147
148
**Problem if you use run() directly instead of start() "**
149
+
148
150
There is no context-switching in the below program because here t1 and t2 will be treated as normal object not thread object.
149
151
Output will be : 1 2 3 4 5 1 2 3 4 5 (One object will finish before starting next)
e1=e2; //now, the first object referred by e1 is available for garbage collection
356
363
```
357
-
- By annonymous object
364
+
- By anonymous object
358
365
```java
359
366
newEmployee();
360
367
```
@@ -367,7 +374,7 @@ protected void finalize(){}
367
374
```
368
375
**NOTE :** The Garbage collector of JVM collects only those objects that are created by new keyword. So if you have created any object without new, you can use finalize method to perform cleanup processing (destroying remaining objects).
369
376
370
-
**gc() method**
377
+
**gc() method:**
371
378
The gc() method is used to invoke the garbage collector to perform cleanup processing. The gc() is found in System and Runtime classes. This method is defined in System class as:
0 commit comments