fla*_*ash 0 java concurrency multithreading thread-safety
我正在处理下面的面试问题,我需要使用两个线程打印出字母和数字.一个打印字母(a,b,c ... z)和其他打印数字(1,2,3 ...... 26).现在我必须以这样的方式实现它,输出应该是:
a
1
b
2
...
...
z
26
Run Code Online (Sandbox Code Playgroud)
所以我想出了下面的代码一,没有同步,但由于某种原因它不打印最后一个字母表 z
class Output {
private static final int MAX = 26;
private static int count = 1;
private static final Queue<Character> queue = new LinkedList<>(Arrays.asList(new Character[] {
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}));
private boolean isAlphabet = true;
public void printAlphabet() {
while (true) {
if (count > MAX)
break;
if (!isAlphabet) {
System.err.println(Thread.currentThread().getName() + " : " + queue.remove());
isAlphabet = true;
}
}
}
public void printNumber() {
while (true) {
if (count > MAX)
break;
if (isAlphabet) {
System.err.println(Thread.currentThread().getName() + " : " + count++);
isAlphabet = false;
}
}
}
}
public class PrintAlphabetNumber {
public static void main(String[] args) {
Output p = new Output();
Thread t1 = new Thread(() -> p.printAlphabet());
t1.setName("Alphabet");
Thread t2 = new Thread(() -> p.printNumber());
t2.setName("Number");
t1.start();
t2.start();
}
}
Run Code Online (Sandbox Code Playgroud)
我上面的代码有什么问题吗?从同步的角度来看,它看起来好不好?
由于某种原因,它不打印最后一个字母表,即z
你中止的时候count > MAX
,在最后一个号码之后是真的.
在最后一个号码之后,你应该打印最后一个字母,但现在count > MAX
它已经停止了.
从同步的角度来看,它看起来好不好?
不,这看起来不太好.
你正在使用自旋锁.这是非常低效的,因为两个循环不断地使用100%CPU,无论他们是否有工作要做.它也不能保证与非易失性锁变量一起使用.
经典的Java解决方案将使用wait()/notify()
.