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
This means a class cannot inherit multiple classes. However, a class **can** implement multiple interfaces.
536
541
537
542
---
543
+
538
544
<sup><sub>[▲ TOP](#table-of-contents)</sub></sup>
539
545
540
546
## Java Generics
@@ -564,6 +570,7 @@ public class Box<T> {
564
570
}
565
571
```
566
572
---
573
+
567
574
<sup><sub>[▲ TOP](#table-of-contents)</sub></sup>
568
575
569
576
## Serialization
@@ -607,7 +614,7 @@ The return value of `readObject()` should be cast to the proper class.
607
614
<sup><sub>[▲ TOP](#table-of-contents)</sub></sup>
608
615
609
616
610
-
## Multithreading
617
+
## Multithreading 🧵
611
618
Lifecycle of a thread:
612
619
-**New:** a new thread begins in this state where it remains until the program starts the thread
613
620
-**Runnable:** executing its task
@@ -617,19 +624,43 @@ Lifecycle of a thread:
617
624
618
625
Thread priorities range from `MIN_PRIORITY` (a constant of 1) to `MAX_PRIORITY` (a constant of 10).
619
626
627
+
628
+
---
629
+
630
+
### 🧵 Ways to Create a Thread in Java
631
+
620
632
There are two ways of creating a thread:
621
-
1. Implement the `Runnable` interface
622
-
- If you want a class to be executed as a thread, it must implement `Runnable`. This method takes three basic steps:
623
-
1. Implement a `run()` method
624
-
2. Instantiate a `Thread` object, like so: `Thread (Runnable threadObj, String threadName)`, where threadObj is an instance of a class that implements `Runnable`, and threadName is the name given to the new thread
625
-
3. Start a Thread object with `start()`, which executes a class to the class's `run()` method
626
-
627
-
2. Extend the `Thread` class
628
-
- This approach provides more flexibility in handling multiple threads created using available methods in the `Thread` class.
629
-
1. Override the `run()` method in `Thread` class
630
-
2. After creating the `Thread` object, start it with the `start()` method
631
-
632
-
### Thread Synchronization
633
+
634
+
#### 1. Implement the `Runnable` interface
635
+
636
+
If you want a class to be executed as a thread, it must implement `Runnable`.
637
+
638
+
This method involves three basic steps:
639
+
640
+
1. Implement a `run()` method.
641
+
642
+
2. Instantiate a `Thread` object like so:
643
+
```java
644
+
Thread(Runnable threadObj, String threadName);
645
+
```
646
+
where `threadObj` is an instance of a class that implements `Runnable`, and `threadName` is the name given to the
647
+
new thread.
648
+
649
+
3. Start the thread with `start()`, which calls the class's `run()` method
650
+
651
+
---
652
+
653
+
#### 2. Extend the `Thread` class.
654
+
655
+
This approach provides more flexibility in handling multiple threads using available methods in the `Thread` class.
656
+
657
+
1. Override the `run()` method in your class that extends `Thread`.
658
+
2. After creating the `Thread` object, start it with the `start()` method.
659
+
660
+
---
661
+
662
+
### Thread Synchronization:
663
+
633
664
What if two threads try to access the same resources? For example, if multiple threads try to write to the same
634
665
file at the same time, they could corrupt the file.
635
666
@@ -858,7 +889,7 @@ tmp.demoMethod();
858
889
```
859
890
860
891
you can use the [Collections.synchronizedList](https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#synchronizedList-java.util.List-)
861
-
function to create a synchronized list, thus getting you the equivalent of a `Vector`.
892
+
function to create a synchronized list, thus getting you the equivalent of a Vector.
0 commit comments