临界区互斥访问的方法

0 同步机制遵循准则

  1. 空闲让进:临界区空闲时,允许请求进入临界区
  2. 忙则等待:有线程进入临界区时,其他线程必须等待
  3. 有限等待:等待不能无限制持续下去
  4. 让权等待:释放等待线程的 CPU 资源

1 单标志

问题:违背空闲让进(当有一个线程结束,另外一个即使没有结束也无法进入临界区)

public class SingleFlag { 
    public static volatile boolean flag;
    public static volatile int num = 0;

    public static void main(String[] args) { 
        new Thread(() -> { 
            while (true) { 
                while (flag) ;
                System.out.println(Thread.currentThread().getName() + " " + ++num);
                flag = false;
            }
        }).start();

        new Thread(() -> { 
            while (true) { 
                while (flag) ;
                System.out.println(Thread.currentThread().getName() + " " + ++num);
                flag = true;
            }
        }).start();
    }
}

2 双标志先检查

问题:违背忙则等待(恰好执行完 1 时线程切换,此时 2 判断也能通过,则会存在安全问题)

public class DoubleFlag { 
    public static volatile boolean[] flag={ false, true};
    public static volatile int num = 0;

    public static void main(String[] args) { 
        new Thread(() -> { 
            while (true) { 
                while (flag[0]); // 1
                flag[1] = true; 
                System.out.println(Thread.currentThread().getName() + " " + ++num);
                flag[1] = false;
            }
        }).start();

        new Thread(() -> { 
            while (true) { 
                while (flag[1]); // 2
                flag[0] = true; 
                System.out.println(Thread.currentThread().getName() + " " + ++num);
                flag[0] = false;
            }
        }).start();
    }
}

3 双标志后检查

问题:违背有限等待原则(两个线程无限制谦让,导致活锁)

public class DoubleFlagAfterCheck { 
    public static volatile boolean[] flag = { false, true};
    public static volatile int num = 0;

    public static void main(String[] args) { 
        new Thread(() -> { 
            while (true) { 
                flag[1] = true; 
                while (flag[0]);
                System.out.println(Thread.currentThread().getName() + " " + ++num);
                flag[1] = false;
            }
        }).start();

        new Thread(() -> { 
            while (true) { 
                flag[0] = true; 
                while (flag[1]);
                System.out.println(Thread.currentThread().getName() + " " + ++num);
                flag[0] = false;
            }
        }).start();
    }
}

4 Peterson

完美解决并发安全性问题
问题:无法达到让权等待(CPU 一直在死循环判断,占用 CPU 资源)

public class Peterson { 
    public static volatile boolean[] flag = { false, false};
    public static volatile int turn = 0;
    public static volatile int num = 0;

    public static void main(String[] args) { 
        new Thread(() -> { 
            while (true) { 
                flag[0] = true;
                turn = 2;
                while (flag[1] && turn == 2) ;
                System.out.println(Thread.currentThread().getName() + " " + ++num);
                flag[0] = false;
            }

        }).start();

        new Thread(() -> { 
            while (true) { 
                flag[1] = true;
                turn = 1;
                while (flag[0] && turn == 1) ;
                System.out.println(Thread.currentThread().getName() + " " + ++num);
                flag[1] = false;
            }
        }).start();
    }
}
    原文作者:不搞数学的汤老师
    原文地址: https://blog.csdn.net/u013570834/article/details/109198517
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞