CountDownLatch vs. Semaphore(CountDownLatch vs. Semaphore)
有什么优势使用
java.util.concurrent.CountdownLatch
代替
java.util.concurrent.Semaphore ?
据我所知,以下片段几乎相当:
信号量
final Semaphore sem = new Semaphore(0); for (int i = 0; i < num_threads; ++ i) { Thread t = new Thread() { public void run() { try { doStuff(); } finally { sem.release(); } } }; t.start(); } sem.acquire(num_threads);
2:CountDownLatch
final CountDownLatch latch = new CountDownLatch(num_threads); for (int i = 0; i < num_threads; ++ i) { Thread t = new Thread() { public void run() { try { doStuff(); } finally { latch.countDown(); } } }; t.start(); } latch.await();
除了在#2的情况下,锁存器不能重复使用,更重要的是,您需要事先知道将创建多少个线程(或等待它们在创建锁存器之前全部启动)。
那么在什么情况下,锁存器会更好?
Is there any advantage of using
java.util.concurrent.CountdownLatch
instead of
java.util.concurrent.Semaphore?
As far as I can tell the following fragments are almost equivalent:
1. Semaphore
final Semaphore sem = new Semaphore(0); for (int i = 0; i < num_threads; ++ i) { Thread t = new Thread() { public void run() { try { doStuff(); } finally { sem.release(); } } }; t.start(); } sem.acquire(num_threads);
2: CountDownLatch
final CountDownLatch latch = new CountDownLatch(num_threads); for (int i = 0; i < num_threads; ++ i) { Thread t = new Thread() { public void run() { try { doStuff(); } finally { latch.countDown(); } } }; t.start(); } latch.await();
Except that in case #2 the latch cannot be reused and more importantly you need to know in advance how many threads will be created (or wait until they are all started before creating the latch.)
So in what situation might the latch be preferable?
原文:https://stackoverflow.com/questions/184147
最满意答案
尝试添加SKLabelNode作为SKSpriteNode的子项。
SKLabelNode *label = [[SKLabelNode alloc]initWithFontNamed:@"Helvetica"]; label.position = CGPointMake(0, -label.frame.size.height/2); SKSpriteNode *background = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(label.frame.size.width, label.frame.size.height)]; background.position = CGPointMake(200, 100); [background addChild:label]; [self addChild:background];
Try adding the SKLabelNode as a child of a SKSpriteNode.
SKLabelNode *label = [[SKLabelNode alloc]initWithFontNamed:@"Helvetica"]; label.position = CGPointMake(0, -label.frame.size.height/2); SKSpriteNode *background = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(label.frame.size.width, label.frame.size.height)]; background.position = CGPointMake(200, 100); [background addChild:label]; [self addChild:background];
相关问答
更多-
尝试添加SKLabelNode作为SKSpriteNode的子项。 SKLabelNode *label = [[SKLabelNode alloc]initWithFontNamed:@"Helvetica"]; label.position = CGPointMake(0, -label.frame.size.height/2); SKSpriteNode *background = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:C ...
-
您没有正确设置标签的位置,因此它可能在屏幕外。 你的代码适合我。 尝试这个: override func didMoveToView(view: SKView) { backgroundColor = SKColor(colorLiteralRed: 0.4, green: 0.835, blue: 1, alpha: 1) //The color is a light blue, so the node should not be hidden by the color ...
-
您的问题是CGPoint(x:CGRectGetMidX(mainNode.frame), y:CGRectGetMidY(mainNode.frame))使您的标签超出范围。 因此,如果您希望SKLabelNode水平和垂直居中,您可以这样做 let myLabel = SKLabelNode(fontNamed:"Chalkduster") myLabel.text = "Hello, World!" myLabel.fontSize = 45 myLabel.horizontalAlignmentMo ...
-
着色.fontColor而不是.color解决了问题! roundLabel.fontColor = UIColor.red 作品! Coloring the .fontColor instead of the .color solved the problem! roundLabel.fontColor = UIColor.red works!
-
如果你反思你的想法,这将更容易。 将红色层放在后面/底部。 最低的zPosition。 让我们说这个例子的-1.0 。 将图片置于此顶部,zPosition为0.0 把上面的字母放在zPostion为1.0 所以,从iPhone的屏幕来看,它是这样的: 屏幕 信 图片 红色BG 你想用这封信来掩盖画面。 完成此操作后,无论何时移动该字母,它都会从图片中显示其下方的内容。 其他一切都是红色的。 If you invert your thinking, this will be much easier. Put ...
-
将SKLabelNode设置为SKSpriteNode Swift的中心(Setting SKLabelNode to centre of SKSpriteNode Swift)[2022-03-08]
您对标签位置的计算是错误的。 您只需要在0,0位置添加一个标签,由于SpriteKit的坐标系规则(0,0位于节点的中心),它将居中。 override func didMove(to view: SKView) { let label = SKLabelNode(fontNamed: "Arial") label.text = "C" label.fontColor = .black label.verticalAlignmentMode ... -
将要使用的字体名称放入本地化,然后使用NSLocalizedString获取它。 Put the name of the font to use into your localization, and use NSLocalizedString to fetch it.
-
你需要使用类似的东西 SKColor(red: 233/255, green: 157/255, blue: 20/255, alpha: 1) 因为红色,绿色和蓝色是介于0.0和1.0之间的值 you need to use something like SKColor(red: 233/255, green: 157/255, blue: 20/255, alpha: 1) because red, green and blue is a value between 0.0 and 1.0
-
几个星期前我遇到过类似的问题。 尝试将颜色变量更改为以下内容: var color = UIColor(red: 1.0 / 255, green: 0.0 / 255, blue: 0.0 / 255, alpha: 0.0) I had a similar problem a few weeks ago. Try changing your color variable to the following: var color = UIColor(red: 1.0 / 255, green: 0.0 / ...
-
每个SKNode都有一个scene属性,它返回节点所在的场景。 而且每一个SKScene都是一个view属性,所以 override func touchesBegan(_ touches: Set
, with event: UIEvent?) { let nextScene = goesTo self.scene?.view?.presentScene(nextScene) } Every SKNode has a scene property which retu ...