靜態同步函式

2021-06-21 00:31:08 字數 917 閱讀 9615

package com.runnabledemo;

/*** 同步函式被靜態修飾後,它使用是鎖是該函式所屬類的位元組碼檔案物件:類名.class,該物件的型別是class。 * 

* 靜態進入記憶體時,記憶體中沒有該類的物件,但一定有該類對應的位元組碼檔案物件,這個物件是唯一的。 * 

*  * 

* * */

public class threadrunnable implements runnable catch (interruptedexception e)

system.out.println(thread.currentthread().getname()

+ "__sale__" + ticket--); }

} }}

else }

}// 同步函式用的是所屬物件的鎖this

public static synchronized void show() catch (interruptedexception e)

system.out.println(thread.currentthread().getname()

+ "__show__" + ticket--); }

} }/**

* @param args */

public static void main(string args) catch (interruptedexception e)

t.flag = false;

t2.start();

try catch (interruptedexception e)

t3.start();

try catch (interruptedexception e)

t.flag =true;

t4.start(); }

}

靜態同步函式

什麼是靜態同步函式?方法上加上static關鍵字,使用synchronized 關鍵字修飾 或者使用類.class檔案。靜態的同步函式使用的鎖是 該函式所屬位元組碼檔案物件 可以用 getclass方法獲取,也可以用當前 類名.class 表示。public static void sale 總結 ...

靜態同步函式

什麼是靜態同步函式?方法上加上static關鍵字,使用synchronized 關鍵字修飾 或者使用類.class檔案。靜態的同步函式使用的鎖是 該函式所屬位元組碼檔案物件 可以用 getclass方法獲取,也可以用當前 類名.class 表示。public static void sale 總結 ...

多執行緒靜態同步函式

多執行緒靜態同步函式和非靜態同步函式的區別是啥?靜態同步函式 就是方法使用了static關鍵字修飾 非靜態同步函式 沒有用static關鍵字修飾的方法,也就是使用this鎖的同步函式 那麼靜態同步函式使用的啥鎖呢?分析 兩個執行緒,乙個執行緒使用靜態同步函式,乙個使用同步 塊this鎖,如果執行緒不...