我正在使用静态代码块来初始化我所拥有的注册表中的某些控制器.因此,我的问题是,我可以保证这个静态代码块只在首次加载类时才会被调用一次吗?我知道我无法保证何时会调用此代码块,我猜它是在Classloader首次加载它时.我意识到我可以在静态代码块中同步类,但我的猜测实际上这是怎么回事?
简单的代码示例是;
class FooRegistry {
static {
//this code must only ever be called once
addController(new FooControllerImpl());
}
private static void addController(IFooController controller) {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
或者我应该这样做;
class FooRegistry {
static {
synchronized(FooRegistry.class) {
addController(new FooControllerImpl());
}
}
private static void addController(IFooController controller) {
// ...
}
}
Run Code Online (Sandbox Code Playgroud) java static multithreading synchronization static-initializer
是不是C++ 0x没有信号量?Stack Overflow上已经有一些关于信号量使用的问题.我一直使用它们(posix信号量)让线程等待另一个线程中的某个事件:
void thread0(...)
{
doSomething0();
event1.wait();
...
}
void thread1(...)
{
doSomething1();
event1.post();
...
}
Run Code Online (Sandbox Code Playgroud)
如果我用互斥量做到这一点:
void thread0(...)
{
doSomething0();
event1.lock(); event1.unlock();
...
}
void thread1(...)
{
event1.lock();
doSomethingth1();
event1.unlock();
...
}
Run Code Online (Sandbox Code Playgroud)
问题:它很难看并且不能保证thread1首先锁定互斥锁(假设同一个线程应该锁定和解锁互斥锁,你也无法在thread0和thread1启动之前锁定event1).
因此,由于boost也没有信号量,实现上述目标的最简单方法是什么?
我知道synchronize
在方法之前使用关键字会为该对象带来同步.也就是说,将同步运行相同对象实例的2个线程.
但是,由于同步是在对象级别,因此将不会同步运行该对象的不同实例的2个线程.如果我们在由该方法调用的Java类中有一个静态变量,我们希望它在该类的实例之间进行同步.这两个实例在两个不同的线程中运行.
我们可以通过以下方式实现同步吗?
public class Test
{
private static int count = 0;
private static final Object lock= new Object();
public synchronized void foo()
{
synchronized(lock)
{
count++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
是不是因为我们已经定义了一个lock
静态的对象并且我们正在使用该synchronized
锁的关键字,所以静态变量count
现在在类的实例之间同步Test
?
lock和Mutex有什么区别?为什么他们不能互换使用?
请澄清我对Singleton和Multithreading的疑问:
getInstance()
方法时会发生什么?getInstance()
synchronized
吗?我正在运行以下形式的事件循环:
var i;
var j = 10;
for (i = 0; i < j; i++) {
asynchronousProcess(callbackFunction() {
alert(i);
});
}
Run Code Online (Sandbox Code Playgroud)
我试图显示一系列显示数字0到10的警报.问题是,当回调函数被触发时,循环已经经历了几次迭代并且它显示了更高的值i
.有关如何解决此问题的任何建议?
我们何时应该使用互斥锁?什么时候应该使用信号量?
与上面的RulyCanceler类的代码相比,我想使用运行代码 CancellationTokenSource
.
如何在取消令牌中提到它,即不抛出/捕获异常?我可以使用该IsCancellationRequested
物业吗?
我试图像这样使用它:
cancelToken.ThrowIfCancellationRequested();
Run Code Online (Sandbox Code Playgroud)
和
try
{
new Thread(() => Work(cancelSource.Token)).Start();
}
catch (OperationCanceledException)
{
Console.WriteLine("Canceled!");
}
Run Code Online (Sandbox Code Playgroud)
但这cancelToken.ThrowIfCancellationRequested();
在方法中给出了运行时错误Work(CancellationToken cancelToken)
:
System.OperationCanceledException was unhandled
Message=The operation was canceled.
Source=mscorlib
StackTrace:
at System.Threading.CancellationToken.ThrowIfCancellationRequested()
at _7CancellationTokens.Token.Work(CancellationToken cancelToken) in C:\xxx\Token.cs:line 33
at _7CancellationTokens.Token.<>c__DisplayClass1.<Main>b__0() in C:\xxx\Token.cs:line 22
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
Run Code Online (Sandbox Code Playgroud)
我成功运行的代码在新线程中捕获了OperationCanceledException:
using System;
using System.Threading; …
Run Code Online (Sandbox Code Playgroud) 这个java代码是什么意思?它会锁定所有物体MyClass
吗?
synchronized(MyClass.class) {
//is all objects of MyClass are thread-safe now ??
}
Run Code Online (Sandbox Code Playgroud)
以上代码与此不同:
synchronized(this) {
//is all objects of MyClass are thread-safe now ??
}
Run Code Online (Sandbox Code Playgroud) 如果我需要按顺序调用3个http API,那么对于以下代码来说,这将是一个更好的替代方法:
http.get({ host: 'www.example.com', path: '/api_1.php' }, function(res) {
res.on('data', function(d) {
http.get({ host: 'www.example.com', path: '/api_2.php' }, function(res) {
res.on('data', function(d) {
http.get({ host: 'www.example.com', path: '/api_3.php' }, function(res) {
res.on('data', function(d) {
});
});
}
});
});
}
});
});
}
Run Code Online (Sandbox Code Playgroud) synchronization ×10
java ×4
asynchronous ×2
c# ×2
boost-thread ×1
c++ ×1
c++11 ×1
class ×1
concurrency ×1
for-loop ×1
javascript ×1
linux ×1
locking ×1
mutex ×1
node.js ×1
object ×1
semaphore ×1
singleton ×1
static ×1
synchronized ×1