java中如何解决线程不安全的问题呢
作者:互联网
下面我们再给出一个线程不安全的例子。
例: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
标签:java,currentThread,Thread,BookMark,win,bookM,安全,线程,bookNum 来源: https://www.cnblogs.com/xiaolongxia1922/p/14692433.html