Public class test implements runnable ...{
Public int X;
// Public synchronized void run ()
Public void run ()...{
System. Out. println ("threading running..." + thread. currentthread (). getname ());
Int hold;
// Synchronized (this)
...{
For (;;)...{
Hold = x + 1;
Try ...{
Thread. Sleep (5000 );
} Catch (exception e )...{
E. getcause ();
}
X = hold;
System. Out. println (thread. currentthread (). getname () + "value x:" + x );
}
}
}
Public static void main (string [] ARGs )...{
System. Out. println ("threading ...");
Test test1 = new test ();
Thread one = new thread (test1 );
Thread two = new thread (test1 );
One. Start ();
Two. Start ();
}
}
Threading...
Threading running... thread-0
Threading running... thread-1
Thread-0 value x: 1
Thread-1 value x: 1
Thread-0 value x: 2
Thread-1 value x: 2
Thread-0 value x: 3
Thread-1 value x: 3
Thread-0 value x: 4
Thread-1 value x: 4
Thread-0 value x: 5
Thread-1 value x: 5
Thread-0 value x: 6
Thread-1 value x: 6
Thread-0 value x: 7
Thread-1 value x: 7