运行结果错误
多个线程对同一个变量进行操作导致的运行结果错误
public class WrongResult {volatile static int i;public static void main(String[] args) throws InterruptedException {Runnable r = new Runnable() {@Overridepublic void run() {for (int j = 0; j < 10000; j++) {i++;}}};Thread thread1 = new Thread(r);thread1.start();Thread thread2 = new Thread(r);thread2.start();thread1.join();thread2.join();System.out.println(i);}}
发布和初始化导致线程安全问题
第二种是对象发布和初始化时导致的线程安全问题,我们创建对象并进行发布和初始化供其他类或对象使用是常见的操作,但如果我们操作的时间或地点不对,就可能导致线程安全问题。如代码所示
public class WrongInit {private Map<Integer, String> students;public WrongInit() {new Thread(new Runnable() {@Overridepublic void run() {students = new HashMap<>();students.put(1, "王小美");students.put(2, "钱二宝");students.put(3, "周三");students.put(4, "赵四");}}).start();}public Map<Integer, String> getStudents() {return students;}public static void main(String[] args) throws InterruptedException {WrongInit multiThreadsError6 = new WrongInit();System.out.println(multiThreadsError6.getStudents().get(1));}}
活跃性问题
死锁
死锁是指两个线程之间相互等待对方资源,但同时又互不相让,都想自己先执行,如代码所示
public class MayDeadLock {Object o1 = new Object();Object o2 = new Object();public void thread1() throws InterruptedException {synchronized (o1) {Thread.sleep(500);synchronized (o2) {System.out.println("线程1成功拿到两把锁");}}}public void thread2() throws InterruptedException {synchronized (o2) {Thread.sleep(500);synchronized (o1) {System.out.println("线程2成功拿到两把锁");}}}public static void main(String[] args) {MayDeadLock mayDeadLock = new MayDeadLock();new Thread(new Runnable() {@Overridepublic void run() {try {mayDeadLock.thread1();} catch (InterruptedException e) {e.printStackTrace();}}}).start();new Thread(new Runnable() {@Overridepublic void run() {try {mayDeadLock.thread2();} catch (InterruptedException e) {e.printStackTrace();}}}).start();}}
活锁
第二种活跃性问题是活锁,活锁与死锁非常相似,也是程序一直等不到结果,但对比于死锁,活锁是活的,什么意思呢?因为正在运行的线程并没有阻塞,它始终在运行中,却一直得不到结果。
举一个例子,假设有一个消息队列,队列里放着各种各样需要被处理的消息,而某个消息由于自身被写错了导致不能被正确处理,执行时会报错,可是队列的重试机制会重新把它放在队列头进行优先重试处理,但这个消息本身无论被执行多少次,都无法被正确处理,每次报错后又会被放到队列头进行重试,周而复始,最终导致线程一直处于忙碌状态,但程序始终得不到结果,便发生了活锁问题。
饥饿
第三个典型的活跃性问题是饥饿,饥饿是指线程需要某些资源时始终得不到,尤其是CPU 资源,就会导致线程一直不能运行而产生的问题。在 Java 中有线程优先级的概念,Java 中优先级分为 1 到 10,1 最低,10 最高。如果我们把某个线程的优先级设置为 1,这是最低的优先级,在这种情况下,这个线程就有可能始终分配不到 CPU 资源,而导致长时间无法运行。或者是某个线程始终持有某个文件的锁,而其他线程想要修改文件就必须先获取锁,这样想要修改文件的线程就会陷入饥饿,长时间不能运行。


