I\'m trying to figure out how synchronized methods work. From my understanding I created two threads T1 and T2 that will call the same method <
try this :
public class Main {
public static void main(String[] args) {
A a = new A();
Thread t1 = new Thread(a);
Thread t2 = new Thread(a);
t1.setName("T1");
t2.setName("T2");
t1.start();
t2.start();
}
}
class B {
public synchronized void addNew(int i){
Thread t = Thread.currentThread();
for (int j = 0; j < 5; j++) {
System.out.println(t.getName() +"-"+(j+i));
}
}
}
class A extends Thread {
private B b1 = new B();
@Override
public void run() {
b1.addNew(100);
}
}