从头认识多线程-1.2 共享变量与线程安全简介

本文详细介绍了共享变量的概念及使用,通过不同代码示例展示了线程安全问题及其解决方案,包括非共享变量、共享变量及同步代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这一章节我们来简单讨论一下共享变量与线程安全简介。

1.共享变量

没有共享的代码:

package com.ray.deepintothread.ch01.topic_2;

public class NotShareTheVariables {
	public static void main(String[] args) {
		for (int i = 0; i < 3; i++) {
			RunnableTwo runnableTwo = new RunnableTwo();
			Thread thread = new Thread(runnableTwo);
			thread.start();
		}
	}
}

class RunnableTwo implements Runnable {

	private int count = 5;

	@Override
	public void run() {
		while (count > 0) {
			System.out.println("由线程" + Thread.currentThread().getName() + "计算 " + count--);
		}
	}
}

输出:

由线程Thread-0计算 5
由线程Thread-0计算 4
由线程Thread-0计算 3
由线程Thread-0计算 2
由线程Thread-0计算 1
由线程Thread-2计算 5
由线程Thread-2计算 4
由线程Thread-2计算 3
由线程Thread-2计算 2
由线程Thread-1计算 5
由线程Thread-2计算 1
由线程Thread-1计算 4
由线程Thread-1计算 3
由线程Thread-1计算 2
由线程Thread-1计算 1


从代码我们可以看见,其实我们建立了3个任务(笔者比较喜欢用任务来命名放入线程里面的Target),分别使用三个线程来执行,因此每一个线程所输出的结果是连续的。

从输出我们可以看见,cup执行线程任务的分配是随机的,是按照cup的调度算法来分配,因此输出呈现出随机状态。


共享变量的代码:

package com.ray.deepintothread.ch01.topic_2;

public class AnsychThread {
	public static void main(String[] args) {
			RunnableThree runnableThree = new RunnableThree();
		for (int i = 0; i < 5; i++) {
			Thread thread = new Thread(runnableThree);
			thread.start();
		}
	}
}

class RunnableThree implements Runnable {

	private int count = 5;

	@Override
	public void run() {
		System.out.println("由线程" + Thread.currentThread().getName() + "计算 " + count--);
	}
}

输出:

由线程Thread-0计算 5
由线程Thread-2计算 3
由线程Thread-1计算 4
由线程Thread-3计算 2
由线程Thread-4计算 1


上面的代码是这样的,建立一个任务,然后由5个线程同时执行这个任务。

从上面的输出可以看到,当5个线程同时执行一个任务的时候,它将会共享任务实例的变量,而且由于“--”的过程分为三步,第一步是取值,第二步是想减,第三步是赋值,而且这三步没有同步,因此很可能当线程1取值的之后,就被线程2想减,因此会造成输出的混乱。

上面的代码就会引起线程安全的问题。


共享变量并同步的代码:

package com.ray.deepintothread.ch01.topic_2;

public class SychThreadWithTag {
	public static void main(String[] args) {
		RunnableFour runnableFour = new RunnableFour();
		for (int i = 0; i < 5; i++) {
			Thread thread = new Thread(runnableFour);
			thread.start();
		}
	}
}

class RunnableFour implements Runnable {

	private int count = 5;

	@Override
	public synchronized void run() {
		System.out.println("由线程" + Thread.currentThread().getName() + "计算 " + count--);
	}
}

输出:

由线程Thread-0计算 5
由线程Thread-2计算 4
由线程Thread-1计算 3
由线程Thread-4计算 2
由线程Thread-3计算 1


由于我们加上了synchronized关键字作同步,因此每一次“--”只有一个线程,其他线程均不能够进入里面操作,因此,输出会变得有序。


总结:这一章节简单介绍了一下共享变量与线程安全。


我的github:https://github.com/raylee2015/DeepIntoThread

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值