use of java.util.concurrent.atomic.AtomicIntegerArray in project CorfuDB by CorfuDB.
the class TXConflictScenariosTest method testNoWriteConflictSimple.
void testNoWriteConflictSimple() throws Exception {
final CorfuSharedCounter sharedCounter1 = instantiateCorfuObject(CorfuSharedCounter.class, "test" + 1);
final CorfuSharedCounter sharedCounter2 = instantiateCorfuObject(CorfuSharedCounter.class, "test" + 2);
commitStatus = new AtomicIntegerArray(2);
t(1, this::TXBegin);
t(2, this::TXBegin);
t(1, () -> {
sharedCounter1.setValue(OVERWRITE_ONCE);
});
t(2, () -> {
sharedCounter2.setValue(OVERWRITE_ONCE);
});
t(1, () -> sharedCounter1.getValue());
t(2, () -> sharedCounter2.getValue());
t(1, () -> sharedCounter2.getValue());
t(2, () -> sharedCounter1.getValue());
t(1, () -> {
sharedCounter1.setValue(OVERWRITE_TWICE);
});
t(2, () -> {
sharedCounter2.setValue(OVERWRITE_TWICE);
});
t(1, () -> sharedCounter1.getValue());
t(1, () -> sharedCounter2.getValue());
t(2, () -> sharedCounter2.getValue());
t(2, () -> sharedCounter1.getValue());
t(1, () -> {
try {
TXEnd();
commitStatus.set(0, COMMITVALUE);
} catch (TransactionAbortedException tae) {
// do nothing
}
});
t(2, () -> {
try {
TXEnd();
commitStatus.set(1, COMMITVALUE);
} catch (TransactionAbortedException tae) {
// do nothing
}
});
}
use of java.util.concurrent.atomic.AtomicIntegerArray in project CorfuDB by CorfuDB.
the class AbstractTransactionContextTest method setupCounters.
/**
* build an array of shared counters for the test
*/
void setupCounters() {
numTasks = PARAMETERS.NUM_ITERATIONS_MODERATE;
sharedCounters = new ArrayList<>();
for (int i = 0; i < numTasks; i++) sharedCounters.add(i, instantiateCorfuObject(CorfuSharedCounter.class, "test" + i));
// initialize all shared counters
for (int i = 0; i < numTasks; i++) sharedCounters.get(i).setValue(INITIAL);
commitStatus = new AtomicIntegerArray(numTasks);
snapStatus = new AtomicIntegerArray(numTasks);
}
use of java.util.concurrent.atomic.AtomicIntegerArray in project elasticsearch by elastic.
the class IndexActionIT method testCreatedFlagParallelExecution.
public void testCreatedFlagParallelExecution() throws Exception {
createIndex("test");
ensureGreen();
int threadCount = 20;
final int docCount = 300;
int taskCount = docCount * threadCount;
final AtomicIntegerArray createdCounts = new AtomicIntegerArray(docCount);
ExecutorService threadPool = Executors.newFixedThreadPool(threadCount);
List<Callable<Void>> tasks = new ArrayList<>(taskCount);
final Random random = random();
for (int i = 0; i < taskCount; i++) {
tasks.add(new Callable<Void>() {
@Override
public Void call() throws Exception {
int docId = random.nextInt(docCount);
IndexResponse indexResponse = index("test", "type", Integer.toString(docId), "field1", "value");
if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
createdCounts.incrementAndGet(docId);
}
return null;
}
});
}
threadPool.invokeAll(tasks);
for (int i = 0; i < docCount; i++) {
assertThat(createdCounts.get(i), lessThanOrEqualTo(1));
}
terminate(threadPool);
}
use of java.util.concurrent.atomic.AtomicIntegerArray in project fastjson by alibaba.
the class Bug_7 method test_AtomicIntegerArray.
public void test_AtomicIntegerArray() throws Exception {
AtomicIntegerArray array = new AtomicIntegerArray(3);
array.set(0, 1);
array.set(1, 2);
array.set(2, 3);
String text = JSON.toJSONString(array);
Assert.assertEquals("[1,2,3]", text);
}
use of java.util.concurrent.atomic.AtomicIntegerArray in project fastjson by alibaba.
the class AtomicCodec method write.
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
SerializeWriter out = serializer.out;
if (object instanceof AtomicInteger) {
AtomicInteger val = (AtomicInteger) object;
out.writeInt(val.get());
return;
}
if (object instanceof AtomicLong) {
AtomicLong val = (AtomicLong) object;
out.writeLong(val.get());
return;
}
if (object instanceof AtomicBoolean) {
AtomicBoolean val = (AtomicBoolean) object;
out.append(val.get() ? "true" : "false");
return;
}
if (object == null) {
out.writeNull(SerializerFeature.WriteNullListAsEmpty);
return;
}
if (object instanceof AtomicIntegerArray) {
AtomicIntegerArray array = (AtomicIntegerArray) object;
int len = array.length();
out.write('[');
for (int i = 0; i < len; ++i) {
int val = array.get(i);
if (i != 0) {
out.write(',');
}
out.writeInt(val);
}
out.write(']');
return;
}
AtomicLongArray array = (AtomicLongArray) object;
int len = array.length();
out.write('[');
for (int i = 0; i < len; ++i) {
long val = array.get(i);
if (i != 0) {
out.write(',');
}
out.writeLong(val);
}
out.write(']');
}
Aggregations