新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

使用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实现数据共享-创新互联
分享URL:http://www.wjwzjz.com/article/djseds.html
在线咨询
服务热线
服务热线:028-86922220
TOP