我偶尔会使用一个volatile实例变量,在这种情况下,我有两个线程读取/写入它并且不希望获得锁定的开销(或潜在的死锁风险); 例如,计时器线程定期更新在某些类上作为getter公开的int ID:
public class MyClass {
private volatile int id;
public MyClass() {
ScheduledExecutorService execService = Executors.newScheduledThreadPool(1);
execService.scheduleAtFixedRate(new Runnable() {
public void run() {
++id;
}
}, 0L, 30L, TimeUnit.SECONDS);
}
public int getId() {
return id;
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题:鉴于JLS只能保证32位读取将是原子有任何一点曾经使用挥发性长时间?(即64位).
警告:请不要回复说使用volatile结束synchronized是预优化的情况; 我很清楚如何/何时使用,synchronized但有些情况volatile更可取.例如,在定义用于单线程应用程序的Spring bean时,我倾向于使用volatile实例变量,因为无法保证Spring上下文将初始化主线程中的每个bean的属性.
这是我在昨晚被拒绝的采访中无法回答的两个问题.
intJava中的volatile是否是线程安全的?也就是说,可以安全地读取和写入而不锁定吗?
我在修改线程内的视图时遇到问题.我试图添加一个子视图,但显示大约需要6秒或更长时间.我终于搞定了,但我不知道究竟是怎么回事.所以我想知道为什么它有效,以下方法之间有什么区别:
//this worked -added the view instantly
dispatch_async(dispatch_get_main_queue(), ^{
//some UI methods ej
[view addSubview: otherView];
}
//this took around 6 or more seconds to display
[viewController performSelectorOnMainThread:@selector(methodThatAddsSubview:) withObject:otherView
waitUntilDone:NO];
//Also didnt work: NSNotification methods - took also around 6 seconds to display
//the observer was in the viewController I wanted to modify
//paired to a method to add a subview.
[[NSNotificationCenter defaultCenter] postNotificationName:
@"notification-identifier" object:object];
Run Code Online (Sandbox Code Playgroud)
作为参考,这是在ACAccountStore类的Completetion Handler中调用的.
accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
if(granted) {
//my methods were …Run Code Online (Sandbox Code Playgroud) Web浏览器是否在iframe中为JavaScript使用单独的执行线程?
我相信Chrome会为每个标签使用单独的线程,因此我猜测iframe中的JavaScript会与其父窗口共享相同的线程,但是,这似乎也是一种安全风险.
我遇到过Handler和runOnUiThread概念.但对我来说,它们似乎仍然存在疑问,因为它们确实存在哪些不同之处.
它们都旨在从后台线程执行UI操作.但是,当我们在两种方法中进行选择时,要考虑的因素是什么.
例如,考虑一个Runnable Thread在后台执行Web服务的人,现在我想更新UI.
更新我的UI的最佳方法是什么?我应该去Handler还是runOnUiThread?
我仍然知道我可以使用AsyncTask并使用onPostExecute.但我只想知道其中的区别.
对于像计数器那样简单的东西,如果多个线程将增加数量.我读到互斥锁会降低效率,因为线程必须等待.所以,对我来说,一个原子计数器将是最有效的,但我在内部读到它基本上是一个锁?所以我想我很困惑如何比另一个更有效率.
Java Future有一个cancel方法,它可以中断运行Future任务的线程.例如,如果我将一个可中断的阻塞调用包装在一个Java Future我可以稍后中断它.
Scala Future没有提供任何cancel方法.假设我在一个中包含一个可中断的阻塞调用Scala Future.我怎么能打断它?
在他们的arXiv论文中,Julia的原作者提到了以下内容:
2.14 Parallelism.并行执行由Julia在标准库中实现的基于消息的多处理系统提供.语言设计通过提供对称协程来支持这些库的实现,协同协程也可以被认为是协同调度的线程.此功能允许异步通信隐藏在库中,而不是要求用户设置回调.朱目前不支持本地线程,这是一种限制,但具有避免同步共享内存的使用的复杂性的优点.
他们说朱莉娅不支持原生线程是什么意思?什么是本机线程?
其他解释性语言(如Python或R)是否支持这种并行性?朱莉娅独自一人吗?
我在IntelliJ IDEA 14.0.2中有多个线程和断点的奇怪问题.断点在停止之前执行之后的代码.
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
private static final int NUM_CLIENTS = 1000;
static class TestRunnable implements Runnable {
AtomicInteger lock;
@Override
public void run() {
synchronized (this.lock) {
int curCounter = this.lock.addAndGet(1);
System.out.println("Thread: " + Thread.currentThread().getName() + "; Count: " + curCounter);
if (curCounter >= NUM_CLIENTS) {
lock.notifyAll();
}
}
}
}
public static void main(final String args[]) {
final AtomicInteger lock = new AtomicInteger(0);
for (int i = 0; i < NUM_CLIENTS; i++) {
TestRunnable …Run Code Online (Sandbox Code Playgroud) multithreading ×10
java ×4
volatile ×2
android ×1
atomic ×1
browser ×1
concurrency ×1
debugging ×1
future ×1
handler ×1
ios ×1
javascript ×1
julia ×1
mutex ×1
objective-c ×1
pthreads ×1
python ×1
r ×1
scala ×1
uikit ×1