import java.io.*; public class Runnable_Class2{ //static int i = 0; public static void main(String args[]){ int count = 0; int count_t = 0; Data dato = new Data(); System.out.println("Cuenta Actual (MAIN_THREAD): " + dato.getData()); RunnableThread rt = new RunnableThread(dato); Thread t = new Thread(rt); t.start(); while(count < 100){ dato.incrementar(); System.out.println("Cuenta Actual (MAIN_THREAD): " + dato.getData()); count++; } } } class RunnableThread implements Runnable{ public RunnableThread(Data d_){ d=d_; } public void run(){ int count_t = 0; while(count_t < 100){ synchronized(Data.class) { d.decrementar(); } System.out.println("Cuenta Actual (SECOND_THREAD): " + d.getData()); count_t++; } } private Data d; } class Data { public synchronized void incrementar(){ i++; } public void decrementar(){ i--; } public synchronized int getData(){ return i; } private int i=0; }