Good afternoon! I had a problem with thread synchronization in java. I am developing a program that creates timers and allows reset to delete and stop. Just learn how to use streams.
The problem is that the code only gives synchronization for some time ... I can not understand my error. Perhaps my path is wrong, so I would like to know how to solve this problem.
I have the following code:
public class StopWatch
{
public synchronized void startClock( final int id )
{
thisThread = new Thread()
{
@Override
public void run()
{
try
{
while( true )
{
System.out.printf( "Thread [%d] = %d\n", id, timerTime );
timerTime += DELAY;
Thread.sleep( DELAY );
}
}
catch( InterruptedException ex )
{
ex.printStackTrace();
}
}
};
thisThread.start();
}
…
private long timerTime = 0;
private static final int DELAY = 100;
private Thread thisThread;
}
I call this class the following:
StopWatch s = new StopWatch(1);
s.startClock();
StopWatch s2 = new StopWatch(2);
s2.startClock();
Exire source
share