public static
class Base {
public synchronized void setValue( int value ) {
try { Thread.sleep( 1000 ); } catch (InterruptedException e) {}
this .value = value;
}
public synchronized int getValue() {
return
value;
}
protected int value = 0;
}
public static
class Derived extends Base {
public int getValue() {
return this .value;
}
}
public static void main(String[] args) {
System.out.println( "Testando Base..." );
test( new Base() );
try { Thread.sleep( 5000 ); } catch (InterruptedException e) {}
System.out.println( "Testando Derivado..." );
test( new Derived() );
}
private static void test( final Base base ) {
Runnable writer = new Runnable() {
public void run() {
System.out.println( "Writer set value " );
base.setValue( 1 );
}
};
Runnable reader =
new Runnable() {
public void run() {
System.out.println(
"Leitor: " + base.getValue() );
}
};
(new Thread( writer ) ).start();
(new Thread( reader ) ).start();
}
|
|