Java并发面试题(三个线程轮流打印十次abc)
方法1:用while循环和变量实现
static int index = 1;
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
if (index > 30) {
break;
}
if (index % 3 == 1) {
System.out.println("a");
index++;
}
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
if (index > 30) {
break;
}
if (index % 3 == 2) {
System.out.println("b");
index++;
}
}
}
});
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
if (index > 30) {
break;
}
if (index % 3 == 0) {
System.out.println("c");
index++;
}
}
}
});
t1.start();
t2.start();
t3.start();
}
方法2:用synchronized、wait、notifyAll实现
static int index = 1;
public static void main(String[] args) throws InterruptedException {
final Object lock = new Object();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
for (int i = 0;
i < 10;
i++) {
while (index % 3 != 1) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("a");
index++;
lock.notifyAll();
}
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
for (int i = 0;
i < 10;
i++) {
while (index % 3 != 2) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("b");
index++;
lock.notifyAll();
}
}
}
});
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
for (int i = 0;
i < 10;
i++) {
while (index % 3 != 0) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("c");
index++;
lock.notifyAll();
}
}
}
});
t1.start();
t2.start();
t3.start();
}
方法3:用ReentrantLock、1个Condition实现
static int index = 1;
public static void main(String[] args) {
final ReentrantLock lock = new ReentrantLock();
final Condition condition = lock.newCondition();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
lock.lock();
try {
for (int i = 0;
i < 10;
i++) {
while (index % 3 != 1) {
condition.await();
}
System.out.println("a");
index++;
condition.signalAll();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
lock.lock();
try {
for (int i = 0;
i < 10;
i++) {
while (index % 3 != 2) {
condition.await();
}
System.out.println("b");
index++;
condition.signalAll();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
});
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
lock.lock();
try {
for (int i = 0;
i < 10;
i++) {
while (index % 3 != 0) {
condition.await();
}
System.out.println("c");
index++;
condition.signalAll();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
});
t1.start();
t2.start();
t3.start();
}
方法4:用ReentrantLock、三个Condition实现
public static void main(String[] args) {
final Lock lock = new ReentrantLock();
final Condition condition1 = lock.newCondition();
final Condition condition2 = lock.newCondition();
final Condition condition3 = lock.newCondition();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
lock.lock();
try {
for (int i = 0;
i < 10;
i++) {
condition1.await();
System.out.println("a");
condition2.signal();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
lock.lock();
try {
for (int i = 0;
i < 10;
i++) {
condition2.await();
System.out.println("b");
condition3.signal();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
});
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
lock.lock();
try {
for (int i = 0;
i < 10;
i++) {
condition3.await();
System.out.println("c");
condition1.signal();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
});
t1.start();
t2.start();
t3.start();
try {
Thread.sleep(1000);
lock.lock();
condition1.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
方法5:用Semaphore实现
public static void main(String[] args) {
final Semaphore semaphore1 = new Semaphore(1);
final Semaphore semaphore2 = new Semaphore(0);
final Semaphore semaphore3 = new Semaphore(0);
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0;
i < 10;
i++) {
try {
semaphore1.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("a");
semaphore2.release();
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0;
i < 10;
i++) {
try {
semaphore2.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("b");
semaphore3.release();
}
}
});
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0;
i < 10;
i++) {
try {
semaphore3.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("c");
semaphore1.release();
}
}
});
t1.start();
t2.start();
t3.start();
}
【Java并发面试题(三个线程轮流打印十次abc)】转载于:https://www.cnblogs.com/jyx140521/p/6747750.html
推荐阅读
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 事件代理
- Java|Java OpenCV图像处理之SIFT角点检测详解
- java中如何实现重建二叉树
- 数组常用方法一
- 【Hadoop踩雷】Mac下安装Hadoop3以及Java版本问题
- Java|Java基础——数组
- RxJava|RxJava 在Android项目中的使用(一)
- java之static、static|java之static、static final、final的区别与应用
- 抱怨并没有任何意义