diff --git a/src/test/java/cn/whaifree/test/tse.java b/src/test/java/cn/whaifree/test/tse.java new file mode 100644 index 0000000..f9ddbc5 --- /dev/null +++ b/src/test/java/cn/whaifree/test/tse.java @@ -0,0 +1,74 @@ +package cn.whaifree.test; + +public class tse { + + final static Object o = new Object(); + volatile static int num = 0; + public static void main(String[] args) { + + new Thread(new Runnable() { + @Override + public void run() { + try { + for (int i = 0; i < 100; i++) { + synchronized (o) { + while (num % 3 != 0) { + o.wait(); + } + num++; + System.out.println("A"); + o.notifyAll(); + } + + } + } catch (Exception e) { + e.printStackTrace(); + } + } + }).start(); + + new Thread(new Runnable() { + @Override + public void run() { + try { + for (int i = 0; i < 100; i++) { + synchronized (o) { + while (num % 3 != 1) { + o.wait(); + } + num++; + System.out.println("B"); + o.notifyAll(); + } + + } + } catch (Exception e) { + e.printStackTrace(); + } + } + }).start(); + + new Thread(new Runnable() { + @Override + public void run() { + try { + for (int i = 0; i < 100; i++) { + synchronized (o) { + while (num % 3 != 2) { + o.wait(); + } + num++; + System.out.println("C"); + o.notifyAll(); + } + + } + } catch (Exception e) { + e.printStackTrace(); + } + } + }).start(); + + + } +}