package com.test.thread.test; public class ThreadTest { public static final Object lock = new Object(); static class A implements Runnable { public void run() { for (int i = 1; i <= 100; i++) { synchronized (lock) { if (i % 2 == 0) { System.out.println(i); } else { try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } lock.notify(); } } } } static class B implements Runnable { public void run() { for (int i = 1; i <= 100; i++) { synchronized (lock) { if (i % 2 == 1) { System.out.println(i); } else { try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } lock.notify(); } } } } public static void main(String[] args) { new Thread(new A()) { }.start(); new Thread(new B()) { }.start(); } }