求一个 Java 面试题的最佳实践
三个线程,分别输出 a 、b 、c,各输出 100 次,要求按 abc 的顺序输出
期待输出为:abcabcabcabc…
三个线程,分别输出 a 、b 、c,各输出 100 次,要求按 abc 的顺序输出
期待输出为:abcabcabcabc…
final Object xx1 = new Object();
final Object xx2 = new Object();
final Object xx3 = new Object();
Thread a = new Thread(() -> {
try {
synchronized (xx1) {
xx1.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
int a1 = 0;
while (a1 < 100) {
System.out.print(“a”);
a1++;
synchronized (xx2) {
xx2.notify();
}
try {
synchronized (xx1) {
xx1.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
a.setDaemon(true);
a.setName(“aaa”);
Thread b = new Thread(() -> {
try {
synchronized (xx2) {
xx2.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
int b1 = 0;
while (b1 < 100) {
System.out.print(“b”);
b1++;
synchronized (xx3) {
xx3.notify();
}
try {
synchronized (xx2) {
xx2.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
b.setDaemon(true);
b.setName(“bbb”);
Thread c = new Thread(() -> {
try {
synchronized (xx3) {
xx3.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
int c1 = 0;
while (c1 < 100) {
System.out.println(“c”);
c1++;
synchronized (xx1) {
xx1.notify();
}
try {
synchronized (xx3) {
xx3.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
c.setDaemon(true);
c.setName(“ccc”);
a.start();
b.start();
c.start();
synchronized (xx1) {
xx1.notify();
}
try {
Thread.sleep(100L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private volatile int state = 0;
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 3; i++) {
Thread thread = new Thread(new Volatile());
thread.start();
thread.join();
}
}
@Override
public void run() {
int count = 0;
while (true) {
if (count >= 100) {
break;
}
if (state == 0) {
System.out.println(“a”);
state = 1;
}
if (state == 1) {
System.out.println(“b”);
state = 2;
}
if (state == 2) {
System.out.println(“c”);
state = 0;
}
count++;
}
}
}
public Abc(int n) {
this.n = n;
object = new Object();
type = 0;
}
public void a(Runnable a) throws InterruptedException {
for (int i = 0; i < n; i++) {
synchronized (object) {
while (!(type == 0)) {
object.wait();
}
a.run();
type = 1;
object.notifyAll();
}
}
}
public void b(Runnable b) throws InterruptedException {
for (int i = 0; i < n; i++) {
synchronized (object) {
while (!(type == 1)) {
object.wait();
}
b.run();
type = 2;
object.notifyAll();
}
}
}
public void c(Runnable c) throws InterruptedException {
for (int i = 0; i < n; i++) {
synchronized (object) {
while (!(type == 2)) {
object.wait();
}
c.run();
type = 0;
object.notifyAll();
}
}
}
}
public synchronized void addChar(char ch){
arr[(int)(ch – ‘a’)]++;
print();
}
void print(){
while(arr[i] > 0){
打印((char)(arr[i] + ‘a’));
arr[i]–;
i = (i + 1)%3
}
}
}
for (int i = 0; i < 3; i++) {
Semaphore current = res[i];
Semaphore next = res[(i + 1) % 3];
final char ch = (char) (i + ‘a’);
new Thread(new Runnable() {
@Override
public void run() {
for (int j = 0; j < 100; j++) {
try {
current.acquire();
System.out.print(ch);
next.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
private static ReentrantLock lock = new ReentrantLock(true);
private String content;
public ReentrantLockDemo(String content) {
this.content = content;
}
@Override
public void run() {
while (true) {
try {
lock.lock();
System.out.println(content);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
public static void main(String[] args) {
ReentrantLockDemo a = new ReentrantLockDemo(“a”);
ReentrantLockDemo b = new ReentrantLockDemo(“b”);
ReentrantLockDemo c = new ReentrantLockDemo(“c”);
Thread threadA = new Thread(a);
Thread threadB = new Thread(b);
Thread threadC = new Thread(c);
threadA.start();
threadB.start();
threadC.start();
}
}
“`
https://gist.github.com/zzh7982/249883f379b50c5ae50eacd48736b5e2
public class Test {
public static void main(String[] args) {
AtomicInteger at = new AtomicInteger();
for (int i = 0; i < 3; i++) {
MyThread t = new MyThread(at, i);
t.start();
}
}
}
class MyThread extends Thread {
private AtomicInteger at;
private int index;
public MyThread(AtomicInteger at, int index) {
this.at = at;
this.index = index;
}
@Override
public void run() {
while(true) {
if(at.get() % 3 == index) {
System.out.print((char)(‘a’ + index));
at.incrementAndGet();
}
}
}
}
public static void main(String[] args) throws InterruptedException {
ExecutorService executorService1 = Executors.newSingleThreadExecutor();
ExecutorService executorService2 = Executors.newSingleThreadExecutor();
ExecutorService executorService3 = Executors.newSingleThreadExecutor();
Runnable[] rs = new Runnable[3];
rs[0] = () -> {
System.out.println(“A”);
executorService2.submit(rs[1]);
};
rs[1] = () -> {
System.out.println(“B”);
executorService2.submit(rs[2]);
};
rs[2] = new Runnable() {
private int a = 1;
public void run() {
System.out.println(“C”);
if (a++ < 100)
executorService3.submit(rs[0]);
}
};
executorService1.submit(rs[0]);
}
以上条件要全写对,比状态量的复杂。
https://gist.github.com/fantasticmao/f78ae0016a81877cf5019d9c22c81c73