Skip to main content

Posts

Showing posts with the label concurrency

Why reading a volatile and writing to a field member is not scalable in Java?

Observe the following program written in Java (complete runnable version follows, but the important part of the program is in the snippet a little bit further below): import java.util.ArrayList; /** A not easy to explain benchmark. */ class MultiVolatileJavaExperiment { public static void main(String[] args) { (new MultiVolatileJavaExperiment()).mainMethod(args); } int size = Integer.parseInt(System.getProperty("size")); int par = Integer.parseInt(System.getProperty("par")); public void mainMethod(String[] args) { int times = 0; if (args.length == 0) times = 1; else times = Integer.parseInt(args[0]); ArrayList < Long > measurements = new ArrayList < Long > (); for (int i = 0; i < times; i++) { long start = System.currentTimeMillis(); run(); long end = System.currentTimeMillis(); long time = (end - start); System.out.printl

Is volatile expensive?

After reading http://gee.cs.oswego.edu/dl/jmm/cookbook.html about the implementation of volatile, especially section "Interactions with Atomic Instructions" I assume that reading a volatile variable without updating it needs a LoadLoad or a LoadStore barrier. Further down the page I see that LoadLoad and LoadStore are effectively no-ops on X86 CPUs. Does this mean that volatile read operations can be done without a explicit cache invalidation on x86, and is as fast a normal variable read (disregarding the reordering constraints of volatile)? I believe I don't understand this correctly. Could someone care to enlighten me? EDIT: I wonder if there are differences in multi-processor environments. On single CPU systems the CPU might look at it's own thread caches, as John V. states, but on multi CPU systems there must be some config option to the CPUs that this is not enough and main memory has to be hit, making volatile slower on multi cpu systems, right? PS: On

Ways to improve performance consistency

In the following example, one thread is sending "messages" via a ByteBuffer which is the consumer is taking. The best performance is very good but its not consistent. public class Main { public static void main(String... args) throws IOException { for (int i = 0; i < 10; i++) doTest(); } public static void doTest() { final ByteBuffer writeBuffer = ByteBuffer.allocateDirect(64 * 1024); final ByteBuffer readBuffer = writeBuffer.slice(); final AtomicInteger readCount = new PaddedAtomicInteger(); final AtomicInteger writeCount = new PaddedAtomicInteger(); for(int i=0;i<3;i++) performTiming(writeBuffer, readBuffer, readCount, writeCount); System.out.println(); } private static void performTiming(ByteBuffer writeBuffer, final ByteBuffer readBuffer, final AtomicInteger readCount, final AtomicInteger writeCount) { writeBuffer.clear(); readBuffer.clear();

java: concurrent collections

I'm trying to find one or more concurrent collections to use that I can implement the following behavior (the names are contrived for analogy purposes): /** * Acts as a broker for a concurrent hash map that stores its keys in order * of submission. At shipping time, the concurrent map is "sealed" * (picture a truck with its cargo door being closed) * and its contents presented as an immutable map, and is replaced * by a new concurrent map ready to accept values. * * Consumers of this class that submit information to it, are expected to * know that this contains a concurrent collection, and should use the * compareAndSet paradigm, e.g. the following: * * LoadingDock loadingDock = ... * boolean done = false; * while (!done) * { * V oldValue = loadingDock.get(); * V newValue = computeNewValue(oldValue, otherInformation); * if (oldValue == null) * done = loadingDock.putIfAbsent(newValue) == null; * else * done = loadingDock.replac

Is possible to postpone trigger fire in Quartz?

I have two processes: Process 1 - implements runnable and can run forever. Process 2 - fires at fixed hour and minute of day (i've created a job that run with Quartz). To warn the process 1 that the other process is running I can use the TriggerListener , but how can I postpone the fire of the second process if the process 1 still doing something? For example: I need to fire the trigger at 2PM, but this need to be done after 2PM if the process 1 isnt idle. Here's some sample: ProcessForever.java import static org.quartz.CronScheduleBuilder.dailyAtHourAndMinute; import static org.quartz.JobBuilder.newJob; import static org.quartz.TriggerBuilder.newTrigger; public class ProcessForever implements Runnable { private boolean processTwoRunning; private Scheduler scheduler; private Trigger trgProcessTwo; private String status; public static final STATUS_PROCESS = "PROCESS"; public static final STATUS_SLEEP = "SLEEP"; private