java多线程全局变量共享问题

         先看下面问题:多个线程访问全局变量x,然后将x与i累加,启动10个线程,想让每个线程的输出结果都是一样的55,但是实际不是的。

package ThreadTest;

public class Counter {

	private int x =0;
	
	// 计数方法
	public void count() {
		for(int i=0;i<=10;i++) {
			x = x+i;
		}
		System.out.println(Thread.currentThread().getName()+"--"+x);
	}
	
	public static void main(String[] args) {
		// 定义线程实现接口
		Runnable runnable = new Runnable(){
			Counter counter = new Counter();
			@Override
			public void run() {
				counter.count();
			}
		};
		// 启动10个线程
		for(int i=0;i<10;i++) {
			new Thread(runnable).start();
		}
	}
}

            实际输出是无规则的。

解决方案一:

package ThreadTest;

public class Counter {

	ThreadLocal<Integer> th=new ThreadLocal<Integer>(){
		protected Integer initialValue() {
			return 0;
		}
	};
	// 计数方法
	public void count() {
		for(int i=0;i<=10;i++) {
			th.set(th.get()+i);
		}
		System.out.println(Thread.currentThread().getName()+"--"+th.get());
	}
	
}

用ThreadLocal,输出结果是55,ok!

由此可以证明,ThreadLocal为每一个线程保存每一个变量,而且每个线程拿到的都是自己的那一份。

解决方案二:

将全局变量局部化

public class Count {


    public void count() {  
        int x =0;
        for(int i = 1; i <= 10; i++) {
            x=x+i;
        }  
        System.out.println(Thread.currentThread().getName() + "-" + x);  
    }  
}

每个线程都有一份自己的局部变量,因此不会产生线程问题。

解决方案三:对象局部化

public static void main(String[] args) {
       Runnable runnable = new Runnable() {               
           public void run() {  
            Count count = new Count();
               count.count();  
           }  
       };  
       for(int i = 0; i < 10; i++) {  
           new Thread(runnable).start();  
       }  
}

每个对象都会有一个自己的全局变量。不会产生线程问题。

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值