java多執行緒學習 七

2021-08-19 17:52:39 字數 3714 閱讀 8892

總結:

本篇主要介紹內部類和多執行緒以及鎖物件變化時候的情況

package chapter2.innertest2;

import chapter2.innertest2.outclass.innerclass1;

import chapter2.innertest2.outclass.innerclass2;

/*演示innerclass1的method1方法對innerclass2上鎖,其他執行緒只能以同步的方式呼叫innerclass2中的靜態同步方法

同步還是非同步,就是判斷執行緒進入時,物件監視器是什麼,這個物件是否有鎖,這個鎖是否被其他執行緒持有(或者說該執行緒能否取得該物件鎖)

從輸出可以看出t1和t2是非同步的,但是t1和t3是同步的

輸出為:

t2進入innerclass1 的method2方法

t1進入innerclass1 的method1方法

j=0i=0

i=1j=1

i=2j=2

i=3j=3

i=4j=4

i=5j=5

j=6i=6

j=7i=7

j=8i=8

j=9i=9

t2離開innerclass1 的method2方法

t1離開innerclass1 的method1方法

t3進入innerclass2 的method1方法

k=0k=1

k=2k=3

k=4k=5

k=6k=7

k=8k=9

t3離開innerclass2 的method1方法

*/public class run

}, "t1");

thread t2=new thread(new runnable()

}, "t2");

thread t3=new thread(new runnable()

},"t3");

t1.start();

t2.start();

t3.start(); }}

package chapter2.innertest2;

public class outclass catch (interruptedexception e)

}system.out.println(threadname + "離開innerclass1 的method1方法");

}} public synchronized void method2() catch (interruptedexception e)

}system.out.println(threadname + "離開innerclass1 的method2方法");

} }static class innerclass2 catch (interruptedexception e)

}system.out.println(threadname + "離開innerclass2 的method1方法");

} }}

在將任何資料型別作為同步鎖時,需要注意的是,是否有多個執行緒同時持有鎖物件,如果同時持有相同的鎖物件,則這些執行緒之間就是同步的;如果分別獲得鎖物件,這些執行緒之間就是非同步的.

以string作為鎖

package chapter2.setnewstringtwolock;

public class myservice

} catch (interruptedexception e) }}

package chapter2.setnewstringtwolock;

/* * 演示鎖物件變化的情況

* 注釋掉main執行緒的sleep過程

* a,b執行緒同時請求lock鎖,a先持有,b等待,a,b執行緒的鎖為同乙個,都是"123",所以是同步的

* 輸出為:

a beging time=1525543623392

a end time=1525543625393

b beging time=1525543625394

b end time=1525543627394

*/public class run1

}package chapter2.setnewstringtwolock;

/* * 演示鎖物件變化的情況

* a執行緒執行的時候,改變了lock的物件,b執行緒獲取鎖的時候,和a的鎖不同,所以是非同步的

* 輸出為:

* a beging time=1525543489477

b beging time=1525543489527

a end time=1525543491478

b end time=1525543491527

*/public class run2

}package chapter2.setnewstringtwolock;

public class threada extends thread

@override

public void run()

}package chapter2.setnewstringtwolock;

public class threadb extends thread

@override

public void run()

}

鎖物件的屬性變化,還是同乙個物件,就是同乙個鎖

package chapter2.setnewpropertieslockone;

/* * 演示當鎖物件的屬性變化時,只要還是同乙個物件,那麼多執行緒間還是同步的,

* 和string不同,如果string的值變化,string就是不同的物件,

* 輸出為:

aend time=1525544321226

bend time=1525544324227

*/public class run catch (interruptedexception e) }}

package chapter2.setnewpropertieslockone;

public class service catch (interruptedexception e)

} }}package chapter2.setnewpropertieslockone;

public class threada extends thread

@override

public void run()

} package chapter2.setnewpropertieslockone;

public class threadb extends thread

@override

public void run()

} package chapter2.setnewpropertieslockone;

public class userinfo

public userinfo(string un,string pwd)

public string getusername()

public void setusername(string username)

public string getpasswd()

public void setpasswd(string passwd)

}

參考:

多執行緒學習(七)

問題 1.賣票系統 如果每個執行緒執行的 相同,可以使用同乙個runnable物件,這個runnable物件中有那個共享資料 解決public class multithreadsharedatastudy class ticketsystem implements runnable 2.設計4個執...

Java多執行緒(第七章)

1.blocked 阻塞 thread state for a thread blocked waiting for a monitor lock.2.new 新建 thread state for a thread which has not yet started.3.runnable 執行或就...

Java多執行緒學習筆記

程序與執行緒 1.程序 執行中的程式 乙個程序至少包含乙個執行緒 2.執行緒 程序中負責程式執行的執行單元 執行緒本身依靠程式進行執行 執行緒是程式中的順序控制流,只能使用分配給程式的資源和環境 3.單執行緒 程式中只存在乙個執行緒,實際上主方法就是乙個主線程 4.多執行緒 在乙個程式中執行多個任務...