import java.util.concurrent.atomic.AtomicInteger;
public class Worker extends Thread {
public static final AtomicInteger counter = new AtomicInteger(0);
@Override
public void run() {
//... do something
counter.incrementAndGet();
}
}
public class Worker extends Thread {
private static volatile int count = 0;
public static int getCount() {
return count;
}
public static synchronized void increment() {
count = count + 1;
}
@Override
public void run() {
//... do something
increment();
}
}
public class Worker extends Thread {
private static Lock lock = new ReentrantLock();
private static Integer count = 0;
public static int getCount() {
return count;
}
public static void increment() {
lock.lock();
try {
count = count + 1;
} finally {
lock.unlock();
}
}
@Override
public void run() {
//... do something
increment();
}
}