基本数据类型
- 简单读写场景:像int、double、boolean等基本数据类型,在单个线程中进行简单的赋值和读取操作时,是线程安全的。因为这些操作在单个线程内具有原子性,不会被其他线程干扰。
- 多线程并发读写场景:当多个线程并发地对同一个基本数据类型变量进行读写操作时,就可能出现线程安全问题。例如,多个线程同时对一个int型变量进行自增操作,由于自增操作不是原子的,可能会导致数据丢失或不一致。不过,Java 提供了Atomic系列的原子类,如AtomicInteger、AtomicBoolean等,使用这些原子类可以保证基本数据类型在多线程环境下的原子性操作,从而确保线程安全。
不安全的int实例
public class IntThreadSafetyExample {
private static int count = 0;
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
count++;
}
});
Thread thread2 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
count--;
}
});
thread1.start();
thread2.start();
thread1.join();
thread2.join();
System.out.println("Count value: " + count);
}
}
在上述代码中,两个线程同时对count变量进行自增和自减操作。由于int类型的自增和自减操作不是原子性的,在多线程环境下会出现数据不一致的情况,最终输出的count值不一定是0。
保证线程安全的int实例
public class volatileInt {
private static volatile int count = 0;
public static synchronized void increment() {
count++;
}
}
在上述代码中, volatile关键字可以保证多线程变量的可见性(一个线程修改后,其他线程立即看到最新值),但不保证原子性。所以这里仍然需要synchronized关键字
保证线程安全的原子类Integer实例
import java.util.concurrent.atomic.AtomicInteger;
public class AtomicIntThreadSafetyExample {
private static AtomicInteger count = new AtomicInteger(0);
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
count.incrementAndGet();
}
});
Thread thread2 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
count.decrementAndGet();
}
});
thread1.start();
thread2.start();
thread1.join();
thread2.join();
System.out.println("Count value: " + count.get());
}
}
在上述代码中,这里使用AtomicInteger来保证count变量在多线程环境下的原子性操作,无论有多少线程同时对其进行增减操作,都能保证数据的一致性,最终输出的count值一定是0。
引用数据类型
- 不可变的引用数据类型:以String类为例,由于String是不可变的,一旦创建就不能修改其内容。所以在多线程环境下,多个线程同时访问同一个String对象是线程安全的,因为不存在数据被修改的风险。类似的还有Integer、Double等包装类,它们也是不可变的,在多线程读取时是安全的。
- 可变的引用数据类型
- 线程安全的可变引用数据类型:Java 中有一些集合类是线程安全的,如ConcurrentHashMap、CopyOnWriteArrayList等。这些类在设计时考虑了多线程并发访问的情况,通过各种锁机制或其他同步手段来保证在多线程环境下的安全访问。
- 非线程安全的可变引用数据类型:像普通的ArrayList、HashMap等集合类,在多线程环境下如果多个线程同时对其进行读写操作,就会出现线程安全问题。例如,一个线程正在向ArrayList中添加元素,另一个线程同时进行删除元素的操作,可能会导致数组越界、数据丢失等问题。
非线程安全的ArrayList实例
import java.util.ArrayList;
import java.util.List;
public class ArrayListThreadSafetyExample {
private static List<Integer> list = new ArrayList<>();
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
list.add(i);
}
});
Thread thread2 = new Thread(() -> {
for (int i = 0; i < 500; i++) {
list.remove(i);
}
});
thread1.start();
thread2.start();
thread1.join();
thread2.join();
System.out.println("List size: " + list.size());
}
}
在上述例子中,两个线程同时对ArrayList进行添加和删除元素的操作。由于ArrayList不是线程安全的,在多线程并发访问时可能会出现ConcurrentModificationException异常,或者导致数据不一致的情况,比如元素丢失或数组越界等问题。
线程安全的CopyOnWriteArrayList实例
import java.util.concurrent.CopyOnWriteArrayList;
public class CopyOnWriteArrayListExample {
private static CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<>();
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
list.add(i);
}
});
Thread thread2 = new Thread(() -> {
for (int i = 0; i < 500; i++) {
list.remove(i);
}
});
thread1.start();
thread2.start();
thread1.join();
thread2.join();
System.out.println("List size: " + list.size());
}
}
CopyOnWriteArrayList是线程安全的集合类,它在进行写操作时会创建一个新的数组来复制原数组的内容,然后在新数组上进行操作,这样就不会影响到其他线程对集合的读取操作,从而保证了多线程环境下的安全性。
基本数据类型和引用数据类型本身并不直接决定其是否线程安全,关键在于它们在多线程环境中的使用方式以及是否采取了适当的同步措施来保证数据的一致性和完整性。

被折叠的 条评论
为什么被折叠?



