下面我们再给出一个线程不安全的例子。
例:1.8.2
class BookMark_to_win {
int bookNum=10;
void onlySellOne() {
if (bookNum > 0) {
System.out.println(Thread.currentThread().getName()
+ " before" + bookNum);
bookNum--;
try {
Thread.sleep(1000);
} catch (Exception e) {
}
System.out.println(Thread.currentThread().getName()
+ " after " + bookNum);
}
}
}
public class Test {
public static void main(String[] args) {
BookMark_to_win bookM = new BookMark_to_win();
while (bookM.bookNum>0) {
bookM.onlySellOne();
}
}
}
更多内容请见原文,文章转载自:https://blog.csdn.net/qq_44639795/article/details/103096071