在北京时参加金山校招的一道笔试题:
四个线程,a b c d ,共享一个变量 i, ab 为加线程,cd 为减线程,四个线程依次执行,顺序为 abcd,输出为 0 1 2 1 0 1 2 1 0……
当时觉得这道题还可以,不一会儿就写出来了,我的思路是:四个线程,ab为一个锁,cd为一个锁,两个锁交替执行,每个锁的两个线程交替执行。后来回校后,zw问这道题,我又重新打了一下,写了好长时间,老是不对,线程的执行顺序总是控制不好,线程语法也老出错,一阵o(╯□╰)o,基础太薄弱……搜了搜方茅塞顿开。
public class Jinsan2 { static int i = 0; static Object lock = new Object(); static boolean flag1 = true, flag2 = true; public static void main(String args[]) { Thread a = new Thread("a") { @Override public void run() { for (;;) { synchronized (lock) { while (!flag1 || !flag2) { // 可以过时,应 while try { lock.wait(); } catch (InterruptedException e) { } } System.out.println(Thread.currentThread().getName()+" "+i++ + " "); flag2 = !flag2; lock.notifyAll(); } } } }; Thread b = new Thread("b") { @Override public void run() { for (;;) { synchronized (lock) { while (!flag1 || flag2) { try { lock.wait(); } catch (InterruptedException e) { } } System.out.println(Thread.currentThread().getName()+" "+i++ + " "); flag1 = false; flag2 = !flag2; lock.notifyAll(); } } } }; Thread c = new Thread("c") { @Override public void run() { for (;;) { synchronized (lock) { while (flag1 || !flag2) { try { lock.wait(); } catch (InterruptedException e) { } } System.out.println(Thread.currentThread().getName()+" "+i-- + " "); flag2 = !flag2; lock.notifyAll(); } } } }; Thread d = new Thread("d") { @Override public void run() { for (;;) { synchronized (lock) { while (flag1 || flag2) { try { lock.wait(); } catch (InterruptedException e) { } } System.out.println(Thread.currentThread().getName()+" "+i-- + " "); flag1 = true; flag2 = !flag2; lock.notifyAll(); } } } }; a.start(); b.start(); c.start(); d.start(); } }
还有一个更好的方法就是为每个线程都标一个ID,a b c d 分别对象 0 1 2 3 ,这样,所有线程都共享一个 count 变量,每个线程执行时,都 首先获count%4 的值,如果对应自己线程的ID,那么就执行,否则等待,id=0 || id=1 表示是 a b 线程,则对 i 进行 加操作,否则进行 减操作这样四个线程都可以用同一个逻辑进行操作
public class Test { private static int count; static int i; private static Object lock = new Object(); static class Threads implements Runnable { int id; public Threads(int id) { this.id = id; } @Override public void run() { for (;;) { synchronized (lock) { while (id != count % 4) { try { lock.wait(); } catch (InterruptedException e) { } } if (id == 0 || id == 1) { System.out.println(Thread.currentThread().getName() + " " + i++ + " "); } if (id == 2 || id == 3) System.out.println(Thread.currentThread().getName() + " " + i-- + " "); count++; lock.notifyAll(); } } } } public static void main(String args[]) { Thread a = new Thread(new Threads(0), "a"); Thread b = new Thread(new Threads(1), "b"); Thread c = new Thread(new Threads(2), "c"); Thread d = new Thread(new Threads(3), "d"); a.start(); b.start(); c.start(); d.start(); } }
另外我调用wait(),notify()时调用的是this的,正确的调用应该是哪个锁对象就调用哪个对象的方法,我这可怕的基本功(ˇˍˇ)