//①线程死锁查看packagecom.chapter12;publicclassThreadDeadLock{publicstaticvoidmain(String[] args){StringBuilder s1 =newStringBuilder();StringBuilder s2 =newStringBuilder();newThread(){@Overridepublicvoidrun(){synchronized(s1){try{Thread.sleep(100);}catch(InterruptedException e){
e.printStackTrace();}synchronized(s2){
s1.append("b");
s2.append("2");System.out.println(s1);System.out.println(s2);}}}}.start();newThread(){@Overridepublicvoidrun(){synchronized(s2){try{Thread.sleep(100);}catch(InterruptedException e){
e.printStackTrace();}synchronized(s1){
s1.append("d");
s2.append("4");System.out.println(s1);System.out.println(s2);}}}}.start();}}C:\Users\Administrator\IdeaProjects\jvm>jstack 12840
执行结果:
Found one Java-level deadlock:============================="Thread-1":
waiting tolock monitor 0x0000000002d0b288(object 0x000000071649ee08, a java.lang.StringBuilder),
which is held by "Thread-0""Thread-0":
waiting tolock monitor 0x0000000002d0c728(object 0x000000071649ee50, a java.lang.StringBuilder),
which is held by "Thread-1"Java stack information for the threads listed above:==================================================="Thread-1":
at com.chapter12.ThreadDeadLock$2.run(ThreadDeadLock.java:40)- waiting tolock<0x000000071649ee08>(a java.lang.StringBuilder)- locked <0x000000071649ee50>(a java.lang.StringBuilder)"Thread-0":
at com.chapter12.ThreadDeadLock$1.run(ThreadDeadLock.java:22)- waiting tolock<0x000000071649ee50>(a java.lang.StringBuilder)- locked <0x000000071649ee08>(a java.lang.StringBuilder)Found1 deadlock.//①线程Sleep查看publicclassThreadSleepTest{publicstaticvoidmain(String[] args){System.out.println("hello - 1");try{Thread.sleep(1000*60*10);}catch(InterruptedException e){
e.printStackTrace();}System.out.println("hello - 2");}}//执行结果>> jstack 920"main" #1 prio=5 os_prio=0 tid=0x00000000036d2800 nid=0x1cb0 waiting on condition [0x000000000320f000]java.lang.Thread.State:TIMED_WAITING(sleeping)
at java.lang.Thread.sleep(NativeMethod)
at com.chapter12.ThreadSleepTest.main(ThreadSleepTest.java:12)//③线程同步测试classNumberimplementsRunnable{privateint number =1;@Overridepublicvoidrun(){while(true){synchronized(this){if(number <=100){try{Thread.sleep(500);}catch(InterruptedException e){
e.printStackTrace();}System.out.println(Thread.currentThread().getName()+":"+ number);
number++;}else{break;}}}}}publicclassThreadSyncTest{publicstaticvoidmain(String[] args){Number number =newNumber();Thread t1 =newThread(number);Thread t2 =newThread(number);
t1.setName("线程1");
t2.setName("线程2");
t1.start();
t2.start();}}//执行结果"线程2" #12 prio=5 os_prio=0 tid=0x0000000027b56800 nid=0x3380 waiting on condition [0x000000002879f000]java.lang.Thread.State:TIMED_WAITING(sleeping)
at java.lang.Thread.sleep(NativeMethod)
at com.chapter12.Number.run(ThreadSyncTest.java:15)- locked <0x00000007164a1960>(a com.chapter12.Number)
at java.lang.Thread.run(Thread.java:748)"线程1" #11 prio=5 os_prio=0 tid=0x0000000027b4d800 nid=0x1d18 waiting for monitor entry [0x000000002869f000]java.lang.Thread.State:BLOCKED(on object monitor)
at com.chapter12.Number.run(ThreadSyncTest.java:13)- waiting tolock<0x00000007164a1960>(a com.chapter12.Number)
at java.lang.Thread.run(Thread.java:748)
成功解决“ModuleNotFoundError: No Module Named ‘utils’”错误的全面指南
在Python编程中,遇到ModuleNotFoundError: No Module Named utils这样的错误通常意味着Python解释器无法找到名为utils的模块。这可能是由于多种原因造成的,比如模块确实不存…
1103. 分糖果 II - 力扣(LeetCode) /*** Note: The returned array must be malloced, assume caller calls free().*/
int* distributeCandies(int candies, int num_people, int* returnSize)
{int num 0;int* arr (int*)malloc(sizeof(int)*num_peo…