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: README.md
+61Lines changed: 61 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -225,4 +225,65 @@ public static void main(String[] args){//main thread starts here
225
225
```
226
226
---
227
227
228
+
### 11. Explain thread life cycle in Java
229
+
230
+
Thread has the following states:
231
+
*New
232
+
*Runnable
233
+
*Running
234
+
*Non-runnable (Blocked)
235
+
*Terminated
236
+
237
+

238
+
239
+
240
+
***New:** In New state, Thread instance has been created but start () method is not yet invoked. Now the thread is not considered alive.
241
+
***Runnable:** The Thread is in runnable state after invocation of the start () method, but before the run () method is invoked. But a thread can also return to the runnable state from waiting/sleeping. In this state the thread is considered alive.
242
+
***Running:** The thread is in running state after it calls the run () method. Now the thread begins the execution.
243
+
***Non-Runnable(Blocked):** The thread is alive but it is not eligible to run. It is not in a runnable state but also, it will return to runnable state after some time. For Example: wait, sleep, block.
244
+
***Terminated:** Once the run method is completed then it is terminated. Now the thread is not alive.
245
+
246
+
---
247
+
248
+
### 12. Which methods are used during the Serialization and Deserialization process?
249
+
250
+
ObjectOutputStream and ObjectInputStream classes are higher level java.io. package. We will use them with lower level classes FileOutputStream and FileInputStream.
251
+
252
+
ObjectOutputStream.writeObject —->Serialize the object and write the serialized object to a file.
253
+
254
+
ObjectInputStream.readObject —> Reads the file and deserializes the object.
255
+
256
+
To be serialized, an object must implement the serializable interface. If a superclass implements Serializable, then the subclass will automatically be serializable.
257
+
258
+
| Serialization | Deserialization |
259
+
| ------------- |-------------|
260
+
|Serialization is the process which is used to convert the objects into byte stream|Deserialization is the opposite process of serialization where we can get the objects back from the byte stream.|
261
+
|An object is serialized by writing it an ObjectOutputStream.|An object is deserialized by reading it from an ObjectInputStream.|
262
+
263
+
---
264
+
265
+
### 13. When to use Runnable interface Vs Thread class in Java?
266
+
267
+
If we need our class to extend some other classes other than the thread then we can go with the runnable interface because in java we can extend only one class. If we are not going to extend any class then we can extend the thread class.
0 commit comments