金山面试题--四个线程a,b,c,d. 线程a,b对变量i加一. 线程c,d对变量i减去一.四个线程顺序执行, 每个线程每次只执行一次.i的初始值为0, 打印结果0 1 2 1 0 1 2 1 0 1

这是一道关于多线程并发控制的面试题,线程a和b负责将变量i加一,线程c和d负责将i减一。题目要求四个线程按特定顺序执行,每个线程执行一次,初始值i为0。最终打印的i值序列展示了线程间的同步和互斥。解答中可能涉及到线程同步技术如锁、条件变量等。
/**
 * 
 * 四个线程,a b c d ,共享一个变量 i ab 为加线程, cd 为减线程,四个线程执行顺序为 abcd,且输出为 0 1 2 1 0 1 2 1 0
 * 1..... 共享一个变量,我们可以加一个锁就够了,关键是什么执行,什么时候等待
 * 其中一个线程执行后,会唤醒所有线程,进行CPU竞争,当然也包括它自己,一共四个线程,我们可以分配两个开关 ,flag1,flag2,共四种情况: a
 * true true b true false c false true d false false
 * 只有满足各自的条件者才可以执行,否则则等待,但这样四个线程则都必需有自己的逻辑处理,稍显啰嗦 所以还有一个更好的方法就是为每个线程都标一个ID,a b c
 * d 分别对象  0 1 2 3 ,这样,所有线程都共享一个 count 变量,每个纯种执行时,都 首先获得 count%4
 * 的值,如果对应自己线程的ID,那么就执行,否则等待,id=0 || id=1 表示是 a b 线程,则对 i 进行 加操作,否则进行 减操作
 * 这样四个线程都可以用同一个逻辑进行操作
 * 
 */

第一种解法 :

public class Jinsan2 {

    static int i = 0;
    static Object lock = new Object();
    static boolean flag1 = true, flag2 = true, flag3 = true;

    public static void main(String args[]) {

	Thread a = new Thread("a") {
	    @Override
	    public void run() {
		for (;;) {
		    synchronized (lock) {
			while (!flag1 || !flag2) {		// 可以过时,应 while
			    System.out.println(Thread.currentThread().getName()
				    + " wait");
			    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) {
			    System.out.println(Thread.currentThread().getName()
				    + " wait");
			    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) {
			    System.out.println(Thread.currentThread().getName()
				    + " wait");
			    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) {
			    System.out.println(Thread.currentThread().getName()
				    + " wait");
			    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();
    }
}

第二种解法


public class JinSan {
    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();
    }
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值