生活随笔
收集整理的這篇文章主要介紹了
Java Day 13
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
線程的狀態
?被創建 運行 凍結 消亡
?
?被創建--start()--> 運行
?運行----run()----> 消亡
???????? stop()
?運行---sleep(time)---> 凍結
??????? wait()-->
??????? <--notify()
?
?cpu 執行資格 被cup處理、處于隊列中
?cpu 執行權?? 正在被cpu處理
創建線程的第二種方法
?實現Runnable接口
?
?Thread t1 = new Thread(d)//d是實現Runnable接口類創建的對象
?1、定義類實現Runnable接口
?2、覆蓋接口中的run方法,將線程任務封裝
?3、通過Thread類創建線程對象,將對象作為線程構造函數的參數進行傳遞
?4、調用線程對象的start方法
細節
?單獨進行封裝
線程安全問題
?1、多線程操作共享數據
?2、操作共享數據的線程代碼有多條
同步代碼塊
?將多條操作共享數據的線程代碼封裝起來,只允許一個線程訪問
?synchronized(對象){ 代碼 }
同步代碼塊 利弊
?利 解決了線程的安全問題
?弊 效率降低
?同步的前提 同步中必須有多個線程使用同一個鎖
同步函數
?
同步函數的鎖
?同步函數使用的鎖是this
靜態同步函數的鎖
?函數所屬字節碼文件對象
?this.getClass();
?類.class
1 class Ticket
implements Runnable{
//extends Thread
2 private int num = 100
;
3 Object obj =
new Object();
4 boolean flag =
true;
5
6 public void run(){
7 //Object obj = new Object();
8 if(flag){
9 while(
true){
10 show();
11 if(num==0
)
12 return;
13 }
14 }
else{
15 while(
true){
16 synchronized(
this){
17 if(num>0
){
18 try{
19 Thread.sleep(10
);
20 }
catch(InterruptedException e){
21 }
22 System.out.println(Thread.currentThread().getName()+"...block..."+num--
);
23 }
24 }
25 if(num==0
)
26 return;
27 //}
28 }
29 }
30
31 }
32
33 public synchronized void show(){
34 if(num>0
){
35 try{
36 Thread.sleep(10
);
37 }
catch(InterruptedException e){ }
38 System.out.println(Thread.currentThread().getName()+"...func..."+num--
);
39 }
40 }
41 }
42
43
44 public class SaleTicketsDemo{
45 public static void main(String[] args){
46 /*Ticket t1 = new Ticket();
47 Ticket t2 = new Ticket();
48 Ticket t3 = new Ticket();
49 Ticket t4 = new Ticket();
50
51 t1.start();
52 t2.start();
53 t3.start();
54 t4.start();*/
55 Ticket t =
new Ticket();
56 Thread t1 =
new Thread(t);
57 Thread t2 =
new Thread(t);
58
59 t1.start();
60 try{Thread.sleep(10);}
catch(InterruptedException e){ }
61 t.flag =
false;
62 t2.start();
63
64 }
65 }
?
單例模式 的多線程問題
?
1 class Single{
2 private static Single s =
null;
3 private Single(){}
4 public static Single getInstance(){
5 if(s==
null){
6 synchronized(Single.
class){
7 if(s==
null){
8 s =
new Single();
9 }
10 }
11 }
12 return s;
13 }
14 }
死鎖
?1、同步的嵌套
1 class Test
implements Runnable{
2 private boolean flag;
3 Test(
boolean blag){
4 this.flag =
flag;
5 }
6 public void run(){
7 if(flag){
8 while(
true){
9 synchronized(MyLock.locka){
10 System.out.println(Thread.currentThread().getName()+" if locka..."
);
11 synchronized(MyLock.lockb){
12 System.out.println(Thread.currentThread().getName()+" if lockb..."
);
13 }
14 }
15 }
16 }
else{
17 while(
true){
18 synchronized(MyLock.lockb){
19 System.out.println(Thread.currentThread().getName()+" else lockb..."
);
20 synchronized(MyLock.locka){
21 System.out.println(Thread.currentThread().getName()+" else locka..."
);
22 }
23 }
24 }
25 }
26 }
27 }
28 class MyLock{
29 public static final Object locka =
new Object();
30 public static final Object lockb =
new Object();
31 }
32 class DeadLockTest{
33 public static void main(String[] args){
34 Test a =
new Test(
true);
35 Test b =
new Test(
false);
36
37 Thread t1 =
new Thread(a);
38 Thread t2 =
new Thread(b);
39 t1.start();
40 t2.start();
41 }
42 }
View Code ?
轉載于:https://www.cnblogs.com/zhuzhuqwa/p/5907516.html
總結
以上是生活随笔為你收集整理的Java Day 13的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。