您的位置:首页 > 职场人生

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

2012-10-22 10:32 330 查看
/**
*
* 四个线程,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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐