创建两个线程,一个打印奇数,一个打印偶数。要求两个线程交替输出,顺序打印出 1 2 3 4 5 ... 100
。
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 {
while (count
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) break;
System.out.println(("ODD : " + count++));
evenSemaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(() -> {
while (count 100) break;
System.out.println(("EVEN : " + count++));
oddSemaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
创建三个线程,分别打印 "A"、"B"、"C",要求按顺序交替输出 ABCABC...
,共打印 10 次。
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
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
用3个线程打印 1~100
的整数,要求3个线程按顺序轮流打印
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 {
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();
}
}
}
import java.util.concurrent.locks.*;
public class Main {
private static int num = 1;
private static final Lock lock = new ReentrantLock();
private static final Condition[] cons = new Condition[3];
private static int state = 0;
public static void main(String[] args) {
for (int i = 0; i {
while (true) {
lock.lock();
try {
if (state % 3 != id) {
cons[id].await();
} else {
if (num > 100) {
for (Condition c : cons) c.signalAll();
break;
}
System.out.println(id + " " + num);
num++;
state = (state + 1) % 3;
cons[(id + 1) % 3].signal();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}).start();
}
}
}
参与评论
手机查看
返回顶部