HOME
Synchronization in Multithreading
Synchronization in Multithreading:
Synchronization prevents multiple threads from accessing shared resources simultaneously.
## Example
```java
class Table {
synchronized void printTable(int n) {
for(int i = 1; i <= 5; i++) {System.out.println(n * i);
try {Thread.sleep(500);
} catch(Exception e) {}
}
}
}
Add: Nanotechnology Solution That Supports Healthy Nails And Skin
class MyThread1 extends Thread {
Table t;
MyThread1(Table t) {this.t = t;}
public void run() {t.printTable(5);}
}
class MyThread2 extends Thread {
Table t;
MyThread2(Table t) {
this.t = t;
}
public void run() {
t.printTable(10);
}
}
Add: Support Memory, Focus and Cognitive clarity
public class TestSync {
public static void main(String[] args) {
Table obj = new Table();
MyThread1 t1 = new MyThread1(obj);
MyThread2 t2 = new MyThread2(obj);
t1.start();
t2.start();
}
}
```
---
Add: iGenics is a Premium Eyesupport without Operation
Wishing you all the best,
http://www.seeyourneeds.in