- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个使用大量socketio请求的应用程序,并且发出了批量请求。接收数据时,我必须遍历列表以添加和删除项:
List carlist;
void receiveData(Map data) {
// Need a lock here
for (var i = 0; i < carlist.lenght;) {
if (data[carlist[i]['name']]['color'] == carlist[i]['color']) {
carlist.removeAt(i);
onDeleteCar(data); // Update the UI.
}
}
// Need a release lock here
}
最佳答案
Dart没有并发线程,因此一个简单的 bool 变量可以用作互斥体。
如果您需要另一个异步执行来等待直到锁被释放然后继续,那么就不仅仅是返回,而是变得更加复杂了。
bool mutex = false;
void receiveData(Map data) {
// Need a lock here
if(mutex) {
return;
} else {
mutex = true;
}
for (var i = 0; i < carlist.lenght;) {
if (data[carlist[i]['name']]['color'] == carlist[i]['color']) {
carlist.removeAt(i);
onDeleteCar(data); // Update the UI.
}
}
// Need a release lock here
mutex = false;
}
import 'dart:async' as async;
import 'dart:collection' as coll;
import 'dart:math' as math;
void main() {
int j = 0;
int delay = 0;
for(int i = 0; i < 100; i++) {
new async.Future.delayed(
new Duration(milliseconds: delay += rnd.nextInt(150)),
() => receiveData({'${j++}': j}, j)
).catchError((e) => print('Error: $e'));
}
}
//List carlist = [];
WorkQueue workQueue = new WorkQueue(timeout: new Duration(milliseconds: 270));
math.Random rnd = new math.Random();
// had to made this async to make the timeout functionality work
// otherwise Dart would have evaluated the timeouts only after all other
// code has already been finished
async.Future receiveData(Map data, int id) {
// ensure that the code passed to add isn't entered by another async thread
// while it is already executed
// id is just for debugging purposes
return workQueue.add(id, () {
async.Completer completer = new async.Completer();
print('$id start');
// for (var i = 0; i < carlist.length;) {
// carlist.add(data);
// if (data[carlist[i]['name']]['color'] == carlist[i]['color']) {
// carlist.removeAt(i);
// onDeleteCar(data); // Update the UI.
// }
// }
// dummy task to burn time
new async.Future.delayed(new Duration(milliseconds: rnd.nextInt(100)), () {
var val = rnd.nextDouble();
// add some fun - check if code that throws is handled correctly
if(val > 0.9) {
completer.completeError('artifical error ${id}');
print('$id end with error');
} else {
completer.complete();
print('$id end');
}
});
return completer.future;
});
// Need a release lock here
}
// just holds a completer and a closure
class Task {
// complete after f was executed
async.Completer completer = new async.Completer();
// this code should only be entered by one thread at a time
Function f;
// only for debugging purposes
int id;
// ignore timeout if f has already been invoked
bool isInvoked = false;
Task(this.id, this.f, Duration timeout){
if(timeout != null) {
completer.future.timeout(timeout, onTimeout: () {
if(!completer.isCompleted && !isInvoked) {
completer.completeError('${id} timed out');
}
})
// future.timeout creates a new Future which also throws when
// the completer is completed with completeError
// not handling this error ends the app with unhandled exception
.catchError((_) {});
}
}
}
class WorkQueue {
// enque all calls
coll.Queue q = new coll.Queue();
// currently executing?
bool isExecuting = false;
// throw when the execution is delayed longer than the provide timeout
Duration timeout;
WorkQueue({this.timeout});
// enqueue a new execution
async.Future add(int id, Function f) {
print('add $id - queue length: ${q.length}');
var t = new Task(id, f, timeout);
q.add(t);
// ensure that the queue is processed
new async.Future(release);
return t.completer.future;
}
// execute next waiting thread if any
void release() {
// do nothing if closure is currently being executed or queue is empty
if(!isExecuting && !q.isEmpty) {
isExecuting = true;
Task t = q.removeFirst();
// check if t hasn't alredy timed out
if(!t.completer.isCompleted) {
// ignore timeout because we are already being invoked
t.isInvoked = true;
// invoke the closure
new async.Future(t.f)
// handle errors in closure
.catchError((e) {
t.completer.completeError(e);
})
// process next Task in queue
.then((_) {
isExecuting = false;
new async.Future(release);
if(!t.completer.isCompleted) {
t.completer.complete();
}
});
}
}
}
}
add 1 - queue length: 0
1 start
1 end
add 2 - queue length: 0
2 start
add 3 - queue length: 0
2 end
3 start
3 end
add 4 - queue length: 0
4 start
4 end with error
Error: artifical error 4
add 5 - queue length: 0
5 start
5 end
add 6 - queue length: 0
6 start
add 7 - queue length: 0
6 end
7 start
7 end
add 8 - queue length: 0
8 start
add 9 - queue length: 0
add 10 - queue length: 1
8 end with error
Error: artifical error 8
9 start
9 end
10 start
add 11 - queue length: 0
10 end
11 start
add 12 - queue length: 0
add 13 - queue length: 1
11 end
12 start
12 end
13 start
13 end
add 14 - queue length: 0
14 start
14 end
add 15 - queue length: 0
15 start
add 16 - queue length: 0
15 end
16 start
16 end with error
Error: artifical error 16
add 17 - queue length: 0
17 start
17 end
add 18 - queue length: 0
18 start
18 end
add 19 - queue length: 0
19 start
add 20 - queue length: 0
19 end
20 start
add 21 - queue length: 0
20 end
21 start
add 22 - queue length: 0
21 end
22 start
22 end
add 23 - queue length: 0
23 start
23 end
add 24 - queue length: 0
24 start
add 25 - queue length: 0
add 26 - queue length: 1
add 27 - queue length: 2
24 end with error
Error: artifical error 24
25 start
add 28 - queue length: 2
add 29 - queue length: 3
25 end
26 start
add 30 - queue length: 3
26 end
27 start
27 end
28 start
add 31 - queue length: 2
28 end
29 start
29 end
30 start
30 end
31 start
add 32 - queue length: 0
31 end
32 start
32 end
add 33 - queue length: 0
33 start
33 end
add 34 - queue length: 0
34 start
34 end
add 35 - queue length: 0
35 start
35 end
add 36 - queue length: 0
36 start
add 37 - queue length: 0
36 end
37 start
add 38 - queue length: 0
37 end
38 start
add 39 - queue length: 0
38 end with error
Error: artifical error 38
39 start
39 end with error
Error: artifical error 39
add 40 - queue length: 0
40 start
add 41 - queue length: 0
40 end
41 start
41 end with error
Error: artifical error 41
add 42 - queue length: 0
42 start
add 43 - queue length: 0
add 44 - queue length: 1
add 45 - queue length: 2
42 end with error
Error: artifical error 42
43 start
add 46 - queue length: 2
43 end
44 start
add 47 - queue length: 2
add 48 - queue length: 3
add 49 - queue length: 4
44 end
45 start
add 50 - queue length: 4
45 end with error
Error: artifical error 45
46 start
46 end
47 start
add 51 - queue length: 3
47 end
48 start
Error: 49 timed out
48 end
add 52 - queue length: 2
Error: 50 timed out
add 53 - queue length: 3
add 54 - queue length: 4
add 55 - queue length: 5
Error: 51 timed out
add 56 - queue length: 6
Error: 52 timed out
add 57 - queue length: 7
Error: 53 timed out
Error: 54 timed out
add 58 - queue length: 8
Error: 55 timed out
add 59 - queue length: 9
Error: 56 timed out
Error: 57 timed out
add 60 - queue length: 10
Error: 58 timed out
add 61 - queue length: 11
Error: 59 timed out
add 62 - queue length: 12
add 63 - queue length: 13
Error: 60 timed out
add 64 - queue length: 14
Error: 61 timed out
Error: 62 timed out
add 65 - queue length: 15
Error: 63 timed out
add 66 - queue length: 16
Error: 64 timed out
add 67 - queue length: 17
Error: 65 timed out
add 68 - queue length: 18
Error: 66 timed out
add 69 - queue length: 19
add 70 - queue length: 20
add 71 - queue length: 21
Error: 67 timed out
add 72 - queue length: 22
Error: 68 timed out
add 73 - queue length: 23
Error: 69 timed out
Error: 70 timed out
Error: 71 timed out
add 74 - queue length: 24
Error: 72 timed out
Error: 73 timed out
add 75 - queue length: 25
add 76 - queue length: 26
Error: 74 timed out
add 77 - queue length: 27
add 78 - queue length: 28
Error: 75 timed out
Error: 76 timed out
add 79 - queue length: 29
add 80 - queue length: 30
Error: 77 timed out
Error: 78 timed out
add 81 - queue length: 31
add 82 - queue length: 32
add 83 - queue length: 33
Error: 79 timed out
Error: 80 timed out
add 84 - queue length: 34
Error: 81 timed out
add 85 - queue length: 35
Error: 82 timed out
Error: 83 timed out
add 86 - queue length: 36
Error: 84 timed out
add 87 - queue length: 37
Error: 85 timed out
add 88 - queue length: 38
add 89 - queue length: 39
add 90 - queue length: 40
add 91 - queue length: 41
Error: 86 timed out
add 92 - queue length: 42
Error: 87 timed out
Error: 88 timed out
add 93 - queue length: 43
Error: 89 timed out
Error: 90 timed out
Error: 91 timed out
Error: 92 timed out
add 94 - queue length: 44
add 95 - queue length: 45
add 96 - queue length: 46
add 97 - queue length: 47
Error: 93 timed out
add 98 - queue length: 48
Error: 94 timed out
Error: 95 timed out
Error: 96 timed out
add 99 - queue length: 49
Error: 97 timed out
add 100 - queue length: 50
Error: 98 timed out
Error: 99 timed out
Error: 100 timed out
关于locking - 我需要Dart中的Mutex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25067164/
我想知道锁是如何在 Java 中实现的。在一些教程中,我发现它们应该使用 wait() 和 notify()/notifyAll() 以及一些 boolean 标志来实现(它可能比那复杂得多,但基本上
我正在开发一个多线程服务器,用于存储和读取来自数据库的信息。数据库是用 RocksDB 实现的。 我遇到的问题是,当我一次从多个线程访问数据库时,我得到了那个错误。 通常是说db在usign后没有被删
例如,这里有一些 linux 中的代码: void set_leds(int val) { int fd = open ("/dev/console", O_WRONLY); // argumen
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 9 年前。 Improve this qu
考虑以下代码。为了防止IndexOutOfBoundsException打电话时 listIterator ,我们使用读取器锁来检索基于索引的 iteartor,并在对 stockCodes 进行写操
这个问题在这里已经有了答案: Java locking structure best pattern (2 个答案) 关闭 6 年前。 有什么区别: private Lock lock = new
是否可以在网页加载时检查 Scroll Lock、Num Lock 和 Caps Lock 的状态?我找到了使用 JavaScript 在按键后进行检查的方法,但这不是我要问的。 最佳答案 2019
是否可以在网页加载时检查 Scroll Lock、Num Lock 和 Caps Lock 的状态?我找到了使用 JavaScript 在按键后进行检查的方法,但这不是我要问的。 最佳答案 2019
在Ubuntu中安装某些东西时出现错误。我尝试了一些命令来杀死正在运行的进程,但仍然遇到相同的错误。 最佳答案 根据文章How to Fix “Waiting for cache lock: Coul
我们有一个gradle构建,它可以动态创建GradleBuild类型的多个任务,以与不同的客户端库版本一起运行以测试其兼容性。在Gradle4上可以正常工作,但是在移至Gradle5后,执行第一项任务
我在尝试运行在 vertica 数据库表上运行查询的客户端应用程序时反复遇到此错误。 有人可以帮助解决这个问题吗? 最佳答案 我想知道您的客户端应用程序向 Vertica 发送了哪种“查询”(以 RE
我有一个创建锁的方法。 ReadWriteLock lock = new ReentrantReadWriteLock(); 然后我使用 Lock Interface 将该对象传递到一个方法中。 m
正如问题所问,我知道这是可能的on Linux ,但我找不到任何适用于 Windows 的最新信息。有可能吗? 最佳答案 您可以使用 ctypes 加载 user32.dll 然后调用 GetKeyS
这是同一个问题 Change keyboard locks in Python或者 How to change caps lock status without key press . 但还是有区别的
与 this question 相同,不知道为什么Java团队没有在Lock中添加一些默认方法界面,类似这样: public default void withLock(Runnable r) {
我有一个带有package-lock.json文件的项目。 现在,我想基于yarn.lock文件或项目的现有package-lock.json生成node_modules文件。 我怎样才能做到这一点?
我正在查看 pthreadtypes.h 文件中的 pthread_mutex_t 结构。 “__lock”代表什么?它就像分配给互斥锁的锁号吗? typedef union { struct _
我正在研究避免死锁的措施,其中一种可能的方法是通过强制线程放弃它在访问另一个锁但无法访问该锁时已经持有的锁来打破循环等待。 以最简单的银行账户转账为例: class Account { priva
我被要求支持一些遗留代码,我看到了一些让我摸不着头脑的事情。在某些代码段中,我看到类实例使用 CMutex 实例来同步方法执行。例如 class CClassA : public CObject {
当我们调用 lock.lock() 或尝试进入一个 synchronized block 时,如果其他线程已经获取了该锁,我们的线程就会阻塞。现在我的问题是,当我们查看 lock.lock() 的实现
我是一名优秀的程序员,十分优秀!