ThreadLocal 补习

package threads;

 

public class ResourceClass {

 

    // num is a variable that many thread will use but keeped in ThreadLocal

    // seqNum

    // num_nothreadlocal is also a variable that many thread will use but it is

    // ordinary.

    // both of their initial value is 20

 

    private ThreadLocal<Num> seqNum = new ThreadLocal<Num>();

    private int num_nothreadlocal = 20;

 

    // each variable has its getter and setter

    public void setNum(int value) {

       if (seqNum.get() == null)

           seqNum.set(new Num());

 

       seqNum.get().setValue(value);

    }

 

    public int getNum() {

       if (seqNum.get() == null)

           seqNum.set(new Num());

       return seqNum.get().getValue().intValue();

    }

 

    public int getNum_nothreadlocal() {

       return num_nothreadlocal;

    }

 

    public void setNum_nothreadlocal(int numNothreadlocal) {

       num_nothreadlocal = numNothreadlocal;

    }

 

    public ThreadLocal<Num> getLoc() {

       return seqNum;

    }

 

    //this should be in another class

    public static void main(String[] args) {

       ResourceClass shareobj = new ResourceClass();

 

       // here we generate three threads to use the same object shareobj

       ComsumerThread t1 = new ComsumerThread(shareobj);

       ComsumerThread t2 = new ComsumerThread(shareobj);

       ComsumerThread t3 = new ComsumerThread(shareobj);

 

       t1.start();

       t2.start();

       t3.start();

    }

 

    //the thread to use some object class

    private static class ComsumerThread extends Thread {

       private ResourceClass sh;

 

       public ComsumerThread(ResourceClass sh) {

           this.sh = sh;

       }

 

       public void run() {

           // each thread change the value of Num and num_nothreadlocal

           // by increase 1

           for (int i = 1; i <= 3; i++) {

              int nm = sh.getNum();

              nm++;

              sh.setNum(nm);

              // under ThreadLocal

              // each thread has the copy value of num

              // the num increase 1 by one time : 21, 22, 23

 

              nm = sh.getNum_nothreadlocal();

              nm++;

              sh.setNum_nothreadlocal(nm);

              // not predictable

 

              System.out.println("thread[" + Thread.currentThread().getName()

                     + "] sn [" + sh.getNum() + "]" + sh.getLoc().get()

                     + "  nothreadlocal:" + sh.getNum_nothreadlocal());

           }

       }

    }

 

    private class Num {

       private Integer num;

 

       public Num() {

           num = 20;

       }

 

       public Integer getValue() {

           return num;

       }

 

       public void setValue(Integer num) {

           this.num = num;

       }

    }

 

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值