When black respond Rxa8, white mate with Rxh4, otherwise they mate with Qa4!
Here is the puzzle for this week:
White to move, mate in two
Happy solving!
public class IdCounter {
private AtomicInteger counter = new AtomicInteger();
public int increment() {
return counter.incrementAndGet();
}
}So AtomicInteger basically wraps a primitive integer value and allows you to make thread-safe operations on it. You don't care anymore about volatile and synchronized. You just use the appropriate methods. The other classes in the java.util.concurrent.atomic package are basically doing the same but not on a different type - Long, Boolean, Array, etc..(AtomicLong, AtomicBoolean, AtomicIntegerArray).if (variable == expectedValue) {
variable = newValue;
}Without appropriate synchronization the above code is error prone to race conditions, because while one thread is setting the newValue, a second thread can be making the comparison with the expectedValue. This is not what you want in the most cases.public class Producer implements Runnable {
private BlockingQueue queue;
public Producer(BlockingQueue queue) {
this.queue = queue;
}
@Override
public void run() {
//Produce 1000 products
for (int i = 0; i < 1000; i++) {
queue.put(new Product());
System.out.println("PRODUCED PRODUCT");
}
}
} public class Consumer implements Runnable {
private BlockingQueue queue;
public Consumer(BlockingQueue queue) {
this.queue = queue;
}
@Override
public void run() {
while (true) {
Product product = queue.take();
System.out.println("CONSUMED PRODUCT");
}
}
} BlockingQueue queue = new ArrayBlockingQueue(50);
Producer producer = new Producer(queue);
new Thread(producer).start();
Consumer consumer = new Consumer(queue);
new Thread(consumer).start(); public class LongRunningTask() {
private boolean shouldStop;
public void work() {
while (!shouldStop) {
launchRocketToTheMoon();
}
}
public void stop() {
shouldStop = true;
}
}
public class LongRunningTask() {
private volatile boolean shouldStop;
public void work() {
while (!shouldStop) {
launchRocketToTheMoon();
}
}
public void stop() {
shouldStop = true;
}
}public class IdCounter {
private volatile int counter;
public int increment() {
counter++;
return counter;
}
}
public class IdCounter {
private int counter;
public synchronized int increment() {
counter++;
return counter;
}
}