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

2022-09-06 06:24:06 字數 4215 閱讀 3139

join
package

com.thread.demo.base;

/*** join方法的使用:

* 作用: 讓其主線程等待子執行緒完成後再執行

* @author

administrator *

*/public

class

threadjoin

catch

(interruptedexception e)

system.out.println("main thread end");

}static

class myrunnable implements

runnable

catch

(interruptedexception e)

system.out.println(thread.currentthread().getname()+"執行中.....");}}

}}

守護執行緒
package

com.thread.demo.base;

import

j**a.io.ioexception;

/*** 測試守護執行緒

* 作用: 為其非守護執行緒提供服務

* @author

administrator *

*/public

class

threaddaemon catch (interruptedexception e)

*///

i/o阻塞

try

catch

(ioexception e)

system.out.println("main thread end!");

}static

class myrunnable implements

runnable

catch

(interruptedexception e)

system.out.println(thread.currentthread().getname()+"執行gc操作準備....");}}

}static

class myrunnable1 implements

runnable

catch

(interruptedexception e)

system.out.println(thread.currentthread().getname()+"執行錄屏....");}}

}}

執行緒禮讓

package

com.thread.demo.base;

/*** 執行緒禮讓

* @author

administrator *

*/public

class

threadyield

}},"aaa");

thread t2 = new thread(new

runnable() }}

},"bbb");

//啟動執行緒

t1.start();

t2.start();

//t1讓t2執行

}}

執行緒優化級
package

com.thread.demo.base;

/*** 執行緒優化級: 提供執行緒優於其他執行緒執行的機率

* @author

administrator *

*/public

class

threadpriority

static

class myrunnable implements

runnable catch (interruptedexception e)

*/system.out.println(thread.currentthread().getname()+"執行gc操作準備....");}}

}static

class myrunnable1 implements

runnable catch (interruptedexception e)

*/system.out.println(thread.currentthread().getname()+"執行錄屏....");}}

}}

執行緒安全問題

package

com.thread.demo.safe;

/*** 執行緒安全問題: 多個執行緒共享同乙個資源導致

* @author

administrator *

*/public

class

threadsafe1

static

class mythread extends

thread

@override

public

void

run()

catch

(interruptedexception e)

tname =thread.currentthread().getname();

system.out.println(tname+"賣出了"+(count--)+"票");}}

}}

package

com.thread.demo.safe;

/*** 執行緒安全問題: 多個執行緒共享同乙個資源導致

* @author

administrator *

*/public

class

threadsafe2

catch

(interruptedexception e)

system.out.println("count="+count);

}static

class myrunnable implements

runnable }}

}

同步塊
package

com.thread.demo.safe.sync;

/*** 使用同步塊解決執行緒安全的問題

* @author

administrator *

*/public

class

syncblock

catch

(interruptedexception e)

system.out.println("count="+count);

}static

class myrunnable implements

runnable }}

}}

同步關鍵字
package

com.thread.demo.safe.sync;

/*** 使用同步關鍵字加鎖物件的例項方法

* @author

administrator *

*/public

class

syncinstance

catch

(interruptedexception e)

system.out.println("count="+count);

}static

class myrunnable implements

runnable

@override

public

void

run() }}

}

class類鎖
package

com.thread.demo.safe.sync;

/*** 使用同步關鍵字加鎖class物件

* @author

administrator *

*/public

class

syncclass

catch

(interruptedexception e)

system.out.println("count="+count);

}static

class myrunnable implements

runnable

@override

public

void

run() }}

}

執行緒同步和執行緒安全

執行緒同步,嚴格來說,是程式通過專用的機制來保證多個並行執行的執行緒在同一時刻不會執行指定的程式段。的執行緒安全,指的是多執行緒以安全執行的方式操作共享資料結構。或者簡單的說,就是程式在多執行緒環境下執行而不會引發資料錯誤。有多種策略實現執行緒安全的資料結構。執行緒同步是現實執行緒安全的一種手段。不...

static 靜態方法 執行緒安全

public class test public static string hello string str string tmp tmp tmp str return tmp hello方法會不會有多執行緒安全問題呢?沒有!靜態方法如果沒有使用靜態變數,則沒有執行緒安全問題。為什麼呢?因為靜態方...

執行緒安全與同步 鎖優化

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