ThreadLocal:每個(gè)線程自身的存儲本地、局部區(qū)域,類似于容器,每個(gè)線程都會(huì)在其中有一定存儲空間
常用的方法get/set/initialValue
官方建議為private static
每個(gè)線程存儲自己的數(shù)據(jù),更改不會(huì)影響其他線程
ThreadLocal 子類InheritableThreadLocal:
繼承上下文環(huán)境的數(shù)據(jù)
public class my {
//Integer 初始值為null
//private static ThreadLocal<Integer> threadlocal=new ThreadLocal<>();
//更改初始值需要?jiǎng)?chuàng)建ThreadLocal的子類重寫initialValue或者使用lambda(jdk8)
private static ThreadLocal<Integer> threadlocal=new ThreadLocal <Integer>()
{//父類匿名內(nèi)部類重寫方法
protected Integer initialValue()
{
return 200;
}
};
//private static ThreadLocal<Integer> threadlocal=new ThreadLocal.withInitial(()->200);
public static void main(String[]args) throws InterruptedException
{
//獲取值
System.out.println(Thread.currentThread().getName()+"-->"+threadlocal.get());
//設(shè)置值
threadlocal.set(99);
System.out.println(Thread.currentThread().getName()+"-->"+threadlocal.get());
new Thread(new test()).start();
new Thread(new test()).start();
}
public static class test implements Runnable
{
public void run()
{
threadlocal.set((int)(Math.random()*100));
System.out.println(Thread.currentThread().getName()+"-->"+threadlocal.get());
}
}
}
每個(gè)線程自身的數(shù)據(jù)更改不會(huì)影響其他線程
public class my {
private static ThreadLocal<Integer> threadlocal=new ThreadLocal <Integer>()
{//父類匿名內(nèi)部類重寫方法
protected Integer initialValue()
{
return 1;
}
};
public static void main(String[]args) throws InterruptedException
{
for(int i=0;i<5;i++)
{
new Thread(new test()).start();
}
}
public static class test implements Runnable
{
public void run()
{
//修改不會(huì)影響其他線程
Integer left=threadlocal.get();
System.out.println(Thread.currentThread().getName()+"-->"+left);
threadlocal.set(left-1);
System.out.println(Thread.currentThread().getName()+"還剩下"+threadlocal.get());
}
}
}
分析ThreadLocal運(yùn)行環(huán)境:
public class my {
private static ThreadLocal<Integer> threadlocal=new ThreadLocal <Integer>()
{//父類匿名內(nèi)部類重寫方法
protected Integer initialValue()
{
return 1;
}
};
public static void main(String[]args) throws InterruptedException
{
new Thread(new test()).start();//兩個(gè)打印的內(nèi)容在不同的線程里
//當(dāng)start()之后,線程才從main轉(zhuǎn)換到其他線程,所以構(gòu)造器里的是main線程的
}
public static class test implements Runnable
{
public test()
{
threadlocal.set(100); //屬于main線程,修改不會(huì)影響start內(nèi)的線程
System.out.println(Thread.currentThread().getName()+"-->"+threadlocal.get());
}
public void run()
{
System.out.println(Thread.currentThread().getName()+"還剩下"+threadlocal.get());
}
}
}
子類InheritableThreadLocal:
public class my {
private static ThreadLocal<Integer> threadlocal=new InheritableThreadLocal<>();
public static void main(String[]args) throws InterruptedException
{
System.out.println(Thread.currentThread().getName()+"-->"+threadlocal.get());
threadlocal.set(2);
//此線程由main線程開辟,拷貝一份main線程的數(shù)據(jù)給此線程
//即將main線程里的2給此線程
new Thread(()->{
System.out.println(Thread.currentThread().getName()+"-->"+threadlocal.get());
threadlocal.set(200);
System.out.println(Thread.currentThread().getName()+"-->"+threadlocal.get());
}).start();
}
}
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動(dòng)態(tài)BGP最優(yōu)骨干路由自動(dòng)選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動(dòng)現(xiàn)已開啟,新人活動(dòng)云服務(wù)器買多久送多久。
分享題目:java多線程-ThreadLocal-創(chuàng)新互聯(lián)
網(wǎng)站路徑:http://chinadenli.net/article48/ehhep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、網(wǎng)站導(dǎo)航、虛擬主機(jī)、外貿(mào)建站、定制網(wǎng)站、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)