使用Runnable实现数据共享-创新互联
使用Runnable实现数据共享,供大家参考,具体内容如下

先上代码:
public class TestThread {
private static final Logger logger = LoggerFactory.getLogger(TestThread.class);
private final class MyRunnable implements Runnable {
private int i;
public MyRunnable() {
this.i = 10;
}
public void run() {
while(i > 0) {
synchronized (this) {
if (i > 0) {
i--;
logger.debug("{} buy one ticket, {} left. ", Thread.currentThread().getName(), i);
}
}
}
}
}
@Test
public void testRunable() throws InterruptedException{
MyRunnable myRunnable = new MyRunnable();
Thread th2 = new Thread(myRunnable);
Thread th3 = new Thread(myRunnable);
th2.start();
th3.start();
th2.join();
th3.join();
}
} 分享题目:使用Runnable实现数据共享-创新互联
文章起源:http://www.scyingshan.cn/article/djseds.html


咨询
建站咨询
