马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
目录
交替打印奇偶数
标题概述
创建两个线程,一个打印奇数,一个打印偶数。要求两个线程交替输出,顺序打印出 1 2 3 4 5 ... 100。
解法一:synchronized + wait/notify
- public class Main {
- private static final Object lock = new Object();
- private static int count = 1;
- public static void main(String[] args) {
- Thread odd = new Thread(() -> {
- while (count <= 100) {
- synchronized (lock) {
- if ((count & 1) == 0) {
- try {
- lock.wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- } else {
- System.out.println(Thread.currentThread().getName() + ": " + count);
- count++;
- lock.notify();
- }
- }
- }
- }, "ODD");
- Thread even = new Thread(() -> {
- while (count <= 100) {
- synchronized (lock) {
- if ((count & 1) == 1) {
- try {
- lock.wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- } else {
- System.out.println(Thread.currentThread().getName() + ": " + count);
- count++;
- lock.notify();
- }
- }
- }
- }, "EVEN");
- odd.start();
- even.start();
- }
- }
复制代码 交替打印ABC
标题概述
创建三个线程,分别打印 "A"、"B"、"C",要求按顺序交替输出 ABCABC...,共打印 10 次。
解法一:synchronized + wait/notify
- import java.util.concurrent.Semaphore;
- public class Main {
- static int count = 1;
- static Semaphore oddSemaphore = new Semaphore(1);
- static Semaphore evenSemaphore = new Semaphore(0);
- public static void main(String[] args) {
- new Thread(() -> {
- while (count <= 100) {
- try {
- oddSemaphore.acquire();
- if (count > 100) break;
- System.out.println(("ODD : " + count++));
- evenSemaphore.release();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }).start();
- new Thread(() -> {
- while (count <= 100) {
- try {
- evenSemaphore.acquire();
- if (count > 100) break;
- System.out.println(("EVEN : " + count++));
- oddSemaphore.release();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }).start();
- }
- }
复制代码 解法二:ReentrantLock + Condition
- public class Main {
- private static int state = 0;
- private static final Object lock = new Object();
- public static void main(String[] args) {
- int loop = 10;
- new Thread(() -> printLetter('A', 0, 10)).start();
- new Thread(() -> printLetter('B', 1, 10)).start();
- new Thread(() -> printLetter('C', 2, 10)).start();
- }
- private static void printLetter(char ch, int target, int loop) {
- for (int i = 0; i < loop;) {
- synchronized (lock) {
- if (state != target) {
- try {
- lock.wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- } else {
- System.out.println(ch);
- state = (state + 1) % 3;
- lock.notifyAll();
- i++;
- }
- }
- }
- }
- }
复制代码 交替打印数字
标题概述
用3个线程打印 1~100 的整数,要求3个线程按顺序轮流打印
解法一:synchronized + wait/notify
- import java.util.concurrent.locks.*;
- public class Main {
- private static int state = 0;
- private static final Lock lock = new ReentrantLock();
- private static final Condition A = lock.newCondition();
- private static final Condition B = lock.newCondition();
- private static final Condition C = lock.newCondition();
- public static void main(String[] args) {
- int loop = 10;
- new Thread(() -> print('A', 0, A, B, loop)).start();
- new Thread(() -> print('B', 1, B, C, loop)).start();
- new Thread(() -> print('C', 2, C, A, loop)).start();
- }
- private static void print(char ch, int target, Condition curr, Condition next, int loop) {
- for (int i = 0; i < loop;) {
- lock.lock();
- try {
- while (state != target) {
- curr.await();
- }
- System.out.println(ch);
- state = (state + 1) % 3;
- i++;
- next.signal();
- } catch (InterruptedException e) {
- e.printStackTrace();
- } finally {
- lock.unlock();
- }
- }
- }
- }
复制代码 解法二:ReentrantLock + Condition
- public class Main {
- private static int count = 1;
- private static final Object lock = new Object();
- public static void main(String[] args) {
- for (int i = 0; i < 3; i++) {
- int id = i;
- new Thread(() -> {
- while (true) {
- synchronized (lock) {
- if (count > 100) break;
- if (count % 3 != id) {
- try {
- lock.wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- } else {
- System.out.println(count++);
- lock.notifyAll();
- }
- }
- }
- }).start();
- }
- }
- }
复制代码 总结与思考
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |