Skip to content

Commit 1ee575c

Browse files
Update README.md
1 parent 470c30f commit 1ee575c

File tree

1 file changed

+60
-53
lines changed

1 file changed

+60
-53
lines changed

_moreReadMe/multithreading/README.md

Lines changed: 60 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ Thread class provide constructors and methods to create and perform operations o
7272
<li><strong>public Thread.State getState(): </strong>returns the state of the thread.</li>
7373
<li><strong>public boolean isAlive(): </strong>tests if the thread is alive.</li>
7474
<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>
7878
<li><strong>public boolean isDaemon(): </strong>tests if the thread is a daemon thread.</li>
7979
<li><strong>public void setDaemon(boolean b): </strong>marks the thread as daemon or user thread.</li>
8080
<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
9595

9696
1. Thread Example by extending Thread class
9797
```java
98-
class Multi extends Thread{
99-
public void run(){
100-
System.out.println("thread is running...");
101-
}
102-
public static void main(String args[]){
103-
Multi t1=new Multi();
104-
t1.start();
105-
}
106-
}
98+
class Multi extends Thread {
99+
public void run() {
100+
System.out.println("thread is running...");
101+
}
102+
public static void main(String args[]) {
103+
Multi t1 = new Multi();
104+
t1.start();
105+
}
106+
}
107107
```
108108

109109
2. Thread Example by implementing Runnable interface
110110
```java
111-
class Multi3 implements Runnable{
112-
public void run(){
113-
System.out.println("thread is running...");
114-
}
115-
116-
public static void main(String args[]){
117-
Multi3 m1=new Multi3();
118-
Thread t1 =new Thread(m1);
119-
t1.start();
120-
}
121-
}
111+
class Multi3 implements Runnable {
112+
public void run() {
113+
System.out.println("thread is running...");
114+
}
115+
116+
public static void main(String args[]) {
117+
Multi3 m1 = new Multi3();
118+
Thread t1 = new Thread(m1);
119+
t1.start();
120+
}
121+
}
122122
```
123123

124124
## Thread Scheduler in Java
125125
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.
126126

127127
**Difference between preemptive scheduling and time slicing**
128+
128129
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.
129130

130131
**NOTE :**
@@ -139,30 +140,35 @@ The sleep() method of Thread class is used to sleep a thread for the specified a
139140
```java
140141
Thread.sleep(500)
141142
```
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.
143144

144145
## run() method in Java
145146
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.
146147

147148
**Problem if you use run() directly instead of start() "**
149+
148150
There is no context-switching in the below program because here t1 and t2 will be treated as normal object not thread object.
149151
Output will be : 1 2 3 4 5 1 2 3 4 5 (One object will finish before starting next)
150152
```java
151-
class TestCallRun extends Thread{
152-
public void run(){
153-
for(int i=1;i<5;i++){
154-
try{Thread.sleep(500);}catch(InterruptedException e){System.out.println(e);}
155-
System.out.println(i);
156-
}
157-
}
158-
public static void main(String args[]){
159-
TestCallRun2 t1=new TestCallRun2();
160-
TestCallRun2 t2=new TestCallRun2();
161-
162-
t1.run();
163-
t2.run();
164-
}
165-
} } }
153+
class TestCallRun extends Thread {
154+
public void run() {
155+
for (int i = 1; i < 5; i++) {
156+
try {
157+
Thread.sleep(500);
158+
} catch (InterruptedException e) {
159+
System.out.println(e);
160+
}
161+
System.out.println(i);
162+
}
163+
}
164+
public static void main(String args[]) {
165+
TestCallRun2 t1 = new TestCallRun2();
166+
TestCallRun2 t2 = new TestCallRun2();
167+
168+
t1.run();
169+
t2.run();
170+
}
171+
}
166172
```
167173

168174
## join() method in Java
@@ -193,8 +199,8 @@ t1.setName("My sweet thread");
193199
```
194200

195201
Getting the Current Thread
196-
**public static Thread currentThread():**
197-
The currentThread() method returns a reference of currently executing thread.
202+
203+
**public static Thread currentThread():** The currentThread() method returns a reference of currently executing thread.
198204
```java
199205
public void run(){
200206
System.out.println(Thread.currentThread().getName());
@@ -323,16 +329,17 @@ Each thread run in a separate **callstack**.
323329

324330
![callstack](https://user-images.githubusercontent.com/2780145/35023526-11302878-fb61-11e7-942a-da830e4cd714.JPG)
325331
```java
326-
class TestMultitasking1 extends Thread{
327-
public void run(){
328-
System.out.println("task one");
329-
}
330-
public static void main(String args[]){
331-
TestMultitasking1 t1=new TestMultitasking1();
332-
TestMultitasking1 t2=new TestMultitasking1();
333-
t1.start();
334-
t2.start();
335-
} }
332+
class TestMultitasking1 extends Thread {
333+
public void run() {
334+
System.out.println("task one");
335+
}
336+
public static void main(String args[]) {
337+
TestMultitasking1 t1 = new TestMultitasking1();
338+
TestMultitasking1 t2 = new TestMultitasking1();
339+
t1.start();
340+
t2.start();
341+
}
342+
}
336343
```
337344

338345
## Java Garbage Collection
@@ -354,7 +361,7 @@ Employee e1=new Employee();
354361
Employee e2=new Employee();
355362
e1=e2; //now, the first object referred by e1 is available for garbage collection
356363
```
357-
- By annonymous object
364+
- By anonymous object
358365
```java
359366
new Employee();
360367
```
@@ -367,7 +374,7 @@ protected void finalize(){}
367374
```
368375
**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).
369376

370-
**gc() method**
377+
**gc() method:**
371378
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:
372379
```java
373380
public static void gc(){}

0 commit comments

Comments
 (0)