Skip to content

Instantly share code, notes, and snippets.

let queue = DispatchQueue(label: "queue", attributes: .concurrent)
let workItem = DispatchWorkItem {
sleep(2)
print("done")
}
queue.async(execute: workItem)
workItem.notify(queue: .main) {
print("notify!")
let queue = DispatchQueue(label: "queue", attributes: .concurrent)
let workItem = DispatchWorkItem {
sleep(3)
print("done")
}
queue.async(execute: workItem)
print("before waiting")
workItem.wait()
print("after waiting")
@shoheiyokoyama
shoheiyokoyama / DispatchWorkItem-cancel.swift
Last active August 15, 2023 17:33
DispatchWorkItem cancel()
let queue = DispatchQueue(label: "queue", attributes: .concurrent)
let workItem = DispatchWorkItem {
print("done")
}
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) {
queue.async(execute: workItem) // not work
}
workItem.cancel()
@shoheiyokoyama
shoheiyokoyama / WorkItem-perform.swift
Last active May 6, 2017 07:46
DispatchWorkItem perform
let workItem = DispatchWorkItem {
sleep(1)
print("done")
}
workItem.perform() // Execute synchronously
print("after perfrom")
//done
//after perfrom
@shoheiyokoyama
shoheiyokoyama / file0.hs
Last active February 27, 2017 03:14
Swiftエンジニアの高階関数実践 ref: http://qiita.com/shoheiyokoyama/items/8c855e8f5caf7d136027
applyTwice :: (a -> a) -> a -> a
applyTwice f x = f (f x)
@shoheiyokoyama
shoheiyokoyama / file0.sh
Last active February 26, 2017 06:04
Swiftエンジニアが学ぶ高階関数「カリー化関数」 ref: http://qiita.com/shoheiyokoyama/items/04416501389c69544c5a
Prelude> max 4 5
5
Prelude> (max 4) 5
5
@shoheiyokoyama
shoheiyokoyama / file0.swift
Last active January 15, 2017 05:40
iOSエンジニアの正規表現入門 ref: http://qiita.com/shoheiyokoyama/items/5dc67fdc9e06a9dc5728
extension String {
func isNumber() -> Bool {
let pattern = "^[\\d]+$"
guard let regex = try? NSRegularExpression(pattern: pattern) else { return false }
let matches = regex.matches(in: self, range: NSRange(location: 0, length: length))
return matches.count > 0
}
}
@available(iOS 10.0, *)
public enum NCWidgetDisplayMode : Int {
case compact // Fixed height
case expanded // Variable height
}
let group = DispatchGroup()
let queue1 = DispatchQueue(label: "com.GCD.groupQueue1")
let queue2 = DispatchQueue(label: "com.GCD.groupQueue2")
let queue3 = DispatchQueue(label: "com.GCD.groupQueue3")
queue1.async(group: group) {
sleep(4)
print("excute queue1")
}
public class DegitObserver implements Observer {
public void update(NumberGenerator generator) {
System.out.println("DegitObserver:" + generator.getNumber());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
}