本文整理汇总了C#中AtomicBoolean类的典型用法代码示例。如果您正苦于以下问题:C# AtomicBoolean类的具体用法?C# AtomicBoolean怎么用?C# AtomicBoolean使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
AtomicBoolean类属于命名空间,在下文中一共展示了AtomicBoolean类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FromValue
public static AtomicBoolean FromValue (bool value)
{
AtomicBoolean temp = new AtomicBoolean ();
temp.Value = value;
return temp;
}
示例2: SingleThreadExecutor
/// <summary>
/// Creates a new <c>SingleThreadExecutor</c> using a custom thread priority.
/// </summary>
/// <param name="priority">
/// The priority to assign the thread.
/// </param>
public SingleThreadExecutor(ThreadPriority priority) {
_actions = new BlockingQueue<Action>();
_running = new AtomicBoolean(false);
_shuttingDown = new AtomicBoolean(false);
_priority = priority;
}
示例3: WidgetBuilderThread
public WidgetBuilderThread(WidgetBuilder widgetBuilder, string text, ParentWidget parent, AtomicBoolean failFlag)
{
this.widgetBuilder = widgetBuilder;
this.text = text;
this.parent = parent;
this.failFlag = failFlag;
}
示例4: AtomicBoolean_Load_Should_Success
public void AtomicBoolean_Load_Should_Success()
{
var atomicBoolean = new AtomicBoolean(true);
Assert.Equal(true, atomicBoolean.Load(MemoryOrder.Relaxed));
Assert.Equal(true, atomicBoolean.Load(MemoryOrder.Acquire));
Assert.Equal(true, atomicBoolean.Load(MemoryOrder.AcqRel));
Assert.Equal(true, atomicBoolean.Load(MemoryOrder.SeqCst));
}
示例5: IndexerThread
public IndexerThread(IndexWriter w, FacetsConfig config, TaxonomyWriter tw, ReferenceManager<SearcherAndTaxonomy> mgr, int ordLimit, AtomicBoolean stop)
{
this.w = w;
this.config = config;
this.tw = tw;
this.mgr = mgr;
this.ordLimit = ordLimit;
this.stop = stop;
}
示例6: ToBoolean
/// <summary>
/// Extracts a <see cref="Boolean"/> from an instance of <see cref="AtomicBoolean"/>.
/// </summary>
/// <param name="atomicBoolean">The <see cref="AtomicBoolean"/> to extract the value of.</param>
/// <exception cref="ArgumentNullException">
/// Thrown if <paramref name="atomicBoolean" /> is <see langword="null"/>.
/// </exception>
/// <returns>the value of the <see cref="AtomicBoolean"/>.</returns>
public static bool ToBoolean(AtomicBoolean atomicBoolean)
{
if (atomicBoolean == null)
{
throw new InvalidCastException();
}
return atomicBoolean.Value;
}
示例7: Execute
public Result<bool> Execute(IAggregateRootId aggregateRootId, int aggregateRootRevision, Action action)
{
var result = new Result<bool>(false);
var acquired = new AtomicBoolean(false);
try
{
if (aggregateLock.TryGetValue(aggregateRootId, out acquired) == false)
{
acquired = acquired ?? new AtomicBoolean(false);
if (aggregateLock.TryAdd(aggregateRootId, acquired) == false)
aggregateLock.TryGetValue(aggregateRootId, out acquired);
}
if (acquired.CompareAndSet(false, true))
{
try
{
AtomicInteger revision = null;
if (aggregateRevisions.TryGetValue(aggregateRootId, out revision) == false)
{
revision = new AtomicInteger(aggregateRootRevision - 1);
if (aggregateRevisions.TryAdd(aggregateRootId, revision) == false)
return result;
}
var currentRevision = revision.Value;
if (revision.CompareAndSet(aggregateRootRevision - 1, aggregateRootRevision))
{
try
{
action();
return Result.Success;
}
catch (Exception)
{
revision.GetAndSet(currentRevision);
throw;
}
}
}
finally
{
acquired.GetAndSet(false);
}
}
return result;
}
catch (Exception ex)
{
return result.WithError(ex);
}
}
示例8: CompareExpectedValueAndSetNewValue
public void CompareExpectedValueAndSetNewValue()
{
AtomicBoolean ai = new AtomicBoolean( true );
Assert.AreEqual( true, ai.Value );
Assert.IsTrue( ai.CompareAndSet( true, false ) );
Assert.AreEqual( false, ai.Value );
Assert.IsTrue( ai.CompareAndSet( false, false ) );
Assert.AreEqual( false, ai.Value );
Assert.IsFalse( ai.CompareAndSet( true, false ) );
Assert.IsFalse( ( ai.Value ) );
Assert.IsTrue( ai.CompareAndSet( false, true ) );
Assert.AreEqual( true, ai.Value );
}
示例9: CompareExpectedValueAndSetNewValueInMultipleThreads
public void CompareExpectedValueAndSetNewValueInMultipleThreads()
{
AtomicBoolean ai = new AtomicBoolean( true );
var t = ThreadManager.StartAndAssertRegistered("T1",delegate
{
while (!ai.CompareAndSet(false, true))
Thread.Sleep(Delays.Short);
}
);
Assert.IsTrue( ai.CompareAndSet( true, false ), "Value" );
ThreadManager.JoinAndVerify();
Assert.IsFalse( t.IsAlive, "Thread is still alive." );
Assert.IsTrue( ai.Value );
}
示例10: TestBlockingReceiveWithTimeout
public void TestBlockingReceiveWithTimeout()
{
QueueChannel channel = new QueueChannel();
AtomicBoolean receiveInterrupted = new AtomicBoolean(false);
CountDownLatch latch = new CountDownLatch(1);
Thread t = new Thread(new ThreadStart(delegate
{
IMessage message = channel.Receive(new TimeSpan(10000));
receiveInterrupted.Value = true;
Assert.IsTrue(message == null);
latch.CountDown();
}));
t.Start();
//Assert.IsFalse(receiveInterrupted.Value);
t.Interrupt();
latch.Await();
Assert.IsTrue(receiveInterrupted.Value);
}
示例11: TestApplyDeletesOnFlush
public virtual void TestApplyDeletesOnFlush()
{
Directory dir = NewDirectory();
// Cannot use RandomIndexWriter because we don't want to
// ever call commit() for this test:
AtomicInteger docsInSegment = new AtomicInteger();
AtomicBoolean closing = new AtomicBoolean();
AtomicBoolean sawAfterFlush = new AtomicBoolean();
IndexWriter w = new IndexWriterAnonymousInnerClassHelper(this, dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random())).SetRAMBufferSizeMB(0.5).SetMaxBufferedDocs(-1).SetMergePolicy(NoMergePolicy.NO_COMPOUND_FILES).SetReaderPooling(false), docsInSegment, closing, sawAfterFlush);
int id = 0;
while (true)
{
StringBuilder sb = new StringBuilder();
for (int termIDX = 0; termIDX < 100; termIDX++)
{
sb.Append(' ').Append(TestUtil.RandomRealisticUnicodeString(Random()));
}
if (id == 500)
{
w.DeleteDocuments(new Term("id", "0"));
}
Document doc = new Document();
doc.Add(NewStringField("id", "" + id, Field.Store.NO));
doc.Add(NewTextField("body", sb.ToString(), Field.Store.NO));
w.UpdateDocument(new Term("id", "" + id), doc);
docsInSegment.IncrementAndGet();
// TODO: fix this test
if (SlowFileExists(dir, "_0_1.del") || SlowFileExists(dir, "_0_1.liv"))
{
if (VERBOSE)
{
Console.WriteLine("TEST: deletes created @ id=" + id);
}
break;
}
id++;
}
closing.Set(true);
Assert.IsTrue(sawAfterFlush.Get());
w.Dispose();
dir.Dispose();
}
示例12: testConcurrentAddWidgets
public void testConcurrentAddWidgets()
{
WidgetBuilder widgetBuilder =
new WidgetBuilder(new Class[] { });
//www.it - ebooks.infoGood Comments
String text = "'''bold text'''";
ParentWidget parent = null;
AtomicBoolean failFlag = new AtomicBoolean();
failFlag.set(false);
//This is our best attempt to get a race condition
//by creating large number of threads.
for (int i = 0; i < 25000; i++)
{
WidgetBuilderThread widgetBuilderThread =
new WidgetBuilderThread(widgetBuilder, text, parent, failFlag);
Thread thread = new Thread(new ThreadStart(assertEquals));
thread.Start();
}
assertEquals(false, failFlag.get());
}
示例13: startRecordRig
private void startRecordRig()
{
recordedRigs = new List<Body[]>();
recordedRigTimePoints = new List<int>();
rigDetected = new AtomicBoolean(false);
try
{
XmlWriterSettings ws = new XmlWriterSettings();
ws.Indent = true;
rigWriter = XmlWriter.Create(tempRigFileName, ws);
rigWriter.WriteStartDocument();
rigWriter.WriteStartElement("BodyDataSequence");
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
示例14: SetValue_ManyConcurrentThreads_OnlyOneSucceeds_Test
public void SetValue_ManyConcurrentThreads_OnlyOneSucceeds_Test()
{
_atomicBoolean = new AtomicBoolean(false);
int totalSucceedingThreads = 0;
Parallel.For(1, 11, (state) =>
{
System.Console.WriteLine(String.Format("Executing SetValue on thread {0}",
Thread.CurrentThread.ManagedThreadId));
if (!_atomicBoolean.SetValue(true))
{
System.Console.WriteLine(String.Format("Thread {0} was successful in calling SetValue.",
Thread.CurrentThread.ManagedThreadId));
Interlocked.Increment(ref totalSucceedingThreads);
}
});
//Assert
_atomicBoolean.Value.Should().BeTrue();
totalSucceedingThreads.Should().Be(1);
}
示例15: ThreadAnonymousInnerClassHelper
public ThreadAnonymousInnerClassHelper(TestDocValuesIndexing outerInstance, IndexWriter w, CountdownEvent startingGun, AtomicBoolean hitExc, Document doc)
{
this.OuterInstance = outerInstance;
this.w = w;
this.StartingGun = startingGun;
this.HitExc = hitExc;
this.Doc = doc;
}