執行緒同步鎖物件不同導致的安全問題及解決辦法

2021-06-27 09:52:19 字數 4858 閱讀 2637

執行緒同步鎖的物件有三個:

1)、objcet物件

2)、this物件

3)、class檔案物件

總結:執行緒同步鎖物件是this,若是靜態函式,則是類名.class

例如:

class ticket implements runnable

public void run()catch(interruptedexception e){}

system.out.println(thread.currentthread().getname()+" "+name+"code..."+ticket--);}}

}}else

while(true)

} //如下函式同步的鎖物件是this

public synchronized void show()catch(interruptedexception e){}

system.out.println(thread.currentthread().getname()+" "+name+"show..."+ticket--);

} }}public class threaddemo catch(interruptedexception e){}

t1.flag=true;

tt2.start();

}}

執行結果是:

thread-0  t1show...20

thread-1 t1code...19

thread-0 t1show...18

thread-1 t1code...17

thread-0 t1show...16

thread-1 t1code...15

thread-0 t1show...14

thread-1 t1code...13

thread-0 t1show...12

thread-1 t1code...11

thread-0 t1show...10

thread-1 t1code...9

thread-0 t1show...8

thread-1 t1code...7

thread-0 t1show...6

thread-1 t1code...5

thread-1 t1code...4

thread-0 t1show...3

thread-1 t1code...2

thread-0 t1show...1

thread-1 t1code...0

執行結果出現ticket為0的情況,因為同步鎖的物件不同,分別是object和this物件

2、若共享操作用的是同乙個鎖物件,則結果正確,修改後的**如下:

class ticket implements runnable

public void run()catch(interruptedexception e){}

system.out.println(thread.currentthread().getname()+" "+name+"code..."+ticket--);}}

}}else

while(true)

} //如下函式同步的鎖物件是this

public synchronized void show()catch(interruptedexception e){}

system.out.println(thread.currentthread().getname()+" "+name+"show..."+ticket--);

} }}

執行後結果是:

thread-0  t1code...20

thread-0 t1code...19

thread-0 t1code...18

thread-0 t1code...17

thread-0 t1code...16

thread-0 t1code...15

thread-0 t1code...14

thread-0 t1code...13

thread-0 t1code...12

thread-0 t1code...11

thread-0 t1code...10

thread-0 t1code...9

thread-0 t1code...8

thread-0 t1code...7

thread-0 t1code...6

thread-0 t1code...5

thread-0 t1code...4

thread-0 t1code...3

thread-0 t1code...2

thread-0 t1code...1

class ticket implements runnable

public void run()catch(interruptedexception e){}

system.out.println(thread.currentthread().getname()+" "+name+"code..."+ticket--);}}

}}else

while(true) }

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

system.out.println(thread.currentthread().getname()+" "+name+"show..."+ticket--);

} }}public class threaddemo catch(interruptedexception e){}

t1.flag=true;

tt2.start();

}}

執行結果是:

thread-0  t1show...20

thread-1 t1code...19

thread-0 t1show...18

thread-1 t1code...17

thread-0 t1show...16

thread-1 t1code...15

thread-0 t1show...14

thread-1 t1code...13

thread-0 t1show...12

thread-1 t1code...11

thread-0 t1show...10

thread-1 t1code...9

thread-0 t1show...8

thread-1 t1code...7

thread-0 t1show...6

thread-1 t1code...5

thread-1 t1code...4

thread-0 t1show...3

thread-1 t1code...2

thread-0 t1show...1

thread-1 t1code...0

分析原因後發現:靜態函式在記憶體中不存在,但是有該類對應的位元組碼檔案

優化後的**是:

class ticket implements runnable

public void run()catch(interruptedexception e){}

system.out.println(thread.currentthread().getname()+" "+name+"code..."+ticket--);}}

}}else

while(true)

} //含有static修飾符的函式同步的鎖物件是ticket.class

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

system.out.println(thread.currentthread().getname()+" "+name+"show..."+ticket--);

} }}public class threaddemo catch(interruptedexception e){}

t1.flag=true;

tt2.start();

}}

執行後的結果是:

thread-0  t1code...20

thread-0 t1code...19

thread-0 t1code...18

thread-0 t1code...17

thread-0 t1code...16

thread-0 t1code...15

thread-0 t1code...14

thread-0 t1code...13

thread-0 t1code...12

thread-0 t1code...11

thread-0 t1code...10

thread-0 t1code...9

thread-0 t1code...8

thread-0 t1code...7

thread-0 t1code...6

thread-0 t1code...5

thread-0 t1code...4

thread-0 t1code...3

thread-0 t1code...2

thread-0 t1code...1

執行緒安全與同步 鎖優化

無同步 a.可重入 b.threadlocal 互斥同步 阻塞同步 synchronized lock lock的優勢 可中斷 可有多個newcondition 自定義是否公平鎖 非阻塞同步 cas 機器指令實現 unsafe loop cas cas問題 a.aba問題 解決 加鎖 b.迴圈等待問...

Linux 多執行緒不同步導致錯誤示例

多個執行緒都可以看到同乙個物件並有對其操作時涉及到同步問題。不同步現象 counter.h ifndef counter h define counter h class counter endif counter.c include counter.h counter counter counte...

執行緒常用方法,執行緒安全和同步鎖

joinpackage com.thread.demo.base join方法的使用 作用 讓其主線程等待子執行緒完成後再執行 author administrator public class threadjoin catch interruptedexception e system.out.p...