package com.java.lang.thread.synchronized_example;
public class _Thread extends Thread {
@Override
public synchronized void run() {
for(int i=0;i<5;i++){
System.out.println("in run method");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void print1(){
for(int i=0;i<5;i++){
System.out.println("in print1 method");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void print2(){
for(int i=0;i<5;i++){
System.out.println("in print2 method");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package com.java.lang.thread.synchronized_example;
public class _Main {
public static void main(String[] args) {
_Thread thread1 = new _Thread();
thread1.start();
_Thread thread2 = new _Thread();
thread2.print1();
_Thread thread3 = new _Thread();
thread3.print2();
}
}