public class Process extends Thread {
private static int count=0;
@Override
public void run()
{
for (int i=0; i< 1000000000; i++)
count++;
} }
Consider the above snippet. If more threads of type Process are running, this code is not thread-safe due to the cuncurrent access to the static variable.
How to make this code "thread safe"?
Is it an option to enclose count++
within a static synchronized
method and invoking this instead?