线程范围内变量

线程内部共享数据,线程间数据独立

传统方式

方法描述

创建一个静态的全局的HashMap,以当前线程的名字作为key值,来进行共享数据的存储。

代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package cn.itcast.heima2;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class ThreadScopeShareData {

private static Map<Thread, Integer> threadData = new HashMap<Thread, Integer>();
public static void main(String[] args) {
for(int i=0;i<2;i++){
new Thread(new Runnable(){
@Override
public void run() {
int data = new Random().nextInt();
System.out.println(Thread.currentThread().getName() + " has put data :" + data);
threadData.put(Thread.currentThread(), data);
new A().get();
new B().get();
}
}).start();
}
}

static class A{
public void get(){
int data = threadData.get(Thread.currentThread());
System.out.println("A from " + Thread.currentThread().getName() + " get data :" + data);
}
}

static class B{
public void get(){
int data = threadData.get(Thread.currentThread());
System.out.println("B from " + Thread.currentThread().getName()+ " get data :" + data);
}
}
}

使用ThreadLocal类

方法描述

ThreadLocal类是java为了我们方便实现线程内部共享变量,而线程间变量独立,所提供的一个类。使用它可以轻松的实现线程内部共享变量,线程间变量独立。

示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

import java.util.HashMap;
import java.util.Map;
import java.util.Random;


public class ThreadLocalTest {


public static void main(String[] args) {
new ThreadLocalTest().init();
}
//init
private void init(){
for(int i =0;i<2;i++){
new Thread(new Runnable() {

public void run() {

int data = new Random().nextInt();
Person.getThreadInstance().setName(Thread.currentThread().getName());
Person.getThreadInstance().setAge(data);
new A().get();
new B().get();
}
}).start();
}
}
//A
class A {
Person person = Person.getThreadInstance();
public void get(){
System.out.println("A:-"+Thread.currentThread().getName()+":name:"+person.getName()+":age:"+person.getAge());
}
}
//B
class B {
Person person = Person.getThreadInstance();
public void get(){
System.out.println("B:-"+Thread.currentThread().getName()+":name:"+person.getName()+":age:"+person.getAge());
}
}
//Person 将跟线程相关的绑定,放在共享的数据类的内部实现
static class Person{
private static ThreadLocal<Person> threadLocal = new ThreadLocal<ThreadLocalTest.Person>();

private Person(){

}

public static Person getThreadInstance(){
Person person = threadLocal.get();
if(person==null){
person = new Person();
threadLocal.set(person);
}
return person;.
}

private String name;
private int age;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.util.HashMap;
import java.util.Map;
import java.util.Random;


public class ThreadLocalTest {


public static final ThreadLocal<Person> threadlocal = new ThreadLocal(){

@Override
protected Object initialValue() {

return new Person();
}

};

public static void main(String[] args) {
new ThreadLocalTest().init();
}

private void init(){
for(int i =0;i<2;i++){
new Thread(new Runnable() {

public void run() {

int data = new Random().nextInt();
threadlocal.get().setName(Thread.currentThread().getName());
threadlocal.get().setAge(data);
new A().get();
new B().get();
}
}).start();
}
}
//A
class A {
Person person = threadlocal.get();
public void get(){
System.out.println("A:-"+Thread.currentThread().getName()+":name:"+person.getName()+":age:"+person.getAge());
}
}
//B
class B {
Person person = threadlocal.get();
public void get(){
System.out.println("B:-"+Thread.currentThread().getName()+":name:"+person.getName()+":age:"+person.getAge());
}
}
//Person
static class Person{

public Person(){

}

private String name;
private int age;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

}

}

当前网速较慢或者你使用的浏览器不支持博客特定功能,请尝试刷新或换用Chrome、Firefox等现代浏览器