001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest;
006:
007: import com.tc.object.config.ConfigVisitor;
008: import com.tc.object.config.DSOClientConfigHelper;
009: import com.tc.object.config.TransparencyClassSpec;
010: import com.tc.simulator.app.ApplicationConfig;
011: import com.tc.simulator.listener.ListenerProvider;
012: import com.tctest.runner.AbstractTransparentApp;
013:
014: import java.util.concurrent.CyclicBarrier;
015: import java.util.concurrent.LinkedBlockingQueue;
016:
017: public class LinkedBlockingQueueSingleNodeTestApp extends
018: AbstractTransparentApp {
019: private static final int NUM_OF_PUTS = 1000;
020: private static final int NUM_OF_LOOPS = 1;
021: private static final int NUM_OF_PUTTER = 1;
022: private static final int NUM_OF_GETTER = 1;
023:
024: private LinkedBlockingQueue queue = new LinkedBlockingQueue();
025:
026: public LinkedBlockingQueueSingleNodeTestApp(String appId,
027: ApplicationConfig cfg, ListenerProvider listenerProvider) {
028: super (appId, cfg, listenerProvider);
029: }
030:
031: public void run() {
032: try {
033: CyclicBarrier barrier = new CyclicBarrier(NUM_OF_PUTTER
034: + NUM_OF_GETTER + 1);
035: for (int i = 0; i < NUM_OF_LOOPS; i++) {
036: Thread[] putters = new Thread[NUM_OF_PUTTER];
037: Thread[] getters = new Thread[NUM_OF_GETTER];
038: for (int j = 0; j < NUM_OF_PUTTER; j++) {
039: putters[j] = new Thread(new Putter(barrier, queue,
040: NUM_OF_GETTER));
041: }
042: for (int j = 0; j < NUM_OF_GETTER; j++) {
043: getters[j] = new Thread(new Getter(barrier, queue));
044: }
045: for (int j = 0; j < NUM_OF_PUTTER; j++) {
046: putters[j].start();
047: }
048: for (int j = 0; j < NUM_OF_GETTER; j++) {
049: getters[j].start();
050: }
051: }
052: barrier.await();
053: } catch (Throwable t) {
054: notifyError(t);
055: }
056: }
057:
058: private static class Getter implements Runnable {
059: private LinkedBlockingQueue queue;
060: private CyclicBarrier barrier;
061:
062: public Getter(CyclicBarrier barrier, LinkedBlockingQueue queue) {
063: this .barrier = barrier;
064: this .queue = queue;
065: }
066:
067: public void run() {
068: try {
069: while (true) {
070: Object o = queue.take();
071: if ("STOP".equals(o)) {
072: break;
073: }
074: WorkItem w = (WorkItem) o;
075: System.out.println("Getting " + w.getI());
076: }
077: barrier.await();
078: } catch (Exception e) {
079: throw new AssertionError(e);
080: }
081: }
082: }
083:
084: private static class Putter implements Runnable {
085: private CyclicBarrier barrier;
086: private LinkedBlockingQueue queue;
087: private int numOfGetter;
088:
089: public Putter(CyclicBarrier barrier, LinkedBlockingQueue queue,
090: int numOfGetter) {
091: this .barrier = barrier;
092: this .queue = queue;
093: this .numOfGetter = numOfGetter;
094: }
095:
096: public void run() {
097: try {
098: for (int i = 0; i < NUM_OF_PUTS; i++) {
099: System.out.println("Putting " + i);
100: queue.put(new WorkItem(i));
101: }
102: for (int i = 0; i < numOfGetter; i++) {
103: queue.put("STOP");
104: }
105: barrier.await();
106: } catch (Exception e) {
107: throw new AssertionError(e);
108: }
109: }
110: }
111:
112: public static void visitL1DSOConfig(ConfigVisitor visitor,
113: DSOClientConfigHelper config) {
114: String testClass = LinkedBlockingQueueSingleNodeTestApp.class
115: .getName();
116: TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
117:
118: config.addIncludePattern(testClass + "$*");
119:
120: String methodExpression = "* " + testClass + "*.*(..)";
121: config.addWriteAutolock(methodExpression);
122:
123: spec.addRoot("queue", "queue");
124: }
125:
126: private static class WorkItem {
127: private final int i;
128:
129: public WorkItem(int i) {
130: this .i = i;
131: }
132:
133: public int getI() {
134: return this.i;
135: }
136: }
137: }
|