最后是今天给大家分享的一些独家干货:
【Android开发核心知识点笔记】
【Android思维脑图(技能树)】
【Android核心高级技术PDF文档,BAT大厂面试真题解析】
【Android高级架构视频学习资源】
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
pthread_mutex_t mutex4 = PTHREAD_MUTEX_INITIALIZER;
static int sequence1 = 0;
static int sequence2 = 0;
int func1() {
pthread_mutex_lock(&mutex1);
++sequence1;
sleep(1);
pthread_mutex_lock(&mutex2);
++sequence2;
pthread_mutex_unlock(&mutex2);
pthread_mutex_unlock(&mutex1);
return sequence1;
}
int func2() {
pthread_mutex_lock(&mutex2);
++sequence2;
sleep(1);
pthread_mutex_lock(&mutex1);
++sequence1;
pthread_mutex_unlock(&mutex1);
pthread_mutex_unlock(&mutex2);
return sequence2;
}
void* thread1(void* arg) {
while (1) {
int iRetValue = func1();
if (iRetValue == 100000) {
pthread_exit(NULL);
}
}
}
void* thread2(void* arg) {
while (1) {
int iRetValue = func2();
if (iRetValue == 100000) {
pthread_exit(NULL);
}
}
}
void* thread3(void* arg) {
while (1) {
sleep(1);
char szBuf[128];
memset(szBuf, 0, sizeof(szBuf));
strcpy(szBuf, “thread3”);
}
}
void* thread4(void* arg) {
while (1) {
sleep(1);
char szBuf[128];
memset(szBuf, 0, sizeof(szBuf));
strcpy(szBuf, “thread3”);
}
}
int main() {
pthread_t tid[4];
if (pthread_create(&tid[0], NULL, &thread1, NULL) != 0) {
_exit(1);
}
if (pthread_create(&tid[1], NULL, &thread2, NULL) != 0) {
_exit(1);
}
if (pthread_create(&tid[2], NULL, &thread3, NULL) != 0) {
_exit(1);
}
if (pthread_create(&tid[3], NULL, &thread4, NULL) != 0) {
_exit(1);
}
sleep(5);
// pthread_cancel(tid[0]);
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
pthread_join(tid[2], NULL);
pthread_join(tid[3], NULL);
pthread_mutex_destroy(&mutex1);
pthread_mutex_destroy(&mutex2);
pthread_mutex_destroy(&mutex3);
pthread_mutex_destroy(&mutex4);
return 0;
}
编译执行:
# g++ -lpthread -g thread.cpp
会发现程序如期的卡住了。
利用pstack获取线程快照
# pstack 28443 >> stack.txt
线程快照如下
Thread 5 (Thread 0x7f159bb6d700 (LWP 28444)):
#0 0x00007f159c75df4d in __lll_lock_wait () from /lib64/libpthread.so.0
#1 0x00007f159c759d02 in _L_lock_791 () from /lib64/libpthread.so.0
#2 0x00007f159c759c08 in pthread_mutex_lock () from /lib64/libpthread.so.0
#3 0x00000000004008d1 in func1 () at thread.cpp:17
#4 0x0000000000400969 in thread1 (arg=0x0) at thread.cpp:38
#5 0x00007f159c757dc5 in start_thread () from /lib64/libpthread.so.0
#6 0x00007f159bc641cd in clone () from /lib64/libc.so.6
Thread 4 (Thread 0x7f159b36c700 (LWP 28445)):
#0 0x00007f159c75df4d in __lll_lock_wait () from /lib64/libpthread.so.0
#1 0x00007f159c759d02 in _L_lock_791 () from /lib64/libpthread.so.0
#2 0x00007f159c759c08 in pthread_mutex_lock () from /lib64/libpthread.so.0
#3 0x000000000040092d in func2 () at thread.cpp:29
#4 0x0000000000400992 in thread2 (arg=0x0) at thread.cpp:47
#5 0x00007f159c757dc5 in start_thread () from /lib64/libpthread.so.0
#6 0x00007f159bc641cd in clone () from /lib64/libc.so.6
Thread 3 (Thread 0x7f159ab6b700 (LWP 28446)):
#0 0x00007f159bc2b41d in nanosleep () from /lib64/libc.so.6
#1 0x00007f159bc2b2b4 in sleep () from /lib64/libc.so.6
#2 0x00000000004009c6 in thread3 (arg=0x0) at thread.cpp:56
#3 0x00007f159c757dc5 in start_thread () from /lib64/libpthread.so.0
#4 0x00007f159bc641cd in clone () from /lib64/libc.so.6
Thread 2 (Thread 0x7f159a36a700 (LWP 28447)):
#0 0x00007f159bc2b41d in nanosleep () from /lib64/libc.so.6
#1 0x00007f159bc2b2b4 in sleep () from /lib64/libc.so.6
#2 0x0000000000400a0b in thread4 (arg=0x0) at thread.cpp:65
#3 0x00007f159c757dc5 in start_thread () from /lib64/libpthread.so.0
#4 0x00007f159bc641cd in clone () from /lib64/libc.so.6
Thread 1 (Thread 0x7f159cb72740 (LWP 28443)):
#0 0x00007f159c758ef7 in pthread_join () from /lib64/libpthread.so.0
#1 0x0000000000400b1b in main () at thread.cpp:89
由快照可以看出线程4和5一直处于等锁状态(pthread_mutex_lock),可以推测线程4和5发生了死锁。
最后
我坚信,坚持学习,每天进步一点,滴水穿石,我们离成功都很近!
以下是总结出来的字节经典面试题目,包含:计算机网络,Kotlin,数据结构与算法,Framework源码,微信小程序,NDK音视频开发,计算机网络等。
字节高级Android经典面试题和答案
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[外链图片转存中…(img-SVg8NKXq-1715762234938)]
[外链图片转存中…(img-uv8Vp1LW-1715762234938)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!