当前位置:网站首页>Gcdqueue encapsulation
Gcdqueue encapsulation
2022-07-26 01:00:00 【JH_ Cao】
1. DispatchQueue It's actually an abstract concept , Not a queue in a data structure
demand : To create a queue , Add synchronous serial task
Code encapsulation :
class GCDQueue: NSObject {
var syncQueue: DispatchQueue?
var head: SingalLinkList = SingalLinkList()
var count: Int = 0
let lock: NSLock = NSLock()
init(_ queueLabel: String) {
syncQueue = DispatchQueue(label: queueLabel)
}
public func clear() {
count = 0
head.next = nil
}
public func getNode(index: Int) -> SingalLinkList? {
var curIndex = index
checkIndex(index: &curIndex)
var node = head.next
for _ in 0..<index {
node = node?.next
}
return node
}
public func add<T>(task: T, _ index: Int = -1) {
lock.lock()
var curIndex = index == -1 ? count : index
checkIndex(index: &curIndex)
if count == 0 {
head.next = SingalLinkList(ele: task, nextL: nil)
} else {
let pre = getNode(index: curIndex - 1)
let node = getNode(index: curIndex)
pre?.next = SingalLinkList(ele: task, nextL: node)
}
count += 1
lock.unlock()
}
public func doTask() {
// Implementation subclass
}
private func checkIndex(index: inout Int) {
if index < 0 || index > count {
print(" The subscript crossing the line , Put it last ")
index = count
}
}
}
class SingalLinkList {
var next: SingalLinkList?
var element: AnyObject?
init() {
element = nil
next = nil
}
init<T>(ele: T?, nextL: SingalLinkList?) {
element = ele as AnyObject
next = nextL
}
}
- Specific tasks should be done , Need to inherit GCDQueue, rewrite doTask Method
class taskQueue: GCDQueue {
override func doTask() {
if count == 0 {
return
}
guard let temp = head.next?.element as? testTask else {
return
}
print("doTask--",temp.val)
count -= 1
let node = head.next?.next
head.next = node
doTask()
}
}
- Use cases
class ViewController: UIViewController {
var queue: taskQueue = taskQueue("testQueue")
override func viewDidLoad() {
super.viewDidLoad()
for i in 0..<5 {
let task = testTask(value: i)
queue.add(task: task)
}
let temp = testTask(value: 100)
queue.add(task: temp, 9)
queue.doTask()
}
}
- Print the results
doTask-- 0
doTask-- 1
doTask-- 2
doTask-- 100
doTask-- 3
doTask-- 4
边栏推荐
- [oops framework] interface management
- Oauth2 and JWT
- Unityvr -- robot arm scene 4- gifts and Christmas tree
- Force deduction record: Sword finger offer (2) -- jz13-22
- typing‘ has no attribute ‘_ SpecialForm‘
- 【秒杀概念】原反补
- Day06 MySQL knowledge points summary
- With data-driven management transformation, the first year of science and technology was at the right time
- 【RTOS训练营】GPIO知识和预习安排 + 晚课提问
- 【RTOS训练营】关于上课和答疑
猜你喜欢
![[laser principle and application -3]: foreign brands of lasers](/img/8a/620544bb52c6b8c4db83f9a14783bb.png)
[laser principle and application -3]: foreign brands of lasers

Ssd7 | embedded friendly target detection network, product landing
![[RTOS training camp] ring buffer, at instruction, preview arrangement and evening class questions](/img/cb/de38e9f4186c4392ef8832659422ff.jpg)
[RTOS training camp] ring buffer, at instruction, preview arrangement and evening class questions

How can a team making facial mask achieve a revenue of more than 1 million a day?

聊聊研发团队中的“人”
![[RTOS training camp] equipment subsystem, questions of evening students](/img/12/20fc69b5faf4c2d6dfaacde8142e33.jpg)
[RTOS training camp] equipment subsystem, questions of evening students

SQL statement exercise

Suddenly found an optimization artifact

Spine_附件皮肤

AI knows everything: build and deploy sign language recognition system from 0
随机推荐
聊聊研发团队中的“人”
The difference and application of in and exists in SQL statement
[install software after computer reset] software that can search all files of the computer, the best screenshot software in the world, free music player, JDK installation, MySQL installation, installa
Spine_附件皮肤
Win11打不开自带杀毒软件怎么办?win11自带杀毒功能打不开
openvino安装踩坑笔记
Set set learning
Unityvr robot Scene 3 gripper
Database tools duel: heidisql and Navicat
Unity get the animation being played
Nanjie's embarrassment
[RTOS training camp] program framework, preview, after-school homework and evening class questions
Oauth2 and JWT
Solve the problem that when the background image is set to be 100% full, when the horizontal scroll bar appears in the zoom browser, the part of the background image beyond the scroll bar is not full
Redis Command Reference Manual - key
How can I become an irreplaceable programmer?
中心对称的二进制模式CSLBP,matlab
el-table滚动条设置
【纪中】2022.7.16 1432.输油管道
[IJCAI 2022] parameter efficient large model sparse training method, which greatly reduces the resources required for sparse training