使用Runnable实现数据共享-创新互联
使用Runnable实现数据共享,供大家参考,具体内容如下
网站设计制作、网站设计,成都做网站公司-创新互联建站已向上1000+企业提供了,网站设计,网站制作,网络营销等服务!设计与技术结合,多年网站推广经验,合理的价格为您打造企业品质网站。先上代码:
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://pwwzsj.com/article/djseds.html