Race Condition in Java Multithreading
Race Condition in Java Multithreading
@ Ove rri de
p ublic voi d run () {
//in cre ment in g
t hi s.in cre ment ();
S yste m.out.p ri nt ln ("Va lue for Thread Afte r i ncrement "
+ Threa d. curre ntThrea d (). ge tNa me () + " " + t hi s. ge tVa lue ());
//de cre me nti n g
t hi s. de cre me nt ();
S yste m.out.p ri nt ln ("Va lue for Thread a t la st "
+ Threa d. curre ntThrea d (). ge tNa me () + " " + t hi s. ge tVa lue ());
}
}
p u bli c i n t ge tV al ue ()
{
ret ur n c ;
}
@ O ver ri d e
pu b li c voi d ru n ()
{
s ync hro ni z ed (thi s )
{
/ / i nc rem e n ti n g
this.increment();
S ys tem .ou t. pri nt ln ( "V a lue f or T hr ead A f ter i nc rem e nt "
+ T hre ad .c ur re nt T hre ad () . ge tN am e () + " " + t hi s . ge tV a lu e ( )) ;
/ /dec rem e n ti ng
thi s .d ec rem en t ( ) ;
S ys tem .ou t. pri nt ln ( "V a lue f or T hr ead a t l as t " +
T h rea d .c u rr en tT hr ead ( ). ge tN am e () + " " + t hi s . ge tV al ue () );
}
}
}
O ut p ut