前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >013.多线程-ThreadLocal

013.多线程-ThreadLocal

作者头像
qubianzhong
发布2018-12-13 17:01:48
2410
发布2018-12-13 17:01:48
举报
文章被收录于专栏:行者常至行者常至

版权声明:本文为博主原创文章,允许转载,请标明出处。

为每一个线程提供一个局部变量。

code of demo:

  • 创建三个线程,分别打印递增的IDS

错误demo

代码语言:javascript
复制
package cn.qbz.thread;

/**
 * @Author: 花生米
 * @Date: 2018/11/16 18:15
 */
public class ThreadLocalTest {
    public static void main(String[] args) {
        //创建三个线程,每个线程均获得其IDS
        Ids ids = new Ids();
        GetIds t1 = new GetIds(ids);
        GetIds t2 = new GetIds(ids);
        GetIds t3 = new GetIds(ids);

        t1.start();
        t2.start();
        t3.start();
    }

}

class GetIds extends Thread {
    private Ids ids;

    public GetIds(Ids ids) {
        this.ids = ids;
    }

    @Override
    public void run() {
        for (int i = 0; i < 3; i++) {
            System.out.println(getName() + "..." + ids.nextId());
        }
    }
}

class Ids {
    private int id = 0;

    public int nextId() {
        return id++;
    }
}

通过创建多个被执行对象来解决

代码语言:javascript
复制
package cn.qbz.thread;

/**
 * @Author: 花生米
 * @Date: 2018/11/16 18:15
 */
public class ThreadLocalTest {
    public static void main(String[] args) {
        //创建三个线程,每个线程均获得其IDS
        Ids ids1 = new Ids();
        Ids ids2 = new Ids();
        Ids ids3 = new Ids();
        GetIds t1 = new GetIds(ids1);
        GetIds t2 = new GetIds(ids2);
        GetIds t3 = new GetIds(ids3);

        t1.start();
        t2.start();
        t3.start();
    }

}

class GetIds extends Thread {
    private Ids ids;

    public GetIds(Ids ids) {
        this.ids = ids;
    }

    @Override
    public void run() {
        for (int i = 0; i < 3; i++) {
            System.out.println(getName() + "..." + ids.nextId());
        }
    }
}

class Ids {
    private int id = 0;

    public int nextId() {
        return id++;
    }
}

使用Thread Local包装Ids

代码语言:javascript
复制
package cn.qbz.thread;

/**
 * @Author: 花生米
 * @Date: 2018/11/16 18:15
 */
public class ThreadLocalTest {
    public static void main(String[] args) {
        //创建三个线程,每个线程均获得其IDS
        Ids ids = new Ids();
        GetIds t1 = new GetIds(ids);
        GetIds t2 = new GetIds(ids);
        GetIds t3 = new GetIds(ids);

        t1.start();
        t2.start();
        t3.start();
    }

}

class GetIds extends Thread {
    private Ids ids;

    public GetIds(Ids ids) {
        this.ids = ids;
    }

    @Override
    public void run() {
        for (int i = 0; i < 3; i++) {
            System.out.println(getName() + "..." + ids.nextId());
        }
    }
}

class Ids {
    ThreadLocal<Integer> threadLocal = ThreadLocal.withInitial(() -> 0);

    public int nextId() {
        threadLocal.set(threadLocal.get() + 1);
        return threadLocal.get();
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年11月16日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • code of demo:
    • 错误demo
      • 通过创建多个被执行对象来解决
        • 使用Thread Local包装Ids
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档